Trait Serializer

Source
pub trait Serializer: Fallible {
    // Required methods
    fn pos(&self) -> usize;
    fn write(&mut self, bytes: &[u8]) -> Result<(), Self::Error>;

    // Provided methods
    fn pad(&mut self, padding: usize) -> Result<(), Self::Error> { ... }
    fn align(&mut self, align: usize) -> Result<usize, Self::Error> { ... }
    fn align_for<T>(&mut self) -> Result<usize, Self::Error> { ... }
    unsafe fn resolve_aligned<T: Archive + ?Sized>(
        &mut self,
        value: &T,
        resolver: T::Resolver,
    ) -> Result<usize, Self::Error> { ... }
    fn serialize_value<T: Serialize<Self>>(
        &mut self,
        value: &T,
    ) -> Result<usize, Self::Error> { ... }
    unsafe fn resolve_unsized_aligned<T: ArchiveUnsized + ?Sized>(
        &mut self,
        value: &T,
        to: usize,
        metadata_resolver: T::MetadataResolver,
    ) -> Result<usize, Self::Error> { ... }
    fn serialize_unsized_value<T: SerializeUnsized<Self> + ?Sized>(
        &mut self,
        value: &T,
    ) -> Result<usize, Self::Error> { ... }
}
Expand description

A byte sink that knows where it is.

A type that is io::Write can be wrapped in a WriteSerializer to equip it with Serializer.

It’s important that the memory for archived objects is properly aligned before attempting to read objects out of it; use an AlignedVec or the AlignedBytes wrappers if they are appropriate.

Required Methods§

Source

fn pos(&self) -> usize

Returns the current position of the serializer.

Source

fn write(&mut self, bytes: &[u8]) -> Result<(), Self::Error>

Attempts to write the given bytes to the serializer.

Provided Methods§

Source

fn pad(&mut self, padding: usize) -> Result<(), Self::Error>

Advances the given number of bytes as padding.

Source

fn align(&mut self, align: usize) -> Result<usize, Self::Error>

Aligns the position of the serializer to the given alignment.

Source

fn align_for<T>(&mut self) -> Result<usize, Self::Error>

Aligns the position of the serializer to be suitable to write the given type.

Source

unsafe fn resolve_aligned<T: Archive + ?Sized>( &mut self, value: &T, resolver: T::Resolver, ) -> Result<usize, Self::Error>

Resolves the given value with its resolver and writes the archived type.

Returns the position of the written archived type.

§Safety
  • resolver must be the result of serializing value
  • The serializer must be aligned for a T::Archived
Source

fn serialize_value<T: Serialize<Self>>( &mut self, value: &T, ) -> Result<usize, Self::Error>

Archives the given object and returns the position it was archived at.

Source

unsafe fn resolve_unsized_aligned<T: ArchiveUnsized + ?Sized>( &mut self, value: &T, to: usize, metadata_resolver: T::MetadataResolver, ) -> Result<usize, Self::Error>

Resolves the given reference with its resolver and writes the archived reference.

Returns the position of the written archived RelPtr.

§Safety
  • metadata_resolver must be the result of serializing the metadata of value
  • to must be the position of the serialized value within the archive
  • The serializer must be aligned for a RelPtr<T::Archived>
Source

fn serialize_unsized_value<T: SerializeUnsized<Self> + ?Sized>( &mut self, value: &T, ) -> Result<usize, Self::Error>

Archives a reference to the given object and returns the position it was archived at.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§