PtrOffsetSerializer

Struct PtrOffsetSerializer 

Source
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>>

Source

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.

Source

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>>,

Source

pub const STORAGE_ALIGNMENT: usize = SA

Alignment of output buffer

Source

pub const VALUE_ALIGNMENT: usize = VA

Typical alignment of values being serialized

Source

pub const MAX_VALUE_ALIGNMENT: usize = MVA

Maximum alignment of values being serialized

Source

pub const MAX_CAPACITY: usize = MAX

Maximum capacity of output buffer.

Source

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>

Source§

fn pos_mapping(&self) -> &PosMapping

Get current position mapping

Source§

fn set_pos_mapping(&mut self, pos_mapping: PosMapping)

Set current position mapping

Source§

fn pos_for<T>(&self, value: &T) -> usize

Get position for a value
Source§

fn do_serialize_value<T: Serialize<Self>>(&mut self, value: &T)

Source§

fn do_push_slice<T>(&mut self, slice: &[T], _ptr_addr: Self::Addr)

Source§

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>

Source§

unsafe fn do_write_ptr(&mut self, ptr_pos: usize, target_pos: usize)

Overwrite pointer. Read more
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>

Source§

unsafe fn write_ptr(&mut self, ptr_pos: usize, target_pos: usize)

Overwrite pointer.

§Safety
  • ptr_pos and target_pos must both sit within bounds of output.
  • target_pos must be location of a valid value for the type being pointed to.
  • ptr_pos must be aligned for a pointer.
Source§

fn do_push_slice<T>(&mut self, slice: &[T], ptr_addr: Self::Addr)

Source§

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>

Source§

type Storage = AlignedVec<STORAGE_ALIGNMENT, VALUE_ALIGNMENT, MAX_VALUE_ALIGNMENT, MAX_CAPACITY>

Storage which backs this serializer.
Source§

type BorrowedStorage = BorrowedStorage

BorrowMut of Storage which backs this serializer. This enables creating a serializer from an existing Storage.
Source§

type Addr = TrackingAddr

Addr type this serializer uses.
Source§

fn serialize_value<T: Serialize<Self>>(&mut self, value: &T)

Serialize a value and all its dependencies. Read more
Source§

fn push_slice<T>(&mut self, slice: &[T], ptr_addr: Self::Addr)

Push a slice of values to output. Read more
Source§

fn push_and_process_slice<T, P: FnOnce(&mut Self)>( &mut self, slice: &[T], ptr_addr: Self::Addr, process: P, )

Push a slice of values to output and continue processing content of the slice. Read more
Source§

fn storage( &self, ) -> &AlignedVec<STORAGE_ALIGNMENT, VALUE_ALIGNMENT, MAX_VALUE_ALIGNMENT, MAX_CAPACITY>

Get immutable ref to Storage backing this Serializer.
Source§

fn storage_mut( &mut self, ) -> &mut AlignedVec<STORAGE_ALIGNMENT, VALUE_ALIGNMENT, MAX_VALUE_ALIGNMENT, MAX_CAPACITY>

Get mutable ref to Storage backing this Serializer.
Source§

fn into_storage(self) -> BorrowedStorage

Consume Serializer and return the backing storage as a BorrowMut<Storage>. Read more
Source§

fn serialize<T: Serialize<Self>>(self, value: &T) -> Self::BorrowedStorage

Serialize a value and all its dependencies. Read more
Source§

fn push<T>(&mut self, value: &T, ptr_addr: Self::Addr)

Push a value to output. Read more
Source§

fn push_and_process<T, P: FnOnce(&mut Self)>( &mut self, t: &T, ptr_addr: Self::Addr, process: P, )

Push a value to output and continue processing the value. Read more
Source§

fn push_raw<T>(&mut self, value: &T)

Push a value to output. Read more
Source§

fn push_raw_slice<T>(&mut self, slice: &[T])

Push a slice of values to output. Read more
Source§

fn push_raw_bytes(&mut self, bytes: &[u8])

Push raw bytes to output. Read more
Source§

unsafe fn write<T>(&mut self, value: &T, addr: usize)

Write a value to storage at a specific position. Read more
Source§

fn write_correction<W: FnOnce(&mut Self)>(&mut self, write: W)

Write a correction to storage. Read more
Source§

fn finalize(self) -> Self::BorrowedStorage

Finalize serialization, consume serializer, and return backing storage as BorrowMut<Storage>.
Source§

fn capacity(&self) -> usize

Get current capacity of output.
Source§

fn pos(&self) -> usize

Get current position in output.

Auto Trait Implementations§

§

impl<const STORAGE_ALIGNMENT: usize, const VALUE_ALIGNMENT: usize, const MAX_VALUE_ALIGNMENT: usize, const MAX_CAPACITY: usize, BorrowedStorage> Freeze for PtrOffsetSerializer<STORAGE_ALIGNMENT, VALUE_ALIGNMENT, MAX_VALUE_ALIGNMENT, MAX_CAPACITY, BorrowedStorage>
where BorrowedStorage: Freeze,

§

impl<const STORAGE_ALIGNMENT: usize, const VALUE_ALIGNMENT: usize, const MAX_VALUE_ALIGNMENT: usize, const MAX_CAPACITY: usize, BorrowedStorage> RefUnwindSafe for PtrOffsetSerializer<STORAGE_ALIGNMENT, VALUE_ALIGNMENT, MAX_VALUE_ALIGNMENT, MAX_CAPACITY, BorrowedStorage>
where BorrowedStorage: RefUnwindSafe,

§

impl<const STORAGE_ALIGNMENT: usize, const VALUE_ALIGNMENT: usize, const MAX_VALUE_ALIGNMENT: usize, const MAX_CAPACITY: usize, BorrowedStorage> Send for PtrOffsetSerializer<STORAGE_ALIGNMENT, VALUE_ALIGNMENT, MAX_VALUE_ALIGNMENT, MAX_CAPACITY, BorrowedStorage>
where BorrowedStorage: Send,

§

impl<const STORAGE_ALIGNMENT: usize, const VALUE_ALIGNMENT: usize, const MAX_VALUE_ALIGNMENT: usize, const MAX_CAPACITY: usize, BorrowedStorage> Sync for PtrOffsetSerializer<STORAGE_ALIGNMENT, VALUE_ALIGNMENT, MAX_VALUE_ALIGNMENT, MAX_CAPACITY, BorrowedStorage>
where BorrowedStorage: Sync,

§

impl<const STORAGE_ALIGNMENT: usize, const VALUE_ALIGNMENT: usize, const MAX_VALUE_ALIGNMENT: usize, const MAX_CAPACITY: usize, BorrowedStorage> Unpin for PtrOffsetSerializer<STORAGE_ALIGNMENT, VALUE_ALIGNMENT, MAX_VALUE_ALIGNMENT, MAX_CAPACITY, BorrowedStorage>
where BorrowedStorage: Unpin,

§

impl<const STORAGE_ALIGNMENT: usize, const VALUE_ALIGNMENT: usize, const MAX_VALUE_ALIGNMENT: usize, const MAX_CAPACITY: usize, BorrowedStorage> UnwindSafe for PtrOffsetSerializer<STORAGE_ALIGNMENT, VALUE_ALIGNMENT, MAX_VALUE_ALIGNMENT, MAX_CAPACITY, BorrowedStorage>
where BorrowedStorage: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.