Skip to main content

Pod

Struct Pod 

Source
pub struct Pod<T: Copy + 'static>(/* private fields */);
Expand description

Indicates that the type is represented by raw bytes and does not have any invalid bit patterns.

By opting into Pod, you are telling wincode that it can serialize and deserialize a type with a single memcpy – it wont pay attention to things like struct layout, endianness, or anything else that would require validity or bit pattern checks. This is a very strong claim to make, so be sure that your type adheres to those requirements.

Composable with sequence containers or compound types (structs, tuples) for an optimized read/write implementation.

This can be useful outside of sequences as well, for example on newtype structs containing byte arrays with #[repr(transparent)].


💡 Note: as of wincode 0.2.0, Pod is no longer needed for types that wincode can determine are “Pod-safe”.

This includes:

  • u8
  • [u8; N]
  • structs comprised of the above, and;
    • annotated with #[derive(SchemaWrite)] or #[derive(SchemaRead)], and;
    • annotated with #[repr(transparent)] or #[repr(C)].

Similarly, using built-in std collections like Vec<T> or Box<[T]> where T is one of the above will also be automatically optimized.

You’ll really only need to reach for Pod when dealing with foreign types for which you cannot derive SchemaWrite or SchemaRead. Or you’re in a controlled scenario where you explicitly want to avoid endianness or layout checks.

