facet_core/impls_core/
scalar.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(PtrConst::new(&raw const MIN_U8))
238        .max(PtrConst::new(&raw const MAX_U8))
239        .build(),
240    ScalarAffinity::number()
241        .unsigned_integer(8)
242        .min(PtrConst::new(&raw const MIN_NZ_U8))
243        .max(PtrConst::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(PtrConst::new(&raw const MIN_I8))
252        .max(PtrConst::new(&raw const MAX_I8))
253        .build(),
254    ScalarAffinity::number()
255        .signed_integer(8)
256        .min(PtrConst::new(&raw const MIN_NZ_I8))
257        .max(PtrConst::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(PtrConst::new(&raw const MIN_U16))
266        .max(PtrConst::new(&raw const MAX_U16))
267        .build(),
268    ScalarAffinity::number()
269        .unsigned_integer(16)
270        .min(PtrConst::new(&raw const MIN_NZ_U16))
271        .max(PtrConst::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(PtrConst::new(&raw const MIN_I16))
280        .max(PtrConst::new(&raw const MAX_I16))
281        .build(),
282    ScalarAffinity::number()
283        .signed_integer(16)
284        .min(PtrConst::new(&raw const MIN_NZ_I16))
285        .max(PtrConst::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(PtrConst::new(&raw const MIN_U32))
294        .max(PtrConst::new(&raw const MAX_U32))
295        .build(),
296    ScalarAffinity::number()
297        .unsigned_integer(32)
298        .min(PtrConst::new(&raw const MIN_NZ_U32))
299        .max(PtrConst::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(PtrConst::new(&raw const MIN_I32))
308        .max(PtrConst::new(&raw const MAX_I32))
309        .build(),
310    ScalarAffinity::number()
311        .signed_integer(32)
312        .min(PtrConst::new(&raw const MIN_NZ_I32))
313        .max(PtrConst::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(PtrConst::new(&raw const MIN_U64))
322        .max(PtrConst::new(&raw const MAX_U64))
323        .build(),
324    ScalarAffinity::number()
325        .unsigned_integer(64)
326        .min(PtrConst::new(&raw const MIN_NZ_U64))
327        .max(PtrConst::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(PtrConst::new(&raw const MIN_I64))
336        .max(PtrConst::new(&raw const MAX_I64))
337        .build(),
338    ScalarAffinity::number()
339        .signed_integer(64)
340        .min(PtrConst::new(&raw const MIN_NZ_I64))
341        .max(PtrConst::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(PtrConst::new(&raw const MIN_U128))
350        .max(PtrConst::new(&raw const MAX_U128))
351        .build(),
352    ScalarAffinity::number()
353        .unsigned_integer(128)
354        .min(PtrConst::new(&raw const MIN_NZ_U128))
355        .max(PtrConst::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(PtrConst::new(&raw const MIN_I128))
364        .max(PtrConst::new(&raw const MAX_I128))
365        .build(),
366    ScalarAffinity::number()
367        .signed_integer(128)
368        .min(PtrConst::new(&raw const MIN_NZ_I128))
369        .max(PtrConst::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(PtrConst::new(&raw const MIN_USIZE))
378        .max(PtrConst::new(&raw const MAX_USIZE))
379        .build(),
380    ScalarAffinity::number()
381        .unsigned_integer(core::mem::size_of::<usize>() * 8)
382        .min(PtrConst::new(&raw const MIN_NZ_USIZE))
383        .max(PtrConst::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(PtrConst::new(&raw const MIN_ISIZE))
392        .max(PtrConst::new(&raw const MAX_ISIZE))
393        .build(),
394    ScalarAffinity::number()
395        .signed_integer(core::mem::size_of::<isize>() * 8)
396        .min(PtrConst::new(&raw const MIN_NZ_ISIZE))
397        .max(PtrConst::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;
409static EPSILON_F32: f32 = f32::EPSILON;
410
411// Constants for f64
412static MIN_F64: f64 = f64::MIN;
413static MAX_F64: f64 = f64::MAX;
414static POSITIVE_INFINITY_F64: f64 = f64::INFINITY;
415static NEGATIVE_INFINITY_F64: f64 = f64::NEG_INFINITY;
416static NAN_F64: f64 = f64::NAN;
417static POSITIVE_ZERO_F64: f64 = 0.0f64;
418static NEGATIVE_ZERO_F64: f64 = -0.0f64;
419static EPSILON_F64: f64 = f64::EPSILON;
420
421unsafe impl Facet for f32 {
422    const SHAPE: &'static Shape = &const {
423        Shape::builder()
424            .id(ConstTypeId::of::<f32>())
425            .layout(Layout::new::<Self>())
426            .def(Def::Scalar(
427                ScalarDef::builder()
428                    .affinity(
429                        ScalarAffinity::number()
430                            .float(1, 8, f32::MANTISSA_DIGITS as usize - 1, false)
431                            .min(PtrConst::new(&raw const MIN_F32))
432                            .max(PtrConst::new(&raw const MAX_F32))
433                            .positive_infinity(PtrConst::new(&raw const POSITIVE_INFINITY_F32))
434                            .negative_infinity(PtrConst::new(&raw const NEGATIVE_INFINITY_F32))
435                            .nan_sample(PtrConst::new(&raw const NAN_F32))
436                            .positive_zero(PtrConst::new(&raw const POSITIVE_ZERO_F32))
437                            .negative_zero(PtrConst::new(&raw const NEGATIVE_ZERO_F32))
438                            .epsilon(PtrConst::new(&raw const EPSILON_F32))
439                            .build(),
440                    )
441                    .build(),
442            ))
443            .vtable(value_vtable!(f32, |f, _opts| write!(f, "f32")))
444            .build()
445    };
446}
447
448unsafe impl Facet for f64 {
449    const SHAPE: &'static Shape = &const {
450        Shape::builder()
451            .id(ConstTypeId::of::<f64>())
452            .layout(Layout::new::<Self>())
453            .def(Def::Scalar(
454                ScalarDef::builder()
455                    .affinity(
456                        ScalarAffinity::number()
457                            .float(1, 11, f64::MANTISSA_DIGITS as usize - 1, false)
458                            .min(PtrConst::new(&raw const MIN_F64))
459                            .max(PtrConst::new(&raw const MAX_F64))
460                            .positive_infinity(PtrConst::new(&raw const POSITIVE_INFINITY_F64))
461                            .negative_infinity(PtrConst::new(&raw const NEGATIVE_INFINITY_F64))
462                            .nan_sample(PtrConst::new(&raw const NAN_F64))
463                            .positive_zero(PtrConst::new(&raw const POSITIVE_ZERO_F64))
464                            .negative_zero(PtrConst::new(&raw const NEGATIVE_ZERO_F64))
465                            .epsilon(PtrConst::new(&raw const EPSILON_F64))
466                            .build(),
467                    )
468                    .build(),
469            ))
470            .vtable(value_vtable!(f64, |f, _opts| write!(f, "f64")))
471            .build()
472    };
473}
474
475unsafe impl Facet for core::net::SocketAddr {
476    const SHAPE: &'static Shape = &const {
477        Shape::builder()
478            .id(ConstTypeId::of::<Self>())
479            .layout(Layout::new::<Self>())
480            .def(Def::Scalar(
481                ScalarDef::builder()
482                    .affinity(ScalarAffinity::socket_addr().build())
483                    .build(),
484            ))
485            .vtable(value_vtable!(core::net::SocketAddr, |f, _opts| write!(
486                f,
487                "SocketAddr"
488            )))
489            .build()
490    };
491}
492
493unsafe impl Facet for core::net::IpAddr {
494    const SHAPE: &'static Shape = &const {
495        Shape::builder()
496            .id(ConstTypeId::of::<Self>())
497            .layout(Layout::new::<Self>())
498            .def(Def::Scalar(
499                ScalarDef::builder()
500                    .affinity(ScalarAffinity::ip_addr().build())
501                    .build(),
502            ))
503            .vtable(value_vtable!(core::net::IpAddr, |f, _opts| write!(
504                f,
505                "IpAddr"
506            )))
507            .build()
508    };
509}
510
511unsafe impl Facet for core::net::Ipv4Addr {
512    const SHAPE: &'static Shape = &const {
513        Shape::builder()
514            .id(ConstTypeId::of::<Self>())
515            .layout(Layout::new::<Self>())
516            .def(Def::Scalar(
517                ScalarDef::builder()
518                    .affinity(ScalarAffinity::ip_addr().build())
519                    .build(),
520            ))
521            .vtable(value_vtable!(core::net::Ipv4Addr, |f, _opts| write!(
522                f,
523                "Ipv4Addr"
524            )))
525            .build()
526    };
527}
528
529unsafe impl Facet for core::net::Ipv6Addr {
530    const SHAPE: &'static Shape = &const {
531        Shape::builder()
532            .id(ConstTypeId::of::<Self>())
533            .layout(Layout::new::<Self>())
534            .def(Def::Scalar(
535                ScalarDef::builder()
536                    .affinity(ScalarAffinity::ip_addr().build())
537                    .build(),
538            ))
539            .vtable(value_vtable!(core::net::Ipv6Addr, |f, _opts| write!(
540                f,
541                "Ipv6Addr"
542            )))
543            .build()
544    };
545}