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<'a, T: ?Sized + 'a> Facet<'a> 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<'a> Facet<'a> for &'a 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<'a> Facet<'a> for alloc::borrow::Cow<'a, 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<'a> Facet<'a> 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(
148                        &const {
149                            let mut vtable = value_vtable_inner!($type, |f, _opts| write!(
150                                f,
151                                "{}",
152                                stringify!($type)
153                            ));
154
155                            vtable.try_from = Some(|source, source_shape, dest| {
156                                if source_shape == Self::SHAPE {
157                                    return Ok(unsafe { dest.copy_from(source, source_shape)? });
158                                }
159                                if source_shape == u64::SHAPE {
160                                    let value: u64 = *unsafe { source.get::<u64>() };
161                                    let converted: $type = value as $type;
162                                    return Ok(unsafe { dest.put::<$type>(converted) });
163                                }
164                                if source_shape == i64::SHAPE {
165                                    let value: i64 = *unsafe { source.get::<i64>() };
166                                    let converted: $type = value as $type;
167                                    return Ok(unsafe { dest.put::<$type>(converted) });
168                                }
169                                if source_shape == f64::SHAPE {
170                                    let value: f64 = *unsafe { source.get::<f64>() };
171                                    let converted: $type = value as $type;
172                                    return Ok(unsafe { dest.put::<$type>(converted) });
173                                }
174                                Err(TryFromError::Incompatible)
175                            });
176
177                            vtable
178                        },
179                    )
180                    .build()
181            };
182        }
183
184        unsafe impl<'a> Facet<'a> for NonZero<$type> {
185            const SHAPE: &'static Shape = &const {
186                // Define conversion functions for transparency
187                unsafe fn try_from_inner<'dst>(
188                    src_ptr: PtrConst<'_>,
189                    src_shape: &'static Shape,
190                    dst: PtrUninit<'dst>,
191                ) -> Result<PtrMut<'dst>, TryFromInnerError> {
192                    if src_shape.id != <$type as Facet>::SHAPE.id {
193                        return Err(TryFromInnerError::UnsupportedSourceShape {
194                            src_shape,
195                            expected: &[<$type as Facet>::SHAPE],
196                        });
197                    }
198
199                    // Get the inner value and check that it's non-zero
200                    let value = unsafe { *src_ptr.get::<$type>() };
201                    let nz = NonZero::new(value)
202                        .ok_or_else(|| TryFromInnerError::InvariantNotRespected)?;
203
204                    // Put the NonZero value into the destination
205                    Ok(unsafe { dst.put(nz) })
206                }
207
208                unsafe fn try_into_inner<'dst>(
209                    src_ptr: PtrConst<'_>,
210                    dst: PtrUninit<'dst>,
211                ) -> Result<PtrMut<'dst>, TryIntoInnerError> {
212                    // Get the NonZero value and extract the inner value
213                    let nz = unsafe { *src_ptr.get::<NonZero<$type>>() };
214                    // Put the inner value into the destination
215                    Ok(unsafe { dst.put(nz.get()) })
216                }
217
218                unsafe fn try_borrow_inner(
219                    src_ptr: PtrConst<'_>,
220                ) -> Result<PtrConst<'_>, TryBorrowInnerError> {
221                    // NonZero<T> has the same memory layout as T, so we can return the input pointer directly
222                    Ok(src_ptr)
223                }
224
225                // Function to return inner type's shape
226                fn inner_shape() -> &'static Shape {
227                    <$type as Facet>::SHAPE
228                }
229
230                Shape::builder()
231                    .id(ConstTypeId::of::<Self>())
232                    .layout(Layout::new::<Self>())
233                    .def(Def::Scalar(
234                        ScalarDef::builder().affinity($nz_affinity).build(),
235                    ))
236                    .vtable(
237                        &const {
238                            let mut vtable = value_vtable_inner!($type, |f, _opts| write!(
239                                f,
240                                "NonZero<{}>",
241                                stringify!($type)
242                            ));
243
244                            // Keep existing try_from for compatibility
245                            vtable.try_from = Some(|source, source_shape, dest| {
246                                if source_shape == Self::SHAPE {
247                                    return Ok(unsafe { dest.copy_from(source, source_shape)? });
248                                }
249                                if source_shape == <$type>::SHAPE {
250                                    let value: $type = *unsafe { source.get::<$type>() };
251                                    let nz = NonZero::new(value).ok_or_else(|| {
252                                        TryFromError::Generic("value must be non-zero")
253                                    })?;
254                                    return Ok(unsafe { dest.put::<NonZero<$type>>(nz) });
255                                }
256                                Err(TryFromError::Incompatible)
257                            });
258
259                            // Add our new transparency functions
260                            vtable.try_from_inner = Some(try_from_inner);
261                            vtable.try_into_inner = Some(try_into_inner);
262                            vtable.try_borrow_inner = Some(try_borrow_inner);
263
264                            vtable
265                        },
266                    )
267                    .inner(inner_shape)
268                    .build()
269            };
270        }
271    };
272}
273
274static MIN_U8: u8 = u8::MIN;
275static MAX_U8: u8 = u8::MAX;
276static MIN_NZ_U8: NonZero<u8> = NonZero::<u8>::MIN;
277static MAX_NZ_U8: NonZero<u8> = NonZero::<u8>::MAX;
278
279static MIN_I8: i8 = i8::MIN;
280static MAX_I8: i8 = i8::MAX;
281static MIN_NZ_I8: NonZero<i8> = NonZero::<i8>::MIN;
282static MAX_NZ_I8: NonZero<i8> = NonZero::<i8>::MAX;
283
284static MIN_U16: u16 = u16::MIN;
285static MAX_U16: u16 = u16::MAX;
286static MIN_NZ_U16: NonZero<u16> = NonZero::<u16>::MIN;
287static MAX_NZ_U16: NonZero<u16> = NonZero::<u16>::MAX;
288
289static MIN_I16: i16 = i16::MIN;
290static MAX_I16: i16 = i16::MAX;
291static MIN_NZ_I16: NonZero<i16> = NonZero::<i16>::MIN;
292static MAX_NZ_I16: NonZero<i16> = NonZero::<i16>::MAX;
293
294static MIN_U32: u32 = u32::MIN;
295static MAX_U32: u32 = u32::MAX;
296static MIN_NZ_U32: NonZero<u32> = NonZero::<u32>::MIN;
297static MAX_NZ_U32: NonZero<u32> = NonZero::<u32>::MAX;
298
299static MIN_I32: i32 = i32::MIN;
300static MAX_I32: i32 = i32::MAX;
301static MIN_NZ_I32: NonZero<i32> = NonZero::<i32>::MIN;
302static MAX_NZ_I32: NonZero<i32> = NonZero::<i32>::MAX;
303
304static MIN_U64: u64 = u64::MIN;
305static MAX_U64: u64 = u64::MAX;
306static MIN_NZ_U64: NonZero<u64> = NonZero::<u64>::MIN;
307static MAX_NZ_U64: NonZero<u64> = NonZero::<u64>::MAX;
308
309static MIN_I64: i64 = i64::MIN;
310static MAX_I64: i64 = i64::MAX;
311static MIN_NZ_I64: NonZero<i64> = NonZero::<i64>::MIN;
312static MAX_NZ_I64: NonZero<i64> = NonZero::<i64>::MAX;
313
314static MIN_U128: u128 = u128::MIN;
315static MAX_U128: u128 = u128::MAX;
316static MIN_NZ_U128: NonZero<u128> = NonZero::<u128>::MIN;
317static MAX_NZ_U128: NonZero<u128> = NonZero::<u128>::MAX;
318
319static MIN_I128: i128 = i128::MIN;
320static MAX_I128: i128 = i128::MAX;
321static MIN_NZ_I128: NonZero<i128> = NonZero::<i128>::MIN;
322static MAX_NZ_I128: NonZero<i128> = NonZero::<i128>::MAX;
323
324static MIN_USIZE: usize = usize::MIN;
325static MAX_USIZE: usize = usize::MAX;
326static MIN_NZ_USIZE: NonZero<usize> = NonZero::<usize>::MIN;
327static MAX_NZ_USIZE: NonZero<usize> = NonZero::<usize>::MAX;
328
329static MIN_ISIZE: isize = isize::MIN;
330static MAX_ISIZE: isize = isize::MAX;
331static MIN_NZ_ISIZE: NonZero<isize> = NonZero::<isize>::MIN;
332static MAX_NZ_ISIZE: NonZero<isize> = NonZero::<isize>::MAX;
333
334impl_facet_for_integer!(
335    u8,
336    ScalarAffinity::number()
337        .unsigned_integer(8)
338        .min(PtrConst::new(&raw const MIN_U8))
339        .max(PtrConst::new(&raw const MAX_U8))
340        .build(),
341    ScalarAffinity::number()
342        .unsigned_integer(8)
343        .min(PtrConst::new(&raw const MIN_NZ_U8))
344        .max(PtrConst::new(&raw const MAX_NZ_U8))
345        .build()
346);
347
348impl_facet_for_integer!(
349    i8,
350    ScalarAffinity::number()
351        .signed_integer(8)
352        .min(PtrConst::new(&raw const MIN_I8))
353        .max(PtrConst::new(&raw const MAX_I8))
354        .build(),
355    ScalarAffinity::number()
356        .signed_integer(8)
357        .min(PtrConst::new(&raw const MIN_NZ_I8))
358        .max(PtrConst::new(&raw const MAX_NZ_I8))
359        .build()
360);
361
362impl_facet_for_integer!(
363    u16,
364    ScalarAffinity::number()
365        .unsigned_integer(16)
366        .min(PtrConst::new(&raw const MIN_U16))
367        .max(PtrConst::new(&raw const MAX_U16))
368        .build(),
369    ScalarAffinity::number()
370        .unsigned_integer(16)
371        .min(PtrConst::new(&raw const MIN_NZ_U16))
372        .max(PtrConst::new(&raw const MAX_NZ_U16))
373        .build()
374);
375
376impl_facet_for_integer!(
377    i16,
378    ScalarAffinity::number()
379        .signed_integer(16)
380        .min(PtrConst::new(&raw const MIN_I16))
381        .max(PtrConst::new(&raw const MAX_I16))
382        .build(),
383    ScalarAffinity::number()
384        .signed_integer(16)
385        .min(PtrConst::new(&raw const MIN_NZ_I16))
386        .max(PtrConst::new(&raw const MAX_NZ_I16))
387        .build()
388);
389
390impl_facet_for_integer!(
391    u32,
392    ScalarAffinity::number()
393        .unsigned_integer(32)
394        .min(PtrConst::new(&raw const MIN_U32))
395        .max(PtrConst::new(&raw const MAX_U32))
396        .build(),
397    ScalarAffinity::number()
398        .unsigned_integer(32)
399        .min(PtrConst::new(&raw const MIN_NZ_U32))
400        .max(PtrConst::new(&raw const MAX_NZ_U32))
401        .build()
402);
403
404impl_facet_for_integer!(
405    i32,
406    ScalarAffinity::number()
407        .signed_integer(32)
408        .min(PtrConst::new(&raw const MIN_I32))
409        .max(PtrConst::new(&raw const MAX_I32))
410        .build(),
411    ScalarAffinity::number()
412        .signed_integer(32)
413        .min(PtrConst::new(&raw const MIN_NZ_I32))
414        .max(PtrConst::new(&raw const MAX_NZ_I32))
415        .build()
416);
417
418impl_facet_for_integer!(
419    u64,
420    ScalarAffinity::number()
421        .unsigned_integer(64)
422        .min(PtrConst::new(&raw const MIN_U64))
423        .max(PtrConst::new(&raw const MAX_U64))
424        .build(),
425    ScalarAffinity::number()
426        .unsigned_integer(64)
427        .min(PtrConst::new(&raw const MIN_NZ_U64))
428        .max(PtrConst::new(&raw const MAX_NZ_U64))
429        .build()
430);
431
432impl_facet_for_integer!(
433    i64,
434    ScalarAffinity::number()
435        .signed_integer(64)
436        .min(PtrConst::new(&raw const MIN_I64))
437        .max(PtrConst::new(&raw const MAX_I64))
438        .build(),
439    ScalarAffinity::number()
440        .signed_integer(64)
441        .min(PtrConst::new(&raw const MIN_NZ_I64))
442        .max(PtrConst::new(&raw const MAX_NZ_I64))
443        .build()
444);
445
446impl_facet_for_integer!(
447    u128,
448    ScalarAffinity::number()
449        .unsigned_integer(128)
450        .min(PtrConst::new(&raw const MIN_U128))
451        .max(PtrConst::new(&raw const MAX_U128))
452        .build(),
453    ScalarAffinity::number()
454        .unsigned_integer(128)
455        .min(PtrConst::new(&raw const MIN_NZ_U128))
456        .max(PtrConst::new(&raw const MAX_NZ_U128))
457        .build()
458);
459
460impl_facet_for_integer!(
461    i128,
462    ScalarAffinity::number()
463        .signed_integer(128)
464        .min(PtrConst::new(&raw const MIN_I128))
465        .max(PtrConst::new(&raw const MAX_I128))
466        .build(),
467    ScalarAffinity::number()
468        .signed_integer(128)
469        .min(PtrConst::new(&raw const MIN_NZ_I128))
470        .max(PtrConst::new(&raw const MAX_NZ_I128))
471        .build()
472);
473
474impl_facet_for_integer!(
475    usize,
476    ScalarAffinity::number()
477        .unsigned_integer(core::mem::size_of::<usize>() * 8)
478        .min(PtrConst::new(&raw const MIN_USIZE))
479        .max(PtrConst::new(&raw const MAX_USIZE))
480        .build(),
481    ScalarAffinity::number()
482        .unsigned_integer(core::mem::size_of::<usize>() * 8)
483        .min(PtrConst::new(&raw const MIN_NZ_USIZE))
484        .max(PtrConst::new(&raw const MAX_NZ_USIZE))
485        .build()
486);
487
488impl_facet_for_integer!(
489    isize,
490    ScalarAffinity::number()
491        .signed_integer(core::mem::size_of::<isize>() * 8)
492        .min(PtrConst::new(&raw const MIN_ISIZE))
493        .max(PtrConst::new(&raw const MAX_ISIZE))
494        .build(),
495    ScalarAffinity::number()
496        .signed_integer(core::mem::size_of::<isize>() * 8)
497        .min(PtrConst::new(&raw const MIN_NZ_ISIZE))
498        .max(PtrConst::new(&raw const MAX_NZ_ISIZE))
499        .build()
500);
501
502// Constants for f32
503static MIN_F32: f32 = f32::MIN;
504static MAX_F32: f32 = f32::MAX;
505static POSITIVE_INFINITY_F32: f32 = f32::INFINITY;
506static NEGATIVE_INFINITY_F32: f32 = f32::NEG_INFINITY;
507static NAN_F32: f32 = f32::NAN;
508static POSITIVE_ZERO_F32: f32 = 0.0f32;
509static NEGATIVE_ZERO_F32: f32 = -0.0f32;
510static EPSILON_F32: f32 = f32::EPSILON;
511
512// Constants for f64
513static MIN_F64: f64 = f64::MIN;
514static MAX_F64: f64 = f64::MAX;
515static POSITIVE_INFINITY_F64: f64 = f64::INFINITY;
516static NEGATIVE_INFINITY_F64: f64 = f64::NEG_INFINITY;
517static NAN_F64: f64 = f64::NAN;
518static POSITIVE_ZERO_F64: f64 = 0.0f64;
519static NEGATIVE_ZERO_F64: f64 = -0.0f64;
520static EPSILON_F64: f64 = f64::EPSILON;
521
522unsafe impl Facet<'_> for f32 {
523    const SHAPE: &'static Shape = &const {
524        Shape::builder()
525            .id(ConstTypeId::of::<f32>())
526            .layout(Layout::new::<Self>())
527            .def(Def::Scalar(
528                ScalarDef::builder()
529                    .affinity(
530                        ScalarAffinity::number()
531                            .float(1, 8, f32::MANTISSA_DIGITS as usize - 1, false)
532                            .min(PtrConst::new(&raw const MIN_F32))
533                            .max(PtrConst::new(&raw const MAX_F32))
534                            .positive_infinity(PtrConst::new(&raw const POSITIVE_INFINITY_F32))
535                            .negative_infinity(PtrConst::new(&raw const NEGATIVE_INFINITY_F32))
536                            .nan_sample(PtrConst::new(&raw const NAN_F32))
537                            .positive_zero(PtrConst::new(&raw const POSITIVE_ZERO_F32))
538                            .negative_zero(PtrConst::new(&raw const NEGATIVE_ZERO_F32))
539                            .epsilon(PtrConst::new(&raw const EPSILON_F32))
540                            .build(),
541                    )
542                    .build(),
543            ))
544            .vtable(
545                &const {
546                    let mut vtable = value_vtable_inner!(f32, |f, _opts| write!(f, "f32"));
547
548                    vtable.try_from = Some(|source, source_shape, dest| {
549                        if source_shape == Self::SHAPE {
550                            return Ok(unsafe { dest.copy_from(source, source_shape)? });
551                        }
552                        if source_shape == u64::SHAPE {
553                            let value: u64 = *unsafe { source.get::<u64>() };
554                            let converted: f32 = value as f32;
555                            return Ok(unsafe { dest.put::<f32>(converted) });
556                        }
557                        if source_shape == i64::SHAPE {
558                            let value: i64 = *unsafe { source.get::<i64>() };
559                            let converted: f32 = value as f32;
560                            return Ok(unsafe { dest.put::<f32>(converted) });
561                        }
562                        if source_shape == f64::SHAPE {
563                            let value: f64 = *unsafe { source.get::<f64>() };
564                            let converted: f32 = value as f32;
565                            return Ok(unsafe { dest.put::<f32>(converted) });
566                        }
567                        Err(TryFromError::Incompatible)
568                    });
569
570                    vtable
571                },
572            )
573            .build()
574    };
575}
576
577unsafe impl Facet<'_> for f64 {
578    const SHAPE: &'static Shape = &const {
579        Shape::builder()
580            .id(ConstTypeId::of::<f64>())
581            .layout(Layout::new::<Self>())
582            .def(Def::Scalar(
583                ScalarDef::builder()
584                    .affinity(
585                        ScalarAffinity::number()
586                            .float(1, 11, f64::MANTISSA_DIGITS as usize - 1, false)
587                            .min(PtrConst::new(&raw const MIN_F64))
588                            .max(PtrConst::new(&raw const MAX_F64))
589                            .positive_infinity(PtrConst::new(&raw const POSITIVE_INFINITY_F64))
590                            .negative_infinity(PtrConst::new(&raw const NEGATIVE_INFINITY_F64))
591                            .nan_sample(PtrConst::new(&raw const NAN_F64))
592                            .positive_zero(PtrConst::new(&raw const POSITIVE_ZERO_F64))
593                            .negative_zero(PtrConst::new(&raw const NEGATIVE_ZERO_F64))
594                            .epsilon(PtrConst::new(&raw const EPSILON_F64))
595                            .build(),
596                    )
597                    .build(),
598            ))
599            .vtable(
600                &const {
601                    let mut vtable = value_vtable_inner!(f64, |f, _opts| write!(f, "f64"));
602
603                    vtable.try_from = Some(|source, source_shape, dest| {
604                        if source_shape == Self::SHAPE {
605                            return Ok(unsafe { dest.copy_from(source, source_shape)? });
606                        }
607                        if source_shape == u64::SHAPE {
608                            let value: u64 = *unsafe { source.get::<u64>() };
609                            let converted: f64 = value as f64;
610                            return Ok(unsafe { dest.put::<f64>(converted) });
611                        }
612                        if source_shape == i64::SHAPE {
613                            let value: i64 = *unsafe { source.get::<i64>() };
614                            let converted: f64 = value as f64;
615                            return Ok(unsafe { dest.put::<f64>(converted) });
616                        }
617                        if source_shape == f32::SHAPE {
618                            let value: f32 = *unsafe { source.get::<f32>() };
619                            let converted: f64 = value as f64;
620                            return Ok(unsafe { dest.put::<f64>(converted) });
621                        }
622                        Err(TryFromError::Incompatible)
623                    });
624
625                    vtable
626                },
627            )
628            .build()
629    };
630}
631
632unsafe impl Facet<'_> for core::net::SocketAddr {
633    const SHAPE: &'static Shape = &const {
634        Shape::builder()
635            .id(ConstTypeId::of::<Self>())
636            .layout(Layout::new::<Self>())
637            .def(Def::Scalar(
638                ScalarDef::builder()
639                    .affinity(ScalarAffinity::socket_addr().build())
640                    .build(),
641            ))
642            .vtable(value_vtable!(core::net::SocketAddr, |f, _opts| write!(
643                f,
644                "SocketAddr"
645            )))
646            .build()
647    };
648}
649
650unsafe impl Facet<'_> for core::net::IpAddr {
651    const SHAPE: &'static Shape = &const {
652        Shape::builder()
653            .id(ConstTypeId::of::<Self>())
654            .layout(Layout::new::<Self>())
655            .def(Def::Scalar(
656                ScalarDef::builder()
657                    .affinity(ScalarAffinity::ip_addr().build())
658                    .build(),
659            ))
660            .vtable(value_vtable!(core::net::IpAddr, |f, _opts| write!(
661                f,
662                "IpAddr"
663            )))
664            .build()
665    };
666}
667
668unsafe impl Facet<'_> for core::net::Ipv4Addr {
669    const SHAPE: &'static Shape = &const {
670        Shape::builder()
671            .id(ConstTypeId::of::<Self>())
672            .layout(Layout::new::<Self>())
673            .def(Def::Scalar(
674                ScalarDef::builder()
675                    .affinity(ScalarAffinity::ip_addr().build())
676                    .build(),
677            ))
678            .vtable(value_vtable!(core::net::Ipv4Addr, |f, _opts| write!(
679                f,
680                "Ipv4Addr"
681            )))
682            .build()
683    };
684}
685
686unsafe impl Facet<'_> for core::net::Ipv6Addr {
687    const SHAPE: &'static Shape = &const {
688        Shape::builder()
689            .id(ConstTypeId::of::<Self>())
690            .layout(Layout::new::<Self>())
691            .def(Def::Scalar(
692                ScalarDef::builder()
693                    .affinity(ScalarAffinity::ip_addr().build())
694                    .build(),
695            ))
696            .vtable(value_vtable!(core::net::Ipv6Addr, |f, _opts| write!(
697                f,
698                "Ipv6Addr"
699            )))
700            .build()
701    };
702}