1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
//! Simple reflection system based on the `#[repr(C)]` memory layout.
//!
//! You can derive [`HasSchema`] for your Rust types to unlock integration with the `bones_schema`
//! ecosystem, including `bones_ecs` and `bones_asset`.

#![warn(missing_docs)]
// This cfg_attr is needed because `rustdoc::all` includes lints not supported on stable
#![cfg_attr(doc, allow(unknown_lints))]
#![deny(rustdoc::all)]
// This allows us to use our stable polyfills for nightly APIs under the same name.
#![allow(unstable_name_collisions)]

// import the macros if the derive feature is enabled.
#[cfg(feature = "derive")]
pub use bones_schema_macros::*;

/// The prelude.
pub mod prelude {
    #[cfg(feature = "serde")]
    pub use crate::ser_de::*;
    pub use crate::{
        alloc::{SMap, SVec, SchemaMap, SchemaVec},
        ptr::*,
        registry::*,
        schema::*,
    };
    #[cfg(feature = "derive")]
    pub use bones_schema_macros::*;
    pub use bones_utils;
}

mod schema;
pub use schema::*;

pub mod alloc;
pub mod ptr;
pub mod raw_fns;
pub mod registry;

/// Implementations of [`HasSchema`] for standard types.
mod std_impls;

/// Serde implementations for [`Schema`].
#[cfg(feature = "serde")]
pub mod ser_de;

#[cfg(test)]
mod test {
    #[cfg(feature = "derive")]
    mod derive_test {
        #![allow(dead_code)]

        use crate::prelude::*;

        #[derive(HasSchema, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Default, Debug)]
        #[schema_module(crate)]
        #[repr(C, u8)]
        pub enum Maybe<T> {
            /// The value is not set.
            #[default]
            Unset,
            /// The value is set.
            Set(T),
        }

        #[derive(HasSchema, Clone, Copy, Debug, PartialEq, Eq, Default)]
        #[schema_module(crate)]
        #[repr(u8)]
        pub enum E {
            #[default]
            None,
            L,
            R,
            U,
            D,
            G,
            S,
        }

        /// Represents the ball in the game
        #[derive(HasSchema, Clone, Default)]
        #[schema_module(crate)]
        pub struct A {
            pub c: Maybe<u32>,
            pub d: SVec<u64>,
            pub e: Maybe<u64>,
            pub f: u32,
            pub g: f32,
            pub h: f32,
            pub i: E,
            pub j: u32,
            pub k: u32,
        }

        #[derive(HasSchema, Clone, Default)]
        #[schema_module(crate)]
        #[repr(C)]
        pub struct B {
            pub c: Maybe<u32>,
            pub d: SVec<u64>,
            pub e: Maybe<u64>,
            pub f: u32,
            pub g: f32,
            pub h: f32,
            pub i: E,
            pub j: u32,
            pub k: u32,
        }

        #[derive(HasSchema, Clone, Default)]
        #[schema_module(crate)]
        pub struct C {
            pub c: Maybe<u32>,
            pub e: Maybe<u64>,
        }

        #[derive(HasSchema, Clone, Default)]
        #[schema_module(crate)]
        #[repr(C)]
        pub struct D {
            pub c: Maybe<u32>,
            pub e: Maybe<u64>,
        }

        #[derive(HasSchema, Clone)]
        #[schema(no_default)]
        #[schema_module(crate)]
        #[repr(C)]
        struct F<T> {
            a: bool,
            b: T,
        }

        /// Makes sure that the layout reported in the schema for a generic type matches the layout
        /// reported by Rust, for two different type parameters.
        #[test]
        fn generic_type_schema_layouts_match() {
            assert_eq!(
                Maybe::<u32>::schema().layout(),
                std::alloc::Layout::new::<Maybe<u32>>()
            );
            assert_eq!(
                Maybe::<u64>::schema().layout(),
                std::alloc::Layout::new::<Maybe<u64>>()
            );
            assert_eq!(
                F::<u64>::schema().layout(),
                std::alloc::Layout::new::<F<u64>>()
            );
            assert_eq!(
                F::<u32>::schema().layout(),
                std::alloc::Layout::new::<F<u32>>()
            );

            // Check a normal enum too, just in case.
            assert_eq!(E::schema().layout(), std::alloc::Layout::new::<E>());
        }

        // Makes sure that the layout reported for two structs, where the only difference between
        // them is the `#[repr(C)]` annotation, matches.
        #[test]
        fn schema_layout_for_repr_c_matches_repr_rust() {
            assert_eq!(A::schema().layout(), B::schema().layout());
            assert_eq!(C::schema().layout(), D::schema().layout());
        }
    }
}