Skip to main content

reifydb_codec/primitive/
any.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2026 ReifyDB
3
4#[cfg(reifydb_assertions)]
5use reifydb_value::value::value_type::ValueType;
6use reifydb_value::{reifydb_assertions, value::Value};
7
8use crate::{
9	row::{bytes::RowBuilder, shape::RowShape},
10	value::{decode_value, encode_value},
11};
12
13impl RowShape {
14	pub fn set_any(&self, row: &mut impl RowBuilder, index: usize, value: &Value) {
15		reifydb_assertions! {
16			assert!(
17				row.len() >= self.total_static_size(),
18				"row/shape size mismatch: row.len()={} < total_static_size()={}",
19				row.len(),
20				self.total_static_size()
21			);
22			assert_eq!(*self.fields()[index].constraint.get_type().inner_type(), ValueType::Any);
23		}
24		let encoded = encode_value(value).expect("unsupported value in any row field");
25		self.replace_dynamic_data(row, index, &encoded);
26	}
27
28	pub fn get_any(&self, row: &[u8], index: usize) -> Value {
29		let field = &self.fields()[index];
30		reifydb_assertions! {
31			assert!(
32				row.len() >= self.total_static_size(),
33				"row/shape size mismatch: row.len()={} < total_static_size()={}",
34				row.len(),
35				self.total_static_size()
36			);
37			assert_eq!(*field.constraint.get_type().inner_type(), ValueType::Any);
38		}
39
40		let ref_slice = &row[field.offset as usize..field.offset as usize + 8];
41		let offset = u32::from_le_bytes([ref_slice[0], ref_slice[1], ref_slice[2], ref_slice[3]]) as usize;
42		let length = u32::from_le_bytes([ref_slice[4], ref_slice[5], ref_slice[6], ref_slice[7]]) as usize;
43
44		let dynamic_start = self.dynamic_section_start();
45		let data_start = dynamic_start + offset;
46		let data_slice = &row[data_start..data_start + length];
47
48		decode_value(data_slice).expect("corrupt any row field bytes")
49	}
50}