Skip to main content

reifydb_core/encoded/
i8.rs

1// SPDX-License-Identifier: AGPL-3.0-or-later
2// Copyright (c) 2025 ReifyDB
3
4use std::ptr;
5
6use reifydb_type::value::r#type::Type;
7
8use crate::encoded::{encoded::EncodedValues, schema::Schema};
9
10impl Schema {
11	pub fn set_i8(&self, row: &mut EncodedValues, index: usize, value: impl Into<i8>) {
12		let field = &self.fields()[index];
13		debug_assert!(row.len() >= self.total_static_size());
14		debug_assert_eq!(*field.constraint.get_type().inner_type(), Type::Int1);
15		row.set_valid(index, true);
16		unsafe {
17			ptr::write_unaligned(
18				row.make_mut().as_mut_ptr().add(field.offset as usize) as *mut i8,
19				value.into(),
20			)
21		}
22	}
23
24	pub fn get_i8(&self, row: &EncodedValues, index: usize) -> i8 {
25		let field = &self.fields()[index];
26		debug_assert!(row.len() >= self.total_static_size());
27		debug_assert_eq!(*field.constraint.get_type().inner_type(), Type::Int1);
28		unsafe { (row.as_ptr().add(field.offset as usize) as *const i8).read_unaligned() }
29	}
30
31	pub fn try_get_i8(&self, row: &EncodedValues, index: usize) -> Option<i8> {
32		if row.is_defined(index) && self.fields()[index].constraint.get_type() == Type::Int1 {
33			Some(self.get_i8(row, index))
34		} else {
35			None
36		}
37	}
38}
39
40#[cfg(test)]
41pub mod tests {
42	use reifydb_type::value::r#type::Type;
43
44	use crate::encoded::schema::Schema;
45
46	#[test]
47	fn test_set_get_i8() {
48		let schema = Schema::testing(&[Type::Int1]);
49		let mut row = schema.allocate();
50		schema.set_i8(&mut row, 0, 42i8);
51		assert_eq!(schema.get_i8(&row, 0), 42i8);
52	}
53
54	#[test]
55	fn test_try_get_i8() {
56		let schema = Schema::testing(&[Type::Int1]);
57		let mut row = schema.allocate();
58
59		assert_eq!(schema.try_get_i8(&row, 0), None);
60
61		schema.set_i8(&mut row, 0, 42i8);
62		assert_eq!(schema.try_get_i8(&row, 0), Some(42i8));
63	}
64
65	#[test]
66	fn test_extremes() {
67		let schema = Schema::testing(&[Type::Int1]);
68		let mut row = schema.allocate();
69
70		schema.set_i8(&mut row, 0, i8::MAX);
71		assert_eq!(schema.get_i8(&row, 0), i8::MAX);
72
73		let mut row2 = schema.allocate();
74		schema.set_i8(&mut row2, 0, i8::MIN);
75		assert_eq!(schema.get_i8(&row2, 0), i8::MIN);
76
77		let mut row3 = schema.allocate();
78		schema.set_i8(&mut row3, 0, 0i8);
79		assert_eq!(schema.get_i8(&row3, 0), 0i8);
80	}
81
82	#[test]
83	fn test_negative_positive() {
84		let schema = Schema::testing(&[Type::Int1, Type::Int1]);
85		let mut row = schema.allocate();
86
87		schema.set_i8(&mut row, 0, -100i8);
88		schema.set_i8(&mut row, 1, 100i8);
89
90		assert_eq!(schema.get_i8(&row, 0), -100i8);
91		assert_eq!(schema.get_i8(&row, 1), 100i8);
92	}
93
94	#[test]
95	fn test_mixed_with_other_types() {
96		let schema = Schema::testing(&[Type::Int1, Type::Boolean, Type::Int1]);
97		let mut row = schema.allocate();
98
99		schema.set_i8(&mut row, 0, -50i8);
100		schema.set_bool(&mut row, 1, true);
101		schema.set_i8(&mut row, 2, 75i8);
102
103		assert_eq!(schema.get_i8(&row, 0), -50i8);
104		assert_eq!(schema.get_bool(&row, 1), true);
105		assert_eq!(schema.get_i8(&row, 2), 75i8);
106	}
107
108	#[test]
109	fn test_undefined_handling() {
110		let schema = Schema::testing(&[Type::Int1, Type::Int1]);
111		let mut row = schema.allocate();
112
113		schema.set_i8(&mut row, 0, 42);
114
115		assert_eq!(schema.try_get_i8(&row, 0), Some(42));
116		assert_eq!(schema.try_get_i8(&row, 1), None);
117
118		schema.set_none(&mut row, 0);
119		assert_eq!(schema.try_get_i8(&row, 0), None);
120	}
121
122	#[test]
123	fn test_try_get_i8_wrong_type() {
124		let schema = Schema::testing(&[Type::Boolean]);
125		let mut row = schema.allocate();
126
127		schema.set_bool(&mut row, 0, true);
128
129		assert_eq!(schema.try_get_i8(&row, 0), None);
130	}
131}