pub struct PtrOffsetSerializer<const STORAGE_ALIGNMENT: usize, const VALUE_ALIGNMENT: usize, const MAX_VALUE_ALIGNMENT: usize, const MAX_CAPACITY: usize, BorrowedStorage: BorrowMut<AlignedVec<STORAGE_ALIGNMENT, VALUE_ALIGNMENT, MAX_VALUE_ALIGNMENT, MAX_CAPACITY>>> { /* private fields */ }Expand description
Serializer that overwrites pointers in output with position offsets, relative to the start of the output buffer.
Unlike PureCopySerializer, this allows a deserializer to walk through the
serializer output in any order.
Values in output will be correctly aligned for their types.
See AlignedStorage for an explanation of the const parameters.
§Example
use ser_raw::{
PtrOffsetSerializer, Serialize, Serializer,
storage::ContiguousStorage
};
let boxed: Box<u8> = Box::new(123);
let mut ser = PtrOffsetSerializer::<16, 8, 16, 1024, _>::new();
let storage = ser.serialize(&boxed);
let slice = storage.as_slice();
const PTR_SIZE: usize = std::mem::size_of::<usize>();
let offset = usize::from_ne_bytes(slice[..PTR_SIZE].try_into().unwrap());
assert_eq!(offset, 8);
assert_eq!(slice[offset], 123);Implementations§
Source§impl<const SA: usize, const VA: usize, const MVA: usize, const MAX: usize> PtrOffsetSerializer<SA, VA, MVA, MAX, AlignedVec<SA, VA, MVA, MAX>>
impl<const SA: usize, const VA: usize, const MVA: usize, const MAX: usize> PtrOffsetSerializer<SA, VA, MVA, MAX, AlignedVec<SA, VA, MVA, MAX>>
Sourcepub fn new() -> Self
pub fn new() -> Self
Create new PtrOffsetSerializer with no memory pre-allocated.
If you know, or can estimate, the amount of buffer space that’s going to
be needed in advance, allocating upfront with with_capacity can
dramatically improve performance vs using new.
Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Create new PtrOffsetSerializer with buffer pre-allocated with
capacity of at least capacity bytes.
capacity will be rounded up to a multiple of MAX_VALUE_ALIGNMENT.
§Panics
Panics if capacity exceeds MAX_CAPACITY.
Source§impl<const SA: usize, const VA: usize, const MVA: usize, const MAX: usize, BorrowedStorage> PtrOffsetSerializer<SA, VA, MVA, MAX, BorrowedStorage>where
BorrowedStorage: BorrowMut<AlignedVec<SA, VA, MVA, MAX>>,
impl<const SA: usize, const VA: usize, const MVA: usize, const MAX: usize, BorrowedStorage> PtrOffsetSerializer<SA, VA, MVA, MAX, BorrowedStorage>where
BorrowedStorage: BorrowMut<AlignedVec<SA, VA, MVA, MAX>>,
Sourcepub const STORAGE_ALIGNMENT: usize = SA
pub const STORAGE_ALIGNMENT: usize = SA
Alignment of output buffer
Sourcepub const VALUE_ALIGNMENT: usize = VA
pub const VALUE_ALIGNMENT: usize = VA
Typical alignment of values being serialized
Sourcepub const MAX_VALUE_ALIGNMENT: usize = MVA
pub const MAX_VALUE_ALIGNMENT: usize = MVA
Maximum alignment of values being serialized
Sourcepub const MAX_CAPACITY: usize = MAX
pub const MAX_CAPACITY: usize = MAX
Maximum capacity of output buffer.
Sourcepub fn from_storage(storage: BorrowedStorage) -> Self
pub fn from_storage(storage: BorrowedStorage) -> Self
Create new PtrOffsetSerializer from an existing
BorrowMut<AlignedVec>.
Trait Implementations§
Source§impl<const STORAGE_ALIGNMENT: usize, const VALUE_ALIGNMENT: usize, const MAX_VALUE_ALIGNMENT: usize, const MAX_CAPACITY: usize, BorrowedStorage: BorrowMut<AlignedVec<STORAGE_ALIGNMENT, VALUE_ALIGNMENT, MAX_VALUE_ALIGNMENT, MAX_CAPACITY>>> PosTracking for PtrOffsetSerializer<STORAGE_ALIGNMENT, VALUE_ALIGNMENT, MAX_VALUE_ALIGNMENT, MAX_CAPACITY, BorrowedStorage>
impl<const STORAGE_ALIGNMENT: usize, const VALUE_ALIGNMENT: usize, const MAX_VALUE_ALIGNMENT: usize, const MAX_CAPACITY: usize, BorrowedStorage: BorrowMut<AlignedVec<STORAGE_ALIGNMENT, VALUE_ALIGNMENT, MAX_VALUE_ALIGNMENT, MAX_CAPACITY>>> PosTracking for PtrOffsetSerializer<STORAGE_ALIGNMENT, VALUE_ALIGNMENT, MAX_VALUE_ALIGNMENT, MAX_CAPACITY, BorrowedStorage>
Source§fn pos_mapping(&self) -> &PosMapping
fn pos_mapping(&self) -> &PosMapping
Get current position mapping
Source§fn set_pos_mapping(&mut self, pos_mapping: PosMapping)
fn set_pos_mapping(&mut self, pos_mapping: PosMapping)
Set current position mapping
fn do_serialize_value<T: Serialize<Self>>(&mut self, value: &T)
fn do_push_slice<T>(&mut self, slice: &[T], _ptr_addr: Self::Addr)
fn do_push_and_process_slice<T, P: FnOnce(&mut Self)>( &mut self, slice: &[T], _ptr_addr: Self::Addr, process: P, )
Source§impl<const STORAGE_ALIGNMENT: usize, const VALUE_ALIGNMENT: usize, const MAX_VALUE_ALIGNMENT: usize, const MAX_CAPACITY: usize, BorrowedStorage: BorrowMut<AlignedVec<STORAGE_ALIGNMENT, VALUE_ALIGNMENT, MAX_VALUE_ALIGNMENT, MAX_CAPACITY>>> PtrOffset for PtrOffsetSerializer<STORAGE_ALIGNMENT, VALUE_ALIGNMENT, MAX_VALUE_ALIGNMENT, MAX_CAPACITY, BorrowedStorage>
impl<const STORAGE_ALIGNMENT: usize, const VALUE_ALIGNMENT: usize, const MAX_VALUE_ALIGNMENT: usize, const MAX_CAPACITY: usize, BorrowedStorage: BorrowMut<AlignedVec<STORAGE_ALIGNMENT, VALUE_ALIGNMENT, MAX_VALUE_ALIGNMENT, MAX_CAPACITY>>> PtrOffset for PtrOffsetSerializer<STORAGE_ALIGNMENT, VALUE_ALIGNMENT, MAX_VALUE_ALIGNMENT, MAX_CAPACITY, BorrowedStorage>
Source§impl<const STORAGE_ALIGNMENT: usize, const VALUE_ALIGNMENT: usize, const MAX_VALUE_ALIGNMENT: usize, const MAX_CAPACITY: usize, BorrowedStorage: BorrowMut<AlignedVec<STORAGE_ALIGNMENT, VALUE_ALIGNMENT, MAX_VALUE_ALIGNMENT, MAX_CAPACITY>>> PtrWriting for PtrOffsetSerializer<STORAGE_ALIGNMENT, VALUE_ALIGNMENT, MAX_VALUE_ALIGNMENT, MAX_CAPACITY, BorrowedStorage>
impl<const STORAGE_ALIGNMENT: usize, const VALUE_ALIGNMENT: usize, const MAX_VALUE_ALIGNMENT: usize, const MAX_CAPACITY: usize, BorrowedStorage: BorrowMut<AlignedVec<STORAGE_ALIGNMENT, VALUE_ALIGNMENT, MAX_VALUE_ALIGNMENT, MAX_CAPACITY>>> PtrWriting for PtrOffsetSerializer<STORAGE_ALIGNMENT, VALUE_ALIGNMENT, MAX_VALUE_ALIGNMENT, MAX_CAPACITY, BorrowedStorage>
Source§unsafe fn write_ptr(&mut self, ptr_pos: usize, target_pos: usize)
unsafe fn write_ptr(&mut self, ptr_pos: usize, target_pos: usize)
Overwrite pointer.
§Safety
ptr_posandtarget_posmust both sit within bounds of output.target_posmust be location of a valid value for the type being pointed to.ptr_posmust be aligned for a pointer.
fn do_push_slice<T>(&mut self, slice: &[T], ptr_addr: Self::Addr)
fn do_push_and_process_slice<T, P: FnOnce(&mut Self)>( &mut self, slice: &[T], ptr_addr: Self::Addr, process: P, )
Source§impl<const STORAGE_ALIGNMENT: usize, const VALUE_ALIGNMENT: usize, const MAX_VALUE_ALIGNMENT: usize, const MAX_CAPACITY: usize, BorrowedStorage: BorrowMut<AlignedVec<STORAGE_ALIGNMENT, VALUE_ALIGNMENT, MAX_VALUE_ALIGNMENT, MAX_CAPACITY>>> Serializer for PtrOffsetSerializer<STORAGE_ALIGNMENT, VALUE_ALIGNMENT, MAX_VALUE_ALIGNMENT, MAX_CAPACITY, BorrowedStorage>
impl<const STORAGE_ALIGNMENT: usize, const VALUE_ALIGNMENT: usize, const MAX_VALUE_ALIGNMENT: usize, const MAX_CAPACITY: usize, BorrowedStorage: BorrowMut<AlignedVec<STORAGE_ALIGNMENT, VALUE_ALIGNMENT, MAX_VALUE_ALIGNMENT, MAX_CAPACITY>>> Serializer for PtrOffsetSerializer<STORAGE_ALIGNMENT, VALUE_ALIGNMENT, MAX_VALUE_ALIGNMENT, MAX_CAPACITY, BorrowedStorage>
Source§type Storage = AlignedVec<STORAGE_ALIGNMENT, VALUE_ALIGNMENT, MAX_VALUE_ALIGNMENT, MAX_CAPACITY>
type Storage = AlignedVec<STORAGE_ALIGNMENT, VALUE_ALIGNMENT, MAX_VALUE_ALIGNMENT, MAX_CAPACITY>
Storage which backs this serializer.Source§type BorrowedStorage = BorrowedStorage
type BorrowedStorage = BorrowedStorage
Source§type Addr = TrackingAddr
type Addr = TrackingAddr
Addr type this serializer uses.Source§fn serialize_value<T: Serialize<Self>>(&mut self, value: &T)
fn serialize_value<T: Serialize<Self>>(&mut self, value: &T)
Source§fn push_slice<T>(&mut self, slice: &[T], ptr_addr: Self::Addr)
fn push_slice<T>(&mut self, slice: &[T], ptr_addr: Self::Addr)
Source§fn push_and_process_slice<T, P: FnOnce(&mut Self)>(
&mut self,
slice: &[T],
ptr_addr: Self::Addr,
process: P,
)
fn push_and_process_slice<T, P: FnOnce(&mut Self)>( &mut self, slice: &[T], ptr_addr: Self::Addr, process: P, )
Source§fn storage(
&self,
) -> &AlignedVec<STORAGE_ALIGNMENT, VALUE_ALIGNMENT, MAX_VALUE_ALIGNMENT, MAX_CAPACITY>
fn storage( &self, ) -> &AlignedVec<STORAGE_ALIGNMENT, VALUE_ALIGNMENT, MAX_VALUE_ALIGNMENT, MAX_CAPACITY>
Storage backing this Serializer.Source§fn storage_mut(
&mut self,
) -> &mut AlignedVec<STORAGE_ALIGNMENT, VALUE_ALIGNMENT, MAX_VALUE_ALIGNMENT, MAX_CAPACITY>
fn storage_mut( &mut self, ) -> &mut AlignedVec<STORAGE_ALIGNMENT, VALUE_ALIGNMENT, MAX_VALUE_ALIGNMENT, MAX_CAPACITY>
Storage backing this Serializer.Source§fn into_storage(self) -> BorrowedStorage
fn into_storage(self) -> BorrowedStorage
BorrowMut<Storage>. Read moreSource§fn serialize<T: Serialize<Self>>(self, value: &T) -> Self::BorrowedStorage
fn serialize<T: Serialize<Self>>(self, value: &T) -> Self::BorrowedStorage
Source§fn push_and_process<T, P: FnOnce(&mut Self)>(
&mut self,
t: &T,
ptr_addr: Self::Addr,
process: P,
)
fn push_and_process<T, P: FnOnce(&mut Self)>( &mut self, t: &T, ptr_addr: Self::Addr, process: P, )
Source§fn push_raw_slice<T>(&mut self, slice: &[T])
fn push_raw_slice<T>(&mut self, slice: &[T])
Source§fn push_raw_bytes(&mut self, bytes: &[u8])
fn push_raw_bytes(&mut self, bytes: &[u8])
Source§unsafe fn write<T>(&mut self, value: &T, addr: usize)
unsafe fn write<T>(&mut self, value: &T, addr: usize)
Source§fn write_correction<W: FnOnce(&mut Self)>(&mut self, write: W)
fn write_correction<W: FnOnce(&mut Self)>(&mut self, write: W)
Source§fn finalize(self) -> Self::BorrowedStorage
fn finalize(self) -> Self::BorrowedStorage
BorrowMut<Storage>.