Skip to main content

reifydb_codec/row/pod/
mod.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2026 ReifyDB
3
4use std::ops::Deref;
5
6use reifydb_value::{byte_size::ByteSize, util::cowvec::CowVec};
7
8use crate::row::bytes::{EncodedBytes, EncodedRowBuilder, RowBuilder, read_defined_at, sealed::Sealed};
9
10pub const POD_HEADER_SIZE: usize = 0;
11
12#[repr(transparent)]
13#[derive(Debug, Clone, PartialEq, Eq)]
14pub struct EncodedPodRow(EncodedBytes);
15
16impl EncodedPodRow {
17	pub fn new(body: &[u8]) -> Self {
18		Self(EncodedBytes(CowVec::new(body.to_vec())))
19	}
20
21	pub fn view(bytes: &EncodedBytes) -> &Self {
22		// SAFETY: EncodedPodRow is repr(transparent) over EncodedBytes, so the pointer cast
23		// preserves layout, and the returned reference borrows the same allocation for the same lifetime.
24		unsafe { &*(bytes as *const EncodedBytes as *const Self) }
25	}
26
27	pub fn bytes(&self) -> &EncodedBytes {
28		&self.0
29	}
30
31	pub fn as_slice(&self) -> &[u8] {
32		self.0.as_slice()
33	}
34
35	pub fn into_bytes(self) -> EncodedBytes {
36		self.0
37	}
38
39	pub fn body(&self) -> &[u8] {
40		&self.0[POD_HEADER_SIZE..]
41	}
42
43	pub fn body_mut(&mut self) -> &mut [u8] {
44		&mut self.0.make_mut()[POD_HEADER_SIZE..]
45	}
46
47	pub fn len(&self) -> usize {
48		self.0.len()
49	}
50
51	pub fn is_empty(&self) -> bool {
52		self.body().is_empty()
53	}
54
55	pub fn byte_size(&self) -> ByteSize {
56		ByteSize::from(self.0.len() as u64)
57	}
58}
59
60impl From<EncodedBytes> for EncodedPodRow {
61	fn from(bytes: EncodedBytes) -> Self {
62		Self(bytes)
63	}
64}
65
66impl From<EncodedPodRow> for EncodedBytes {
67	fn from(row: EncodedPodRow) -> Self {
68		row.0
69	}
70}
71
72#[repr(transparent)]
73#[derive(Debug, Clone, PartialEq, Eq)]
74pub struct EncodedPodRowBuilder(EncodedRowBuilder);
75
76impl EncodedPodRowBuilder {
77	pub(crate) fn wrap(builder: EncodedRowBuilder) -> Self {
78		Self(builder)
79	}
80
81	#[inline]
82	pub fn is_defined(&self, index: usize) -> bool {
83		read_defined_at(self.as_slice(), POD_HEADER_SIZE, index)
84	}
85
86	pub fn body(&self) -> &[u8] {
87		&self.as_slice()[POD_HEADER_SIZE..]
88	}
89
90	pub fn freeze(self) -> EncodedPodRow {
91		EncodedPodRow(self.0.freeze())
92	}
93}
94
95impl Sealed for EncodedPodRowBuilder {
96	fn buffer(&self) -> &Vec<u8> {
97		self.0.buffer()
98	}
99
100	fn buffer_mut(&mut self) -> &mut Vec<u8> {
101		self.0.buffer_mut()
102	}
103
104	fn take_buffer(self) -> Vec<u8> {
105		self.0.take_buffer()
106	}
107}
108
109impl EncodedPodRow {
110	pub fn thaw(self) -> EncodedPodRowBuilder {
111		EncodedPodRowBuilder(self.0.thaw())
112	}
113}
114
115impl Deref for EncodedPodRowBuilder {
116	type Target = [u8];
117
118	fn deref(&self) -> &Self::Target {
119		self.as_slice()
120	}
121}