facet_trait/impls/
scalar_impls.rs

1use crate::value_vtable;
2use crate::*;
3use core::alloc::Layout;
4
5unsafe impl Facet for () {
6    const ARCHETYPE: Self = ();
7    const SHAPE: &'static Shape = &const {
8        Shape::builder()
9            .layout(Layout::new::<Self>())
10            .def(Def::Scalar(ScalarDef::of::<Self>()))
11            .vtable(value_vtable!((), |f, _opts| write!(f, "()")))
12            .build()
13    };
14}
15
16unsafe impl Facet for String {
17    const ARCHETYPE: Self = String::new();
18    const SHAPE: &'static Shape = &const {
19        Shape::builder()
20            .layout(Layout::new::<Self>())
21            .def(Def::Scalar(ScalarDef::of::<Self>()))
22            .vtable(value_vtable!(String, |f, _opts| write!(f, "String")))
23            .build()
24    };
25}
26
27unsafe impl Facet for &str {
28    const ARCHETYPE: Self = "";
29    const SHAPE: &'static Shape = &const {
30        Shape::builder()
31            .layout(Layout::new::<Self>())
32            .def(Def::Scalar(ScalarDef::of::<Self>()))
33            .vtable(value_vtable!(&str, |f, _opts| write!(f, "&str")))
34            .build()
35    };
36}
37
38#[cfg(feature = "std")]
39unsafe impl Facet for std::borrow::Cow<'_, str> {
40    const ARCHETYPE: Self = std::borrow::Cow::Borrowed("");
41    const SHAPE: &'static Shape = &const {
42        Shape::builder()
43            .layout(Layout::new::<Self>())
44            .def(Def::Scalar(ScalarDef::of::<Self>()))
45            .vtable(value_vtable!(std::borrow::Cow<'_, str>, |f, _opts| write!(
46                f,
47                "Cow<'_, str>"
48            )))
49            .build()
50    };
51}
52
53unsafe impl Facet for bool {
54    const ARCHETYPE: Self = false;
55    const SHAPE: &'static Shape = &const {
56        Shape::builder()
57            .layout(Layout::new::<Self>())
58            .def(Def::Scalar(ScalarDef::of::<Self>()))
59            .vtable(value_vtable!(bool, |f, _opts| write!(f, "bool")))
60            .build()
61    };
62}
63
64macro_rules! impl_facet_for_integer {
65    ($type:ty) => {
66        unsafe impl Facet for $type {
67            const ARCHETYPE: Self = 0;
68            const SHAPE: &'static Shape = &const {
69                Shape::builder()
70                    .layout(Layout::new::<Self>())
71                    .def(Def::Scalar(ScalarDef::of::<Self>()))
72                    .vtable(value_vtable!($type, |f, _opts| write!(
73                        f,
74                        stringify!($type)
75                    )))
76                    .build()
77            };
78        }
79    };
80}
81
82impl_facet_for_integer!(u8);
83impl_facet_for_integer!(i8);
84impl_facet_for_integer!(u16);
85impl_facet_for_integer!(i16);
86impl_facet_for_integer!(u32);
87impl_facet_for_integer!(i32);
88impl_facet_for_integer!(u64);
89impl_facet_for_integer!(i64);
90impl_facet_for_integer!(u128);
91impl_facet_for_integer!(i128);
92
93macro_rules! impl_facet_for_float {
94    ($type:ty) => {
95        unsafe impl Facet for $type {
96            const ARCHETYPE: Self = 0.0;
97            const SHAPE: &'static Shape = &const {
98                Shape::builder()
99                    .layout(Layout::new::<Self>())
100                    .def(Def::Scalar(ScalarDef::of::<Self>()))
101                    .vtable(value_vtable!($type, |f, _opts| write!(
102                        f,
103                        stringify!($type)
104                    )))
105                    .build()
106            };
107        }
108    };
109}
110
111impl_facet_for_float!(f32);
112impl_facet_for_float!(f64);