1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
use crate::{
    builder::Builder, errors::ErrorKind, slice_helpers::SliceWithStartOffset, traits::*, Cursor,
};
use core::mem::MaybeUninit;

macro_rules! gen_primitive_types {
    ($ty:ty, $size:expr) => {
        impl Primitive for $ty {
            const ALIGNMENT: usize = $size;
            const SIZE: usize = $size;
        }

        impl WriteAsPrimitive<$ty> for $ty {
            #[inline]
            fn write<const N: usize>(&self, cursor: Cursor<'_, N>, _buffer_position: u32) {
                cursor.assert_size().finish(self.to_le_bytes());
            }
        }

        impl WriteAs<$ty> for $ty {
            type Prepared = Self;
            #[inline]
            fn prepare(&self, _builder: &mut Builder) -> Self {
                *self
            }
        }

        impl WriteAsDefault<$ty, $ty> for $ty {
            type Prepared = Self;
            #[inline]
            fn prepare(&self, _builder: &mut Builder, default: &$ty) -> Option<Self> {
                #[allow(clippy::float_cmp)]
                if self == default {
                    None
                } else {
                    Some(*self)
                }
            }
        }

        impl WriteAsOptional<$ty> for $ty {
            type Prepared = Self;
            #[inline]
            fn prepare(&self, _builder: &mut Builder) -> Option<Self> {
                Some(*self)
            }
        }

        impl<'buf> TableRead<'buf> for $ty {
            #[inline]
            fn from_buffer(
                buffer: SliceWithStartOffset<'buf>,
                offset: usize,
            ) -> core::result::Result<$ty, ErrorKind> {
                let buffer = buffer.advance_as_array(offset)?.as_array();
                Ok(<$ty>::from_le_bytes(*buffer))
            }
        }

        impl<'buf> VectorRead<'buf> for $ty {
            const STRIDE: usize = $size;
            #[inline]
            unsafe fn from_buffer(buffer: SliceWithStartOffset<'buf>, offset: usize) -> $ty {
                let buffer = buffer.unchecked_advance_as_array(offset).as_array();
                <$ty>::from_le_bytes(*buffer)
            }
        }

        impl VectorWrite<$ty> for $ty {
            const STRIDE: usize = $size;
            type Value = $ty;
            #[inline]
            fn prepare(&self, _builder: &mut Builder) -> Self::Value {
                *self
            }

            #[inline]
            unsafe fn write_values(
                values: &[$ty],
                bytes: *mut MaybeUninit<u8>,
                buffer_position: u32,
            ) {
                let bytes = bytes as *mut [MaybeUninit<u8>; $size];
                for (i, v) in values.iter().enumerate() {
                    v.write(
                        Cursor::new(&mut *bytes.add(i)),
                        buffer_position - ($size * i) as u32,
                    );
                }
            }
        }
    };
}

gen_primitive_types!(i8, 1);
gen_primitive_types!(u8, 1);
gen_primitive_types!(i16, 2);
gen_primitive_types!(u16, 2);
gen_primitive_types!(i32, 4);
gen_primitive_types!(u32, 4);
gen_primitive_types!(i64, 8);
gen_primitive_types!(u64, 8);
gen_primitive_types!(f32, 4);
gen_primitive_types!(f64, 8);