facet_core/_trait/impls/
scalar_impls.rs

1use crate::value_vtable;
2use crate::*;
3use core::alloc::Layout;
4
5unsafe impl Facet for () {
6    const SHAPE: &'static Shape = &const {
7        Shape::builder()
8            .layout(Layout::new::<Self>())
9            .def(Def::Scalar(
10                ScalarDef::builder().fully_qualified_type_name("()").build(),
11            ))
12            .vtable(value_vtable!((), |f, _opts| write!(f, "()")))
13            .build()
14    };
15}
16
17unsafe impl Facet for String {
18    const SHAPE: &'static Shape = &const {
19        Shape::builder()
20            .layout(Layout::new::<Self>())
21            .def(Def::Scalar(
22                ScalarDef::builder()
23                    .fully_qualified_type_name("alloc::string::String")
24                    .build(),
25            ))
26            .vtable(value_vtable!(String, |f, _opts| write!(f, "String")))
27            .build()
28    };
29}
30
31unsafe impl Facet for &str {
32    const SHAPE: &'static Shape = &const {
33        Shape::builder()
34            .layout(Layout::new::<Self>())
35            .def(Def::Scalar(
36                ScalarDef::builder()
37                    .fully_qualified_type_name("&core::primitive::str")
38                    .build(),
39            ))
40            .vtable(value_vtable!(&str, |f, _opts| write!(f, "&str")))
41            .build()
42    };
43}
44
45// FIXME: That's wrong. This is an enum, so it should be treated as an enum.
46#[cfg(feature = "std")]
47unsafe impl Facet for std::borrow::Cow<'_, str> {
48    const SHAPE: &'static Shape = &const {
49        Shape::builder()
50            .layout(Layout::new::<Self>())
51            .def(Def::Scalar(
52                ScalarDef::builder()
53                    .fully_qualified_type_name("std::borrow::Cow<'_, str>")
54                    .build(),
55            ))
56            .vtable(value_vtable!(std::borrow::Cow<'_, str>, |f, _opts| write!(
57                f,
58                "Cow<'_, str>"
59            )))
60            .build()
61    };
62}
63
64unsafe impl Facet for bool {
65    const SHAPE: &'static Shape = &const {
66        Shape::builder()
67            .layout(Layout::new::<Self>())
68            .def(Def::Scalar(
69                ScalarDef::builder()
70                    .fully_qualified_type_name("core::primitive::bool")
71                    .build(),
72            ))
73            .vtable(value_vtable!(bool, |f, _opts| write!(f, "bool")))
74            .build()
75    };
76}
77
78macro_rules! impl_facet_for_integer {
79    ($type:ty) => {
80        unsafe impl Facet for $type {
81            const SHAPE: &'static Shape = &const {
82                Shape::builder()
83                    .layout(Layout::new::<Self>())
84                    .def(Def::Scalar(
85                        ScalarDef::builder()
86                            .fully_qualified_type_name(stringify!($type))
87                            .build(),
88                    ))
89                    .vtable(value_vtable!($type, |f, _opts| write!(
90                        f,
91                        stringify!($type)
92                    )))
93                    .build()
94            };
95        }
96    };
97}
98
99impl_facet_for_integer!(core::primitive::u8);
100impl_facet_for_integer!(core::primitive::i8);
101impl_facet_for_integer!(core::primitive::u16);
102impl_facet_for_integer!(core::primitive::i16);
103impl_facet_for_integer!(core::primitive::u32);
104impl_facet_for_integer!(core::primitive::i32);
105impl_facet_for_integer!(core::primitive::u64);
106impl_facet_for_integer!(core::primitive::i64);
107impl_facet_for_integer!(core::primitive::u128);
108impl_facet_for_integer!(core::primitive::i128);
109impl_facet_for_integer!(core::primitive::usize);
110impl_facet_for_integer!(core::primitive::isize);
111
112macro_rules! impl_facet_for_float {
113    ($type:ty) => {
114        unsafe impl Facet for $type {
115            const SHAPE: &'static Shape = &const {
116                Shape::builder()
117                    .layout(Layout::new::<Self>())
118                    .def(Def::Scalar(
119                        ScalarDef::builder()
120                            .fully_qualified_type_name(stringify!($type))
121                            .build(),
122                    ))
123                    .vtable(value_vtable!($type, |f, _opts| write!(
124                        f,
125                        stringify!($type)
126                    )))
127                    .build()
128            };
129        }
130    };
131}
132
133impl_facet_for_float!(core::primitive::f32);
134impl_facet_for_float!(core::primitive::f64);
135
136#[cfg(feature = "std")]
137unsafe impl Facet for std::net::SocketAddr {
138    const SHAPE: &'static Shape = &const {
139        Shape::builder()
140            .layout(Layout::new::<Self>())
141            .def(Def::Scalar(
142                ScalarDef::builder()
143                    .fully_qualified_type_name("std::net::SocketAddr")
144                    .build(),
145            ))
146            .vtable(value_vtable!(std::net::SocketAddr, |f, _opts| write!(
147                f,
148                "SocketAddr"
149            )))
150            .build()
151    };
152}