planus/impls/
bool_.rs

1use crate::{
2    builder::Builder, errors::ErrorKind, slice_helpers::SliceWithStartOffset, traits::*, Cursor,
3};
4use core::mem::MaybeUninit;
5
6impl Primitive for bool {
7    const ALIGNMENT: usize = 1;
8    const SIZE: usize = 1;
9}
10
11impl WriteAsPrimitive<bool> for bool {
12    #[inline]
13    fn write<const N: usize>(&self, cursor: Cursor<'_, N>, _buffer_position: u32) {
14        cursor.assert_size().finish([if *self { 1 } else { 0 }]);
15    }
16}
17
18impl WriteAs<bool> for bool {
19    type Prepared = Self;
20    #[inline]
21    fn prepare(&self, _builder: &mut Builder) -> Self {
22        *self
23    }
24}
25
26impl WriteAsDefault<bool, bool> for bool {
27    type Prepared = Self;
28    #[inline]
29    fn prepare(&self, _builder: &mut Builder, default: &bool) -> Option<bool> {
30        if self == default {
31            None
32        } else {
33            Some(*self)
34        }
35    }
36}
37
38impl WriteAsOptional<bool> for bool {
39    type Prepared = Self;
40    #[inline]
41    fn prepare(&self, _builder: &mut Builder) -> Option<Self> {
42        Some(*self)
43    }
44}
45
46impl<'buf> TableRead<'buf> for bool {
47    #[inline]
48    fn from_buffer(
49        buffer: SliceWithStartOffset<'buf>,
50        offset: usize,
51    ) -> core::result::Result<bool, ErrorKind> {
52        Ok(buffer.advance_as_array::<1>(offset)?.as_array()[0] != 0)
53    }
54}
55
56impl<'buf> VectorRead<'buf> for bool {
57    const STRIDE: usize = 1;
58
59    #[inline]
60    unsafe fn from_buffer(buffer: SliceWithStartOffset<'buf>, offset: usize) -> bool {
61        *buffer.as_slice().get_unchecked(offset) != 0
62    }
63}
64
65impl VectorWrite<bool> for bool {
66    const STRIDE: usize = 1;
67
68    type Value = bool;
69
70    #[inline]
71    fn prepare(&self, _builder: &mut Builder) -> Self::Value {
72        *self
73    }
74
75    #[inline]
76    unsafe fn write_values(
77        values: &[Self::Value],
78        bytes: *mut MaybeUninit<u8>,
79        buffer_position: u32,
80    ) {
81        let bytes = bytes as *mut [MaybeUninit<u8>; 1];
82        for (i, v) in values.iter().enumerate() {
83            v.write(Cursor::new(&mut *bytes.add(i)), buffer_position - i as u32);
84        }
85    }
86}