facet_core/_trait/impls/
scalar_impls.rs

1use crate::value_vtable;
2use crate::*;
3use core::alloc::Layout;
4use core::num::NonZero;
5use typeid::ConstTypeId;
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
37unsafe impl<T: ?Sized> Facet for core::marker::PhantomData<T> {
38    const SHAPE: &'static Shape = &const {
39        Shape::builder()
40            .id(ConstTypeId::of::<Self>())
41            .layout(Layout::new::<Self>())
42            .def(Def::Scalar(
43                ScalarDef::builder()
44                    .affinity(ScalarAffinity::empty().build())
45                    .build(),
46            ))
47            .vtable(value_vtable!((), |f, _opts| write!(f, "PhantomData")))
48            .build()
49    };
50}
51
52#[cfg(feature = "alloc")]
53unsafe impl Facet for alloc::string::String {
54    const SHAPE: &'static Shape = &const {
55        Shape::builder()
56            .id(ConstTypeId::of::<alloc::string::String>())
57            .layout(Layout::new::<Self>())
58            .def(Def::Scalar(
59                ScalarDef::builder()
60                    // `String` is always on the heap
61                    .affinity(ScalarAffinity::string().max_inline_length(0).build())
62                    .build(),
63            ))
64            .vtable(value_vtable!(alloc::string::String, |f, _opts| write!(
65                f,
66                "String"
67            )))
68            .build()
69    };
70}
71
72unsafe impl Facet for char {
73    const SHAPE: &'static Shape = &const {
74        Shape::builder()
75            .id(ConstTypeId::of::<char>())
76            .layout(Layout::new::<Self>())
77            .def(Def::Scalar(
78                ScalarDef::builder()
79                    .affinity(ScalarAffinity::char().build())
80                    .build(),
81            ))
82            .vtable(value_vtable!(char, |f, _opts| write!(f, "char")))
83            .build()
84    };
85}
86
87unsafe impl Facet for &str {
88    const SHAPE: &'static Shape = &const {
89        Shape::builder()
90            .id(ConstTypeId::of::<&str>())
91            .layout(Layout::new::<Self>())
92            .def(Def::Scalar(
93                ScalarDef::builder()
94                    .affinity(ScalarAffinity::string().build())
95                    .build(),
96            ))
97            .vtable(value_vtable!(&str, |f, _opts| write!(f, "&str")))
98            .build()
99    };
100}
101
102#[cfg(feature = "alloc")]
103unsafe impl Facet for alloc::borrow::Cow<'_, str> {
104    const SHAPE: &'static Shape = &const {
105        Shape::builder()
106            .id(ConstTypeId::of::<alloc::borrow::Cow<'_, str>>())
107            .layout(Layout::new::<Self>())
108            .def(Def::Scalar(
109                ScalarDef::builder()
110                    .affinity(ScalarAffinity::string().build())
111                    .build(),
112            ))
113            .vtable(value_vtable!(
114                alloc::borrow::Cow<'_, str>,
115                |f, _opts| write!(f, "Cow<'_, str>")
116            ))
117            .build()
118    };
119}
120
121unsafe impl Facet for bool {
122    const SHAPE: &'static Shape = &const {
123        Shape::builder()
124            .id(ConstTypeId::of::<bool>())
125            .layout(Layout::new::<Self>())
126            .def(Def::Scalar(
127                ScalarDef::builder()
128                    .affinity(ScalarAffinity::boolean().build())
129                    .build(),
130            ))
131            .vtable(value_vtable!(bool, |f, _opts| write!(f, "bool")))
132            .build()
133    };
134}
135
136macro_rules! impl_facet_for_integer {
137    ($type:ty, $affinity:expr, $nz_affinity:expr) => {
138        unsafe impl Facet for $type {
139            const SHAPE: &'static Shape = &const {
140                Shape::builder()
141                    .id(ConstTypeId::of::<Self>())
142                    .layout(Layout::new::<Self>())
143                    .def(Def::Scalar(
144                        ScalarDef::builder().affinity($affinity).build(),
145                    ))
146                    .vtable(value_vtable!($type, |f, _opts| write!(
147                        f,
148                        stringify!($type)
149                    )))
150                    .build()
151            };
152        }
153
154        unsafe impl Facet for NonZero<$type> {
155            const SHAPE: &'static Shape = &const {
156                Shape::builder()
157                    .id(ConstTypeId::of::<Self>())
158                    .layout(Layout::new::<Self>())
159                    .def(Def::Scalar(
160                        ScalarDef::builder().affinity($nz_affinity).build(),
161                    ))
162                    .vtable(value_vtable!($type, |f, _opts| write!(
163                        f,
164                        "core::num::NonZero<{}>",
165                        stringify!($type)
166                    )))
167                    .build()
168            };
169        }
170    };
171}
172
173static MIN_U8: u8 = u8::MIN;
174static MAX_U8: u8 = u8::MAX;
175static MIN_NZ_U8: NonZero<u8> = NonZero::<u8>::MIN;
176static MAX_NZ_U8: NonZero<u8> = NonZero::<u8>::MAX;
177
178static MIN_I8: i8 = i8::MIN;
179static MAX_I8: i8 = i8::MAX;
180static MIN_NZ_I8: NonZero<i8> = NonZero::<i8>::MIN;
181static MAX_NZ_I8: NonZero<i8> = NonZero::<i8>::MAX;
182
183static MIN_U16: u16 = u16::MIN;
184static MAX_U16: u16 = u16::MAX;
185static MIN_NZ_U16: NonZero<u16> = NonZero::<u16>::MIN;
186static MAX_NZ_U16: NonZero<u16> = NonZero::<u16>::MAX;
187
188static MIN_I16: i16 = i16::MIN;
189static MAX_I16: i16 = i16::MAX;
190static MIN_NZ_I16: NonZero<i16> = NonZero::<i16>::MIN;
191static MAX_NZ_I16: NonZero<i16> = NonZero::<i16>::MAX;
192
193static MIN_U32: u32 = u32::MIN;
194static MAX_U32: u32 = u32::MAX;
195static MIN_NZ_U32: NonZero<u32> = NonZero::<u32>::MIN;
196static MAX_NZ_U32: NonZero<u32> = NonZero::<u32>::MAX;
197
198static MIN_I32: i32 = i32::MIN;
199static MAX_I32: i32 = i32::MAX;
200static MIN_NZ_I32: NonZero<i32> = NonZero::<i32>::MIN;
201static MAX_NZ_I32: NonZero<i32> = NonZero::<i32>::MAX;
202
203static MIN_U64: u64 = u64::MIN;
204static MAX_U64: u64 = u64::MAX;
205static MIN_NZ_U64: NonZero<u64> = NonZero::<u64>::MIN;
206static MAX_NZ_U64: NonZero<u64> = NonZero::<u64>::MAX;
207
208static MIN_I64: i64 = i64::MIN;
209static MAX_I64: i64 = i64::MAX;
210static MIN_NZ_I64: NonZero<i64> = NonZero::<i64>::MIN;
211static MAX_NZ_I64: NonZero<i64> = NonZero::<i64>::MAX;
212
213static MIN_U128: u128 = u128::MIN;
214static MAX_U128: u128 = u128::MAX;
215static MIN_NZ_U128: NonZero<u128> = NonZero::<u128>::MIN;
216static MAX_NZ_U128: NonZero<u128> = NonZero::<u128>::MAX;
217
218static MIN_I128: i128 = i128::MIN;
219static MAX_I128: i128 = i128::MAX;
220static MIN_NZ_I128: NonZero<i128> = NonZero::<i128>::MIN;
221static MAX_NZ_I128: NonZero<i128> = NonZero::<i128>::MAX;
222
223static MIN_USIZE: usize = usize::MIN;
224static MAX_USIZE: usize = usize::MAX;
225static MIN_NZ_USIZE: NonZero<usize> = NonZero::<usize>::MIN;
226static MAX_NZ_USIZE: NonZero<usize> = NonZero::<usize>::MAX;
227
228static MIN_ISIZE: isize = isize::MIN;
229static MAX_ISIZE: isize = isize::MAX;
230static MIN_NZ_ISIZE: NonZero<isize> = NonZero::<isize>::MIN;
231static MAX_NZ_ISIZE: NonZero<isize> = NonZero::<isize>::MAX;
232
233impl_facet_for_integer!(
234    u8,
235    ScalarAffinity::number()
236        .unsigned_integer(8)
237        .min(OpaqueConst::new(&raw const MIN_U8))
238        .max(OpaqueConst::new(&raw const MAX_U8))
239        .build(),
240    ScalarAffinity::number()
241        .unsigned_integer(8)
242        .min(OpaqueConst::new(&raw const MIN_NZ_U8))
243        .max(OpaqueConst::new(&raw const MAX_NZ_U8))
244        .build()
245);
246
247impl_facet_for_integer!(
248    i8,
249    ScalarAffinity::number()
250        .signed_integer(8)
251        .min(OpaqueConst::new(&raw const MIN_I8))
252        .max(OpaqueConst::new(&raw const MAX_I8))
253        .build(),
254    ScalarAffinity::number()
255        .signed_integer(8)
256        .min(OpaqueConst::new(&raw const MIN_NZ_I8))
257        .max(OpaqueConst::new(&raw const MAX_NZ_I8))
258        .build()
259);
260
261impl_facet_for_integer!(
262    u16,
263    ScalarAffinity::number()
264        .unsigned_integer(16)
265        .min(OpaqueConst::new(&raw const MIN_U16))
266        .max(OpaqueConst::new(&raw const MAX_U16))
267        .build(),
268    ScalarAffinity::number()
269        .unsigned_integer(16)
270        .min(OpaqueConst::new(&raw const MIN_NZ_U16))
271        .max(OpaqueConst::new(&raw const MAX_NZ_U16))
272        .build()
273);
274
275impl_facet_for_integer!(
276    i16,
277    ScalarAffinity::number()
278        .signed_integer(16)
279        .min(OpaqueConst::new(&raw const MIN_I16))
280        .max(OpaqueConst::new(&raw const MAX_I16))
281        .build(),
282    ScalarAffinity::number()
283        .signed_integer(16)
284        .min(OpaqueConst::new(&raw const MIN_NZ_I16))
285        .max(OpaqueConst::new(&raw const MAX_NZ_I16))
286        .build()
287);
288
289impl_facet_for_integer!(
290    u32,
291    ScalarAffinity::number()
292        .unsigned_integer(32)
293        .min(OpaqueConst::new(&raw const MIN_U32))
294        .max(OpaqueConst::new(&raw const MAX_U32))
295        .build(),
296    ScalarAffinity::number()
297        .unsigned_integer(32)
298        .min(OpaqueConst::new(&raw const MIN_NZ_U32))
299        .max(OpaqueConst::new(&raw const MAX_NZ_U32))
300        .build()
301);
302
303impl_facet_for_integer!(
304    i32,
305    ScalarAffinity::number()
306        .signed_integer(32)
307        .min(OpaqueConst::new(&raw const MIN_I32))
308        .max(OpaqueConst::new(&raw const MAX_I32))
309        .build(),
310    ScalarAffinity::number()
311        .signed_integer(32)
312        .min(OpaqueConst::new(&raw const MIN_NZ_I32))
313        .max(OpaqueConst::new(&raw const MAX_NZ_I32))
314        .build()
315);
316
317impl_facet_for_integer!(
318    u64,
319    ScalarAffinity::number()
320        .unsigned_integer(64)
321        .min(OpaqueConst::new(&raw const MIN_U64))
322        .max(OpaqueConst::new(&raw const MAX_U64))
323        .build(),
324    ScalarAffinity::number()
325        .unsigned_integer(64)
326        .min(OpaqueConst::new(&raw const MIN_NZ_U64))
327        .max(OpaqueConst::new(&raw const MAX_NZ_U64))
328        .build()
329);
330
331impl_facet_for_integer!(
332    i64,
333    ScalarAffinity::number()
334        .signed_integer(64)
335        .min(OpaqueConst::new(&raw const MIN_I64))
336        .max(OpaqueConst::new(&raw const MAX_I64))
337        .build(),
338    ScalarAffinity::number()
339        .signed_integer(64)
340        .min(OpaqueConst::new(&raw const MIN_NZ_I64))
341        .max(OpaqueConst::new(&raw const MAX_NZ_I64))
342        .build()
343);
344
345impl_facet_for_integer!(
346    u128,
347    ScalarAffinity::number()
348        .unsigned_integer(128)
349        .min(OpaqueConst::new(&raw const MIN_U128))
350        .max(OpaqueConst::new(&raw const MAX_U128))
351        .build(),
352    ScalarAffinity::number()
353        .unsigned_integer(128)
354        .min(OpaqueConst::new(&raw const MIN_NZ_U128))
355        .max(OpaqueConst::new(&raw const MAX_NZ_U128))
356        .build()
357);
358
359impl_facet_for_integer!(
360    i128,
361    ScalarAffinity::number()
362        .signed_integer(128)
363        .min(OpaqueConst::new(&raw const MIN_I128))
364        .max(OpaqueConst::new(&raw const MAX_I128))
365        .build(),
366    ScalarAffinity::number()
367        .signed_integer(128)
368        .min(OpaqueConst::new(&raw const MIN_NZ_I128))
369        .max(OpaqueConst::new(&raw const MAX_NZ_I128))
370        .build()
371);
372
373impl_facet_for_integer!(
374    usize,
375    ScalarAffinity::number()
376        .unsigned_integer(core::mem::size_of::<usize>() * 8)
377        .min(OpaqueConst::new(&raw const MIN_USIZE))
378        .max(OpaqueConst::new(&raw const MAX_USIZE))
379        .build(),
380    ScalarAffinity::number()
381        .unsigned_integer(core::mem::size_of::<usize>() * 8)
382        .min(OpaqueConst::new(&raw const MIN_NZ_USIZE))
383        .max(OpaqueConst::new(&raw const MAX_NZ_USIZE))
384        .build()
385);
386
387impl_facet_for_integer!(
388    isize,
389    ScalarAffinity::number()
390        .signed_integer(core::mem::size_of::<isize>() * 8)
391        .min(OpaqueConst::new(&raw const MIN_ISIZE))
392        .max(OpaqueConst::new(&raw const MAX_ISIZE))
393        .build(),
394    ScalarAffinity::number()
395        .signed_integer(core::mem::size_of::<isize>() * 8)
396        .min(OpaqueConst::new(&raw const MIN_NZ_ISIZE))
397        .max(OpaqueConst::new(&raw const MAX_NZ_ISIZE))
398        .build()
399);
400
401// Constants for f32
402static MIN_F32: f32 = f32::MIN;
403static MAX_F32: f32 = f32::MAX;
404static POSITIVE_INFINITY_F32: f32 = f32::INFINITY;
405static NEGATIVE_INFINITY_F32: f32 = f32::NEG_INFINITY;
406static NAN_F32: f32 = f32::NAN;
407static POSITIVE_ZERO_F32: f32 = 0.0f32;
408static NEGATIVE_ZERO_F32: f32 = -0.0f32;
409
410// Constants for f64
411static MIN_F64: f64 = f64::MIN;
412static MAX_F64: f64 = f64::MAX;
413static POSITIVE_INFINITY_F64: f64 = f64::INFINITY;
414static NEGATIVE_INFINITY_F64: f64 = f64::NEG_INFINITY;
415static NAN_F64: f64 = f64::NAN;
416static POSITIVE_ZERO_F64: f64 = 0.0f64;
417static NEGATIVE_ZERO_F64: f64 = -0.0f64;
418
419unsafe impl Facet for f32 {
420    const SHAPE: &'static Shape = &const {
421        Shape::builder()
422            .id(ConstTypeId::of::<f32>())
423            .layout(Layout::new::<Self>())
424            .def(Def::Scalar(
425                ScalarDef::builder()
426                    .affinity(
427                        ScalarAffinity::number()
428                            .float(1, 8, 23)
429                            .min(OpaqueConst::new(&raw const MIN_F32))
430                            .max(OpaqueConst::new(&raw const MAX_F32))
431                            .positive_infinity(OpaqueConst::new(&raw const POSITIVE_INFINITY_F32))
432                            .negative_infinity(OpaqueConst::new(&raw const NEGATIVE_INFINITY_F32))
433                            .nan_sample(OpaqueConst::new(&raw const NAN_F32))
434                            .positive_zero(OpaqueConst::new(&raw const POSITIVE_ZERO_F32))
435                            .negative_zero(OpaqueConst::new(&raw const NEGATIVE_ZERO_F32))
436                            .build(),
437                    )
438                    .build(),
439            ))
440            .vtable(value_vtable!(f32, |f, _opts| write!(f, "f32")))
441            .build()
442    };
443}
444
445unsafe impl Facet for f64 {
446    const SHAPE: &'static Shape = &const {
447        Shape::builder()
448            .id(ConstTypeId::of::<f64>())
449            .layout(Layout::new::<Self>())
450            .def(Def::Scalar(
451                ScalarDef::builder()
452                    .affinity(
453                        ScalarAffinity::number()
454                            .float(1, 11, 52)
455                            .min(OpaqueConst::new(&raw const MIN_F64))
456                            .max(OpaqueConst::new(&raw const MAX_F64))
457                            .positive_infinity(OpaqueConst::new(&raw const POSITIVE_INFINITY_F64))
458                            .negative_infinity(OpaqueConst::new(&raw const NEGATIVE_INFINITY_F64))
459                            .nan_sample(OpaqueConst::new(&raw const NAN_F64))
460                            .positive_zero(OpaqueConst::new(&raw const POSITIVE_ZERO_F64))
461                            .negative_zero(OpaqueConst::new(&raw const NEGATIVE_ZERO_F64))
462                            .build(),
463                    )
464                    .build(),
465            ))
466            .vtable(value_vtable!(f64, |f, _opts| write!(f, "f64")))
467            .build()
468    };
469}
470
471unsafe impl Facet for core::net::SocketAddr {
472    const SHAPE: &'static Shape = &const {
473        Shape::builder()
474            .id(ConstTypeId::of::<Self>())
475            .layout(Layout::new::<Self>())
476            .def(Def::Scalar(
477                ScalarDef::builder()
478                    .affinity(ScalarAffinity::socket_addr().build())
479                    .build(),
480            ))
481            .vtable(value_vtable!(core::net::SocketAddr, |f, _opts| write!(
482                f,
483                "SocketAddr"
484            )))
485            .build()
486    };
487}
488
489unsafe impl Facet for core::net::IpAddr {
490    const SHAPE: &'static Shape = &const {
491        Shape::builder()
492            .id(ConstTypeId::of::<Self>())
493            .layout(Layout::new::<Self>())
494            .def(Def::Scalar(
495                ScalarDef::builder()
496                    .affinity(ScalarAffinity::ip_addr().build())
497                    .build(),
498            ))
499            .vtable(value_vtable!(core::net::IpAddr, |f, _opts| write!(
500                f,
501                "IpAddr"
502            )))
503            .build()
504    };
505}
506
507unsafe impl Facet for core::net::Ipv4Addr {
508    const SHAPE: &'static Shape = &const {
509        Shape::builder()
510            .id(ConstTypeId::of::<Self>())
511            .layout(Layout::new::<Self>())
512            .def(Def::Scalar(
513                ScalarDef::builder()
514                    .affinity(ScalarAffinity::ip_addr().build())
515                    .build(),
516            ))
517            .vtable(value_vtable!(core::net::Ipv4Addr, |f, _opts| write!(
518                f,
519                "Ipv4Addr"
520            )))
521            .build()
522    };
523}
524
525unsafe impl Facet for core::net::Ipv6Addr {
526    const SHAPE: &'static Shape = &const {
527        Shape::builder()
528            .id(ConstTypeId::of::<Self>())
529            .layout(Layout::new::<Self>())
530            .def(Def::Scalar(
531                ScalarDef::builder()
532                    .affinity(ScalarAffinity::ip_addr().build())
533                    .build(),
534            ))
535            .vtable(value_vtable!(core::net::Ipv6Addr, |f, _opts| write!(
536                f,
537                "Ipv6Addr"
538            )))
539            .build()
540    };
541}