facet_core/_trait/impls/
scalar_impls.rs

1use typeid::ConstTypeId;
2
3use crate::value_vtable;
4use crate::*;
5use core::alloc::Layout;
6
7unsafe impl Facet for ConstTypeId {
8    const SHAPE: &'static Shape = &const {
9        Shape::builder()
10            .id(ConstTypeId::of::<ConstTypeId>())
11            .layout(Layout::new::<Self>())
12            .def(Def::Scalar(
13                ScalarDef::builder()
14                    .affinity(ScalarAffinity::opaque().build())
15                    .build(),
16            ))
17            .vtable(value_vtable!((), |f, _opts| write!(f, "ConstTypeId")))
18            .build()
19    };
20}
21
22unsafe impl Facet for () {
23    const SHAPE: &'static Shape = &const {
24        Shape::builder()
25            .id(ConstTypeId::of::<()>())
26            .layout(Layout::new::<Self>())
27            .def(Def::Scalar(
28                ScalarDef::builder()
29                    .affinity(ScalarAffinity::empty().build())
30                    .build(),
31            ))
32            .vtable(value_vtable!((), |f, _opts| write!(f, "()")))
33            .build()
34    };
35}
36
37#[cfg(feature = "std")]
38unsafe impl Facet for std::string::String {
39    const SHAPE: &'static Shape = &const {
40        Shape::builder()
41            .id(ConstTypeId::of::<String>())
42            .layout(Layout::new::<Self>())
43            .def(Def::Scalar(
44                ScalarDef::builder()
45                    // `String` is always on the heap
46                    .affinity(ScalarAffinity::string().max_inline_length(0).build())
47                    .build(),
48            ))
49            .vtable(value_vtable!(String, |f, _opts| write!(f, "String")))
50            .build()
51    };
52}
53
54unsafe impl Facet for &str {
55    const SHAPE: &'static Shape = &const {
56        Shape::builder()
57            .id(ConstTypeId::of::<&str>())
58            .layout(Layout::new::<Self>())
59            .def(Def::Scalar(
60                ScalarDef::builder()
61                    .affinity(ScalarAffinity::string().build())
62                    .build(),
63            ))
64            .vtable(value_vtable!(&str, |f, _opts| write!(f, "&str")))
65            .build()
66    };
67}
68
69#[cfg(feature = "std")]
70unsafe impl Facet for std::borrow::Cow<'_, str> {
71    const SHAPE: &'static Shape = &const {
72        Shape::builder()
73            .id(ConstTypeId::of::<std::borrow::Cow<'_, str>>())
74            .layout(Layout::new::<Self>())
75            .def(Def::Scalar(
76                ScalarDef::builder()
77                    .affinity(ScalarAffinity::string().build())
78                    .build(),
79            ))
80            .vtable(value_vtable!(std::borrow::Cow<'_, str>, |f, _opts| write!(
81                f,
82                "Cow<'_, str>"
83            )))
84            .build()
85    };
86}
87
88unsafe impl Facet for bool {
89    const SHAPE: &'static Shape = &const {
90        Shape::builder()
91            .id(ConstTypeId::of::<bool>())
92            .layout(Layout::new::<Self>())
93            .def(Def::Scalar(
94                ScalarDef::builder()
95                    .affinity(ScalarAffinity::boolean().build())
96                    .build(),
97            ))
98            .vtable(value_vtable!(bool, |f, _opts| write!(f, "bool")))
99            .build()
100    };
101}
102
103macro_rules! impl_facet_for_integer {
104    ($type:ty, $affinity:expr) => {
105        unsafe impl Facet for $type {
106            const SHAPE: &'static Shape = &const {
107                Shape::builder()
108                    .id(ConstTypeId::of::<$type>())
109                    .layout(Layout::new::<Self>())
110                    .def(Def::Scalar(
111                        ScalarDef::builder().affinity($affinity).build(),
112                    ))
113                    .vtable(value_vtable!($type, |f, _opts| write!(
114                        f,
115                        stringify!($type)
116                    )))
117                    .build()
118            };
119        }
120    };
121}
122
123static MIN_U8: u8 = u8::MIN;
124static MAX_U8: u8 = u8::MAX;
125
126static MIN_I8: i8 = i8::MIN;
127static MAX_I8: i8 = i8::MAX;
128
129static MIN_U16: u16 = u16::MIN;
130static MAX_U16: u16 = u16::MAX;
131
132static MIN_I16: i16 = i16::MIN;
133static MAX_I16: i16 = i16::MAX;
134
135static MIN_U32: u32 = u32::MIN;
136static MAX_U32: u32 = u32::MAX;
137
138static MIN_I32: i32 = i32::MIN;
139static MAX_I32: i32 = i32::MAX;
140
141static MIN_U64: u64 = u64::MIN;
142static MAX_U64: u64 = u64::MAX;
143
144static MIN_I64: i64 = i64::MIN;
145static MAX_I64: i64 = i64::MAX;
146
147static MIN_U128: u128 = u128::MIN;
148static MAX_U128: u128 = u128::MAX;
149
150static MIN_I128: i128 = i128::MIN;
151static MAX_I128: i128 = i128::MAX;
152
153static MIN_USIZE: usize = usize::MIN;
154static MAX_USIZE: usize = usize::MAX;
155
156static MIN_ISIZE: isize = isize::MIN;
157static MAX_ISIZE: isize = isize::MAX;
158
159impl_facet_for_integer!(
160    u8,
161    ScalarAffinity::number()
162        .unsigned_integer(8)
163        .min(OpaqueConst::new(&raw const MIN_U8))
164        .max(OpaqueConst::new(&raw const MAX_U8))
165        .build()
166);
167
168impl_facet_for_integer!(
169    i8,
170    ScalarAffinity::number()
171        .signed_integer(8)
172        .min(OpaqueConst::new(&raw const MIN_I8))
173        .max(OpaqueConst::new(&raw const MAX_I8))
174        .build()
175);
176
177impl_facet_for_integer!(
178    u16,
179    ScalarAffinity::number()
180        .unsigned_integer(16)
181        .min(OpaqueConst::new(&raw const MIN_U16))
182        .max(OpaqueConst::new(&raw const MAX_U16))
183        .build()
184);
185
186impl_facet_for_integer!(
187    i16,
188    ScalarAffinity::number()
189        .signed_integer(16)
190        .min(OpaqueConst::new(&raw const MIN_I16))
191        .max(OpaqueConst::new(&raw const MAX_I16))
192        .build()
193);
194
195impl_facet_for_integer!(
196    u32,
197    ScalarAffinity::number()
198        .unsigned_integer(32)
199        .min(OpaqueConst::new(&raw const MIN_U32))
200        .max(OpaqueConst::new(&raw const MAX_U32))
201        .build()
202);
203
204impl_facet_for_integer!(
205    i32,
206    ScalarAffinity::number()
207        .signed_integer(32)
208        .min(OpaqueConst::new(&raw const MIN_I32))
209        .max(OpaqueConst::new(&raw const MAX_I32))
210        .build()
211);
212
213impl_facet_for_integer!(
214    u64,
215    ScalarAffinity::number()
216        .unsigned_integer(64)
217        .min(OpaqueConst::new(&raw const MIN_U64))
218        .max(OpaqueConst::new(&raw const MAX_U64))
219        .build()
220);
221
222impl_facet_for_integer!(
223    i64,
224    ScalarAffinity::number()
225        .signed_integer(64)
226        .min(OpaqueConst::new(&raw const MIN_I64))
227        .max(OpaqueConst::new(&raw const MAX_I64))
228        .build()
229);
230
231impl_facet_for_integer!(
232    u128,
233    ScalarAffinity::number()
234        .unsigned_integer(128)
235        .min(OpaqueConst::new(&raw const MIN_U128))
236        .max(OpaqueConst::new(&raw const MAX_U128))
237        .build()
238);
239
240impl_facet_for_integer!(
241    i128,
242    ScalarAffinity::number()
243        .signed_integer(128)
244        .min(OpaqueConst::new(&raw const MIN_I128))
245        .max(OpaqueConst::new(&raw const MAX_I128))
246        .build()
247);
248
249impl_facet_for_integer!(
250    usize,
251    ScalarAffinity::number()
252        .unsigned_integer(core::mem::size_of::<usize>() * 8)
253        .min(OpaqueConst::new(&raw const MIN_USIZE))
254        .max(OpaqueConst::new(&raw const MAX_USIZE))
255        .build()
256);
257
258impl_facet_for_integer!(
259    isize,
260    ScalarAffinity::number()
261        .signed_integer(core::mem::size_of::<isize>() * 8)
262        .min(OpaqueConst::new(&raw const MIN_ISIZE))
263        .max(OpaqueConst::new(&raw const MAX_ISIZE))
264        .build()
265);
266
267// Constants for f32
268static MIN_F32: f32 = f32::MIN;
269static MAX_F32: f32 = f32::MAX;
270static POSITIVE_INFINITY_F32: f32 = f32::INFINITY;
271static NEGATIVE_INFINITY_F32: f32 = f32::NEG_INFINITY;
272static NAN_F32: f32 = f32::NAN;
273static POSITIVE_ZERO_F32: f32 = 0.0f32;
274static NEGATIVE_ZERO_F32: f32 = -0.0f32;
275
276// Constants for f64
277static MIN_F64: f64 = f64::MIN;
278static MAX_F64: f64 = f64::MAX;
279static POSITIVE_INFINITY_F64: f64 = f64::INFINITY;
280static NEGATIVE_INFINITY_F64: f64 = f64::NEG_INFINITY;
281static NAN_F64: f64 = f64::NAN;
282static POSITIVE_ZERO_F64: f64 = 0.0f64;
283static NEGATIVE_ZERO_F64: f64 = -0.0f64;
284
285unsafe impl Facet for f32 {
286    const SHAPE: &'static Shape = &const {
287        Shape::builder()
288            .id(ConstTypeId::of::<f32>())
289            .layout(Layout::new::<Self>())
290            .def(Def::Scalar(
291                ScalarDef::builder()
292                    .affinity(
293                        ScalarAffinity::number()
294                            .float(1, 8, 23)
295                            .min(OpaqueConst::new(&raw const MIN_F32))
296                            .max(OpaqueConst::new(&raw const MAX_F32))
297                            .positive_infinity(OpaqueConst::new(&raw const POSITIVE_INFINITY_F32))
298                            .negative_infinity(OpaqueConst::new(&raw const NEGATIVE_INFINITY_F32))
299                            .nan_sample(OpaqueConst::new(&raw const NAN_F32))
300                            .positive_zero(OpaqueConst::new(&raw const POSITIVE_ZERO_F32))
301                            .negative_zero(OpaqueConst::new(&raw const NEGATIVE_ZERO_F32))
302                            .build(),
303                    )
304                    .build(),
305            ))
306            .vtable(value_vtable!(f32, |f, _opts| write!(f, "f32")))
307            .build()
308    };
309}
310
311unsafe impl Facet for f64 {
312    const SHAPE: &'static Shape = &const {
313        Shape::builder()
314            .id(ConstTypeId::of::<f64>())
315            .layout(Layout::new::<Self>())
316            .def(Def::Scalar(
317                ScalarDef::builder()
318                    .affinity(
319                        ScalarAffinity::number()
320                            .float(1, 11, 52)
321                            .min(OpaqueConst::new(&raw const MIN_F64))
322                            .max(OpaqueConst::new(&raw const MAX_F64))
323                            .positive_infinity(OpaqueConst::new(&raw const POSITIVE_INFINITY_F64))
324                            .negative_infinity(OpaqueConst::new(&raw const NEGATIVE_INFINITY_F64))
325                            .nan_sample(OpaqueConst::new(&raw const NAN_F64))
326                            .positive_zero(OpaqueConst::new(&raw const POSITIVE_ZERO_F64))
327                            .negative_zero(OpaqueConst::new(&raw const NEGATIVE_ZERO_F64))
328                            .build(),
329                    )
330                    .build(),
331            ))
332            .vtable(value_vtable!(f64, |f, _opts| write!(f, "f64")))
333            .build()
334    };
335}
336
337#[cfg(feature = "std")]
338unsafe impl Facet for std::net::SocketAddr {
339    const SHAPE: &'static Shape = &const {
340        Shape::builder()
341            .id(ConstTypeId::of::<Self>())
342            .layout(Layout::new::<Self>())
343            .def(Def::Scalar(
344                ScalarDef::builder()
345                    .affinity(ScalarAffinity::socket_addr().build())
346                    .build(),
347            ))
348            .vtable(value_vtable!(std::net::SocketAddr, |f, _opts| write!(
349                f,
350                "SocketAddr"
351            )))
352            .build()
353    };
354}
355
356#[cfg(feature = "std")]
357unsafe impl Facet for std::net::IpAddr {
358    const SHAPE: &'static Shape = &const {
359        Shape::builder()
360            .id(ConstTypeId::of::<Self>())
361            .layout(Layout::new::<Self>())
362            .def(Def::Scalar(
363                ScalarDef::builder()
364                    .affinity(ScalarAffinity::ip_addr().build())
365                    .build(),
366            ))
367            .vtable(value_vtable!(std::net::IpAddr, |f, _opts| write!(
368                f,
369                "IpAddr"
370            )))
371            .build()
372    };
373}
374
375#[cfg(feature = "std")]
376unsafe impl Facet for std::net::Ipv4Addr {
377    const SHAPE: &'static Shape = &const {
378        Shape::builder()
379            .id(ConstTypeId::of::<Self>())
380            .layout(Layout::new::<Self>())
381            .def(Def::Scalar(
382                ScalarDef::builder()
383                    .affinity(ScalarAffinity::ip_addr().build())
384                    .build(),
385            ))
386            .vtable(value_vtable!(std::net::Ipv4Addr, |f, _opts| write!(
387                f,
388                "Ipv4Addr"
389            )))
390            .build()
391    };
392}
393
394#[cfg(feature = "std")]
395unsafe impl Facet for std::net::Ipv6Addr {
396    const SHAPE: &'static Shape = &const {
397        Shape::builder()
398            .id(ConstTypeId::of::<Self>())
399            .layout(Layout::new::<Self>())
400            .def(Def::Scalar(
401                ScalarDef::builder()
402                    .affinity(ScalarAffinity::ip_addr().build())
403                    .build(),
404            ))
405            .vtable(value_vtable!(std::net::Ipv6Addr, |f, _opts| write!(
406                f,
407                "Ipv6Addr"
408            )))
409            .build()
410    };
411}