Skip to main content

reifydb_codec/primitive/
any.rs

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