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);
92impl_facet_for_integer!(usize);
93impl_facet_for_integer!(isize);
94
95macro_rules! impl_facet_for_float {
96    ($type:ty) => {
97        unsafe impl Facet for $type {
98            const ARCHETYPE: Self = 0.0;
99            const SHAPE: &'static Shape = &const {
100                Shape::builder()
101                    .layout(Layout::new::<Self>())
102                    .def(Def::Scalar(ScalarDef::of::<Self>()))
103                    .vtable(value_vtable!($type, |f, _opts| write!(
104                        f,
105                        stringify!($type)
106                    )))
107                    .build()
108            };
109        }
110    };
111}
112
113impl_facet_for_float!(f32);
114impl_facet_for_float!(f64);
115
116#[cfg(feature = "std")]
117unsafe impl Facet for std::net::SocketAddr {
118    const ARCHETYPE: Self = std::net::SocketAddr::V4(std::net::SocketAddrV4::new(
119        std::net::Ipv4Addr::new(0, 0, 0, 0),
120        0,
121    ));
122    const SHAPE: &'static Shape = &const {
123        Shape::builder()
124            .layout(Layout::new::<Self>())
125            .def(Def::Scalar(ScalarDef::of::<Self>()))
126            .vtable(value_vtable!(std::net::SocketAddr, |f, _opts| write!(
127                f,
128                "SocketAddr"
129            )))
130            .build()
131    };
132}