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> SchemaRead<'de> for Pod<T>
where T: Copy + 'static,

Source§

const TYPE_META: TypeMeta

Source§

type Dst = T

Source§

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

Read into dst from reader. Read more
Source§

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

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

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

Source§

const TYPE_META: TypeMeta

Source§

type Src = T

Source§

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

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

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

Write Self::Src to writer.
Source§

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

Source§

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

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

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

Source§

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

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

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

Deserialize bytes into target.
Source§

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

Source§

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

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

fn deserialize_from_into<'de>( src: &mut impl Reader<'de>, dst: &mut MaybeUninit<<Self as SchemaRead<'de>>::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> Serialize for T
where T: SchemaWrite + ?Sized,

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: &mut 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, 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> SchemaReadOwned for T
where T: for<'de> SchemaRead<'de>,