shapely_core/
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| 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!(i8, Scalar::I8);
30impl_shapely_for_integer!(i16, Scalar::I16);
31impl_shapely_for_integer!(i32, Scalar::I32);
32impl_shapely_for_integer!(i64, Scalar::I64);
33
34macro_rules! impl_shapely_for_float {
35    ($type:ty, $scalar:expr) => {
36        impl Shapely for $type {
37            fn shape() -> Shape {
38                Shape {
39                    name: |f| write!(f, stringify!($type)),
40                    typeid: mini_typeid::of::<Self>(),
41                    layout: Layout::new::<$type>(),
42                    innards: Innards::Scalar($scalar),
43                    set_to_default: Some(|addr: *mut u8| unsafe {
44                        *(addr as *mut $type) = 0.0;
45                    }),
46                    // floats don't need to drop
47                    drop_in_place: None,
48                }
49            }
50        }
51    };
52}
53
54impl_shapely_for_float!(f32, Scalar::F32);
55impl_shapely_for_float!(f64, Scalar::F64);
56
57impl Shapely for String {
58    fn shape() -> Shape {
59        Shape {
60            name: |f| write!(f, "String"),
61            typeid: mini_typeid::of::<Self>(),
62            layout: Layout::new::<String>(),
63            innards: Innards::Scalar(Scalar::String),
64            set_to_default: Some(|addr: *mut u8| unsafe {
65                *(addr as *mut String) = String::new();
66            }),
67            drop_in_place: Some(|addr: *mut u8| unsafe {
68                std::ptr::drop_in_place(addr as *mut String);
69            }),
70        }
71    }
72}
73
74impl Shapely for bool {
75    fn shape() -> Shape {
76        Shape {
77            name: |f| write!(f, "bool"),
78            typeid: mini_typeid::of::<Self>(),
79            layout: Layout::new::<bool>(),
80            innards: Innards::Scalar(Scalar::Boolean),
81            set_to_default: Some(|addr: *mut u8| unsafe {
82                *(addr as *mut bool) = false;
83            }),
84            // bool doesn't need to drop
85            drop_in_place: None,
86        }
87    }
88}
89
90impl Shapely for () {
91    fn shape() -> Shape {
92        Shape {
93            name: |f| write!(f, "()"),
94            typeid: mini_typeid::of::<Self>(),
95            layout: Layout::new::<()>(),
96            innards: Innards::Scalar(Scalar::Nothing),
97            set_to_default: Some(|_addr: *mut u8| {}),
98            drop_in_place: None,
99        }
100    }
101}