§Safety

  • The type must allow any bit pattern (e.g., no bools, no chars, etc.)
  • If used on a compound type like a struct, all fields must be also be Pod, its layout must be guaranteed (via #[repr(transparent)] or #[repr(C)]), and the struct must not have any padding.
  • Must not contain references or pointers (includes types like Vec or Box).
    • Note, you may use Pod inside types like Vec or Box, e.g., Vec<Pod<T>> or Box<[Pod<T>]>, but specifying Pod on the outer type is invalid.

§Examples

A repr-transparent newtype struct containing a byte array where you cannot derive SchemaWrite or SchemaRead:

#[derive(Serialize, Deserialize, Clone, Copy)]
#[repr(transparent)]
struct Address([u8; 32]);

#[derive(Serialize, Deserialize, SchemaWrite, SchemaRead)]
struct MyStruct {
    #[wincode(with = "Pod<_>")]
    address: Address
}

let my_struct = MyStruct {
    address: Address(array::from_fn(|i| i as u8)),
};
let wincode_bytes = wincode::serialize(&my_struct).unwrap();
let bincode_bytes = bincode::serialize(&my_struct).unwrap();
assert_eq!(wincode_bytes, bincode_bytes);

Trait Implementations§

Source§

impl<'de, T, C: ConfigCore> SchemaRead<'de, C> for Pod<T>
where T: Copy + 'static,

Source§

const TYPE_META: TypeMeta

Metadata about the type’s serialization. Read more
Source§

type Dst = T

Source§

fn read( reader: impl Reader<'de>, dst: &mut MaybeUninit<Self::Dst>, ) -> ReadResult<()>

Read into dst from reader. Read more
Source§

fn get(reader: impl Reader<'de>) -> ReadResult<Self::Dst>

Read Self::Dst from reader into a new Self::Dst.
Source§

impl<T, C: ConfigCore> SchemaWrite<C> for Pod<T>
where T: Copy + 'static,

Source§

const TYPE_META: TypeMeta

Metadata about the type’s serialization. Read more
Source§

type Src = T

Source§

fn size_of(_src: &Self::Src) -> WriteResult<usize>

Get the serialized size of Self::Src. Read more
Source§

fn write(writer: impl Writer, src: &Self::Src) -> WriteResult<()>

Write Self::Src to writer.
Source§

impl<T, C: ConfigCore> ZeroCopy<C> for Pod<T>
where T: Copy + 'static,

Source§

fn from_bytes<'de>(bytes: &'de [u8], config: C) -> ReadResult<&'de Self>
where Self: SchemaRead<'de, C, Dst = Self> + Sized,

Like crate::ZeroCopy::from_bytes, but allows the caller to provide a custom configuration.
Source§

fn from_bytes_mut<'de>( bytes: &'de mut [u8], config: C, ) -> ReadResult<&'de mut Self>
where Self: SchemaRead<'de, C, Dst = Self> + Sized,

Like crate::ZeroCopy::from_bytes_mut, but allows the caller to provide a custom configuration.

Auto Trait Implementations§

§

impl<T> Freeze for Pod<T>

§

impl<T> RefUnwindSafe for Pod<T>
where T: RefUnwindSafe,

§

impl<T> Send for Pod<T>
where T: Send,

§

impl<T> Sync for Pod<T>
where T: Sync,

§

impl<T> Unpin for Pod<T>
where T: Unpin,

§

impl<T> UnwindSafe for Pod<T>
where T: 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<'de, T> Deserialize<'de> for T
where T: SchemaRead<'de, Configuration>,

Source§

fn deserialize(src: &'de [u8]) -> ReadResult<Self::Dst>

Deserialize the input src bytes into a new Self::Dst.
Source§

fn deserialize_into( src: &'de [u8], dst: &mut MaybeUninit<Self::Dst>, ) -> ReadResult<()>

Deserialize the input src bytes into dst.
Source§

impl<'de, T, C> Deserialize<'de, C> for T
where C: Config, T: SchemaRead<'de, C>,

Source§

fn deserialize(src: &'de [u8], config: C) -> ReadResult<Self::Dst>

Deserialize the input bytes into a new Self::Dst.
Source§

fn deserialize_into( src: &'de [u8], dst: &mut MaybeUninit<Self::Dst>, config: C, ) -> ReadResult<()>

Deserialize the input bytes into dst.
Source§

impl<T, C> DeserializeOwned<C> for T
where C: Config, T: SchemaReadOwned<C>,

Source§

fn deserialize_from<'de>( src: impl Reader<'de>, ) -> ReadResult<<Self as SchemaRead<'de, C>>::Dst>

Deserialize from the given Reader into a new Self::Dst.
Source§

fn deserialize_from_into<'de>( src: impl Reader<'de>, dst: &mut MaybeUninit<<Self as SchemaRead<'de, C>>::Dst>, ) -> ReadResult<()>

Deserialize from the given Reader into dst.
Source§

impl<T> DeserializeOwned for T

Source§

fn deserialize_from<'de>( src: impl Reader<'de>, ) -> ReadResult<<Self as SchemaRead<'de, DefaultConfig>>::Dst>

Deserialize from the given Reader into a new Self::Dst.
Source§

fn deserialize_from_into<'de>( src: impl Reader<'de>, dst: &mut MaybeUninit<<Self as SchemaRead<'de, DefaultConfig>>::Dst>, ) -> ReadResult<()>

Deserialize from the given Reader into dst.
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, C> Serialize<C> for T
where C: Config, T: SchemaWrite<C> + ?Sized,

Source§

fn serialize(src: &Self::Src, config: C) -> WriteResult<Vec<u8>>

Available on crate feature alloc only.
Serialize a serializable type into a Vec of bytes.
Source§

fn serialize_into( dst: impl Writer, src: &Self::Src, config: C, ) -> WriteResult<()>

Serialize a serializable type into the given Writer.
Source§

fn serialized_size(src: &Self::Src, config: C) -> WriteResult<u64>

Get the size in bytes of the type when serialized.
Source§

impl<T> Serialize for T

Source§

fn serialize(src: &Self::Src) -> WriteResult<Vec<u8>>

Available on crate feature alloc only.
Serialize a serializable type into a Vec of bytes.
Source§

fn serialize_into(dst: impl Writer, src: &Self::Src) -> WriteResult<()>

Serialize a serializable type into the given byte buffer.
Source§

fn serialized_size(src: &Self::Src) -> WriteResult<u64>

Get the size in bytes of the type when serialized.
Source§

impl<T, Target, C> TagEncoding<C> for T
where C: ConfigCore, T: for<'de> SchemaRead<'de, C, Dst = Target> + SchemaWrite<C, Src = Target> + 'static, Target: TryFrom<u32>, u32: TryFrom<Target>,

Source§

type Target = Target

Source§

fn try_from_u32( value: u32, ) -> Result<<T as TagEncoding<C>>::Target, TagEncodingOverflow>

Convert a u32 to the encoding target.
Source§

fn try_into_u32( x: <T as TagEncoding<C>>::Target, ) -> Result<u32, TagEncodingOverflow>

Convert the encoding target to a u32.
Source§

fn size_of_from_u32(value: u32) -> WriteResult<usize>

Get the size of the encoding target from the given u32. Read more
Source§

fn write_from_u32(writer: impl Writer, value: u32) -> WriteResult<()>

Write the encoding target from the given u32 to the given Writer. Read more
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.
Source§

impl<T> ZeroCopy for T

Source§

fn from_bytes<'de>(bytes: &'de [u8]) -> ReadResult<&'de Self>
where Self: SchemaRead<'de, DefaultConfig, Dst = Self> + Sized,

Get a reference to a type from the given bytes. Read more
Source§

fn from_bytes_mut<'de>(bytes: &'de mut [u8]) -> ReadResult<&'de mut Self>
where Self: SchemaRead<'de, DefaultConfig, Dst = Self> + Sized,

Get a mutable reference to a type from the given bytes. Read more
Source§

impl<T, C> SchemaReadOwned<C> for T
where C: ConfigCore, T: for<'de> SchemaRead<'de, C>,