shapely_core/impls/
scalar_impls.rs

1use std::alloc::Layout;
2
3use crate::*;
4
5macro_rules! impl_shapely_for_integer {
6    ($type:ty, $scalar:expr) => {
7        impl Shapely for $type {
8            fn shape() -> Shape {
9                Shape {
10                    name: |f, _nameopts| write!(f, stringify!($type)),
11                    typeid: mini_typeid::of::<Self>(),
12                    layout: Layout::new::<$type>(),
13                    innards: Innards::Scalar($scalar),
14                    set_to_default: Some(|addr: *mut u8| unsafe {
15                        *(addr as *mut $type) = 0;
16                    }),
17                    // integers don't need to drop
18                    drop_in_place: None,
19                }
20            }
21        }
22    };
23}
24
25impl_shapely_for_integer!(u8, Scalar::U8);
26impl_shapely_for_integer!(u16, Scalar::U16);
27impl_shapely_for_integer!(u32, Scalar::U32);
28impl_shapely_for_integer!(u64, Scalar::U64);
29impl_shapely_for_integer!(u128, Scalar::U128);
30impl_shapely_for_integer!(i8, Scalar::I8);
31impl_shapely_for_integer!(i16, Scalar::I16);
32impl_shapely_for_integer!(i32, Scalar::I32);
33impl_shapely_for_integer!(i64, Scalar::I64);
34impl_shapely_for_integer!(i128, Scalar::I128);
35
36macro_rules! impl_shapely_for_float {
37    ($type:ty, $scalar:expr) => {
38        impl Shapely for $type {
39            fn shape() -> Shape {
40                Shape {
41                    name: |f, _nameopts| write!(f, stringify!($type)),
42                    typeid: mini_typeid::of::<Self>(),
43                    layout: Layout::new::<$type>(),
44                    innards: Innards::Scalar($scalar),
45                    set_to_default: Some(|addr: *mut u8| unsafe {
46                        *(addr as *mut $type) = 0.0;
47                    }),
48                    // floats don't need to drop
49                    drop_in_place: None,
50                }
51            }
52        }
53    };
54}
55
56impl_shapely_for_float!(f32, Scalar::F32);
57impl_shapely_for_float!(f64, Scalar::F64);
58
59impl Shapely for String {
60    fn shape() -> Shape {
61        Shape {
62            name: |f, _nameopts| write!(f, "String"),
63            typeid: mini_typeid::of::<Self>(),
64            layout: Layout::new::<String>(),
65            innards: Innards::Scalar(Scalar::String),
66            set_to_default: Some(|addr: *mut u8| unsafe {
67                *(addr as *mut String) = String::new();
68            }),
69            drop_in_place: Some(|addr: *mut u8| unsafe {
70                std::ptr::drop_in_place(addr as *mut String);
71            }),
72        }
73    }
74}
75
76impl Shapely for bool {
77    fn shape() -> Shape {
78        Shape {
79            name: |f, _nameopts| write!(f, "bool"),
80            typeid: mini_typeid::of::<Self>(),
81            layout: Layout::new::<bool>(),
82            innards: Innards::Scalar(Scalar::Boolean),
83            set_to_default: Some(|addr: *mut u8| unsafe {
84                *(addr as *mut bool) = false;
85            }),
86            // bool doesn't need to drop
87            drop_in_place: None,
88        }
89    }
90}
91
92impl Shapely for () {
93    fn shape() -> Shape {
94        Shape {
95            name: |f, _nameopts| write!(f, "()"),
96            typeid: mini_typeid::of::<Self>(),
97            layout: Layout::new::<()>(),
98            innards: Innards::Scalar(Scalar::Nothing),
99            set_to_default: None,
100            drop_in_place: None,
101        }
102    }
103}