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