reifydb_codec/encoded/
boolean.rs1use std::ptr;
5
6use reifydb_value::{reifydb_assertions, value::value_type::ValueType};
7
8use crate::encoded::{row::EncodedRow, shape::RowShape};
9
10impl RowShape {
11 pub fn set_bool(&self, row: &mut EncodedRow, index: usize, value: impl Into<bool>) {
12 let field = &self.fields()[index];
13 reifydb_assertions! {
14 assert!(
15 row.len() >= self.total_static_size(),
16 "row/shape size mismatch: row.len()={} < total_static_size()={}",
17 row.len(),
18 self.total_static_size()
19 );
20 assert_eq!(*field.constraint.get_type().inner_type(), ValueType::Boolean);
21 }
22 row.set_valid(index, true);
23 unsafe {
24 ptr::write_unaligned(
25 row.make_mut().as_mut_ptr().add(field.offset as usize) as *mut bool,
26 value.into(),
27 )
28 }
29 }
30
31 pub fn get_bool(&self, row: &EncodedRow, index: usize) -> bool {
32 let field = &self.fields()[index];
33 reifydb_assertions! {
34 assert!(
35 row.len() >= self.total_static_size(),
36 "row/shape size mismatch: row.len()={} < total_static_size()={}",
37 row.len(),
38 self.total_static_size()
39 );
40 assert_eq!(*field.constraint.get_type().inner_type(), ValueType::Boolean);
41 }
42 unsafe { (row.as_ptr().add(field.offset as usize) as *const bool).read_unaligned() }
43 }
44
45 pub fn try_get_bool(&self, row: &EncodedRow, index: usize) -> Option<bool> {
46 if row.is_defined(index) && self.fields()[index].constraint.get_type() == ValueType::Boolean {
47 Some(self.get_bool(row, index))
48 } else {
49 None
50 }
51 }
52}
53
54#[cfg(test)]
55pub mod tests {
56 use reifydb_value::value::value_type::ValueType;
57
58 use crate::encoded::shape::RowShape;
59
60 #[test]
61 fn test_set_get_bool() {
62 let shape = RowShape::testing(&[ValueType::Boolean]);
63 let mut row = shape.allocate();
64 shape.set_bool(&mut row, 0, true);
65 assert!(shape.get_bool(&row, 0));
66 }
67
68 #[test]
69 fn test_try_get_bool() {
70 let shape = RowShape::testing(&[ValueType::Boolean]);
71 let mut row = shape.allocate();
72
73 assert_eq!(shape.try_get_bool(&row, 0), None);
74
75 shape.set_bool(&mut row, 0, true);
76 assert_eq!(shape.try_get_bool(&row, 0), Some(true));
77 }
78
79 #[test]
80 fn test_false() {
81 let shape = RowShape::testing(&[ValueType::Boolean]);
82 let mut row = shape.allocate();
83 shape.set_bool(&mut row, 0, false);
84 assert!(!shape.get_bool(&row, 0));
85 assert_eq!(shape.try_get_bool(&row, 0), Some(false));
86 }
87
88 #[test]
89 fn test_mixed_with_other_types() {
90 let shape = RowShape::testing(&[ValueType::Boolean, ValueType::Int4, ValueType::Boolean]);
91 let mut row = shape.allocate();
92
93 shape.set_bool(&mut row, 0, true);
94 shape.set_i32(&mut row, 1, 42);
95 shape.set_bool(&mut row, 2, false);
96
97 assert_eq!(shape.get_bool(&row, 0), true);
98 assert_eq!(shape.get_i32(&row, 1), 42);
99 assert_eq!(shape.get_bool(&row, 2), false);
100 }
101
102 #[test]
103 fn test_undefined_handling() {
104 let shape = RowShape::testing(&[ValueType::Boolean, ValueType::Boolean]);
105 let mut row = shape.allocate();
106
107 shape.set_bool(&mut row, 0, true);
108
109 assert_eq!(shape.try_get_bool(&row, 0), Some(true));
110 assert_eq!(shape.try_get_bool(&row, 1), None);
111
112 shape.set_none(&mut row, 0);
113 assert_eq!(shape.try_get_bool(&row, 0), None);
114 }
115
116 #[test]
117 fn test_try_get_bool_wrong_type() {
118 let shape = RowShape::testing(&[ValueType::Int1]);
119 let mut row = shape.allocate();
120
121 shape.set_i8(&mut row, 0, 42);
122
123 assert_eq!(shape.try_get_bool(&row, 0), None);
124 }
125}