vecdb/variants/raw/zerocopy/
strategy.rs

1use std::marker::PhantomData;
2
3use crate::{Error, Result, ZeroCopyVecValue, variants::raw::RawStrategy};
4
5/// Serialization strategy using zerocopy for native byte order access.
6///
7/// Uses direct memory mapping in native byte order - not portable across endianness.
8#[derive(Debug, Clone, Copy)]
9pub struct ZeroCopyStrategy<T>(PhantomData<T>);
10
11impl<T: ZeroCopyVecValue> RawStrategy<T> for ZeroCopyStrategy<T> {
12    #[inline(always)]
13    fn read(bytes: &[u8]) -> Result<T> {
14        T::read_from_prefix(bytes)
15            .map(|(v, _)| v)
16            .map_err(|_| Error::ZeroCopyError)
17    }
18
19    #[inline(always)]
20    fn write_to_vec(value: &T, buf: &mut Vec<u8>) {
21        buf.extend_from_slice(value.as_bytes());
22    }
23
24    #[inline(always)]
25    fn write_to_slice(value: &T, dst: &mut [u8]) {
26        dst.copy_from_slice(value.as_bytes());
27    }
28}