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)].
- annotated with
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, nochars, 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
VecorBox).- Note, you may use
Podinside types likeVecorBox, e.g.,Vec<Pod<T>>orBox<[Pod<T>]>, but specifyingPodon the outer type is invalid.
- Note, you may use
§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,
impl<'de, T> SchemaRead<'de> for Pod<T>where
T: Copy + 'static,
Source§impl<T> SchemaWrite for Pod<T>where
T: Copy + 'static,
impl<T> SchemaWrite for Pod<T>where
T: Copy + 'static,
Source§impl<T> ZeroCopy for Pod<T>where
T: Copy + 'static,
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,
fn from_bytes<'de>(bytes: &'de [u8]) -> ReadResult<&'de Self>where
Self: SchemaRead<'de, Dst = Self> + Sized,
Source§fn from_bytes_mut<'de>(bytes: &'de mut [u8]) -> ReadResult<&'de mut Self>where
Self: SchemaRead<'de, Dst = Self> + Sized,
fn from_bytes_mut<'de>(bytes: &'de mut [u8]) -> ReadResult<&'de mut Self>where
Self: SchemaRead<'de, Dst = Self> + Sized,
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<'de, T> Deserialize<'de> for Twhere
T: SchemaRead<'de>,
impl<'de, T> Deserialize<'de> for Twhere
T: SchemaRead<'de>,
Source§fn deserialize(src: &'de [u8]) -> ReadResult<Self::Dst>
fn deserialize(src: &'de [u8]) -> ReadResult<Self::Dst>
bytes into a new Self::Dst.Source§fn deserialize_into(
src: &'de [u8],
dst: &mut MaybeUninit<Self::Dst>,
) -> ReadResult<()>
fn deserialize_into( src: &'de [u8], dst: &mut MaybeUninit<Self::Dst>, ) -> ReadResult<()>
bytes into target.Source§impl<T> DeserializeOwned for Twhere
T: SchemaReadOwned,
impl<T> DeserializeOwned for Twhere
T: SchemaReadOwned,
Source§fn deserialize_from<'de>(
src: &mut impl Reader<'de>,
) -> ReadResult<<Self as SchemaRead<'de>>::Dst>
fn deserialize_from<'de>( src: &mut impl Reader<'de>, ) -> ReadResult<<Self as SchemaRead<'de>>::Dst>
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<()>
fn deserialize_from_into<'de>( src: &mut impl Reader<'de>, dst: &mut MaybeUninit<<Self as SchemaRead<'de>>::Dst>, ) -> ReadResult<()>
Reader into dst.Source§impl<T> Serialize for Twhere
T: SchemaWrite + ?Sized,
impl<T> Serialize for Twhere
T: SchemaWrite + ?Sized,
Source§fn serialize(src: &Self::Src) -> WriteResult<Vec<u8>>
fn serialize(src: &Self::Src) -> WriteResult<Vec<u8>>
alloc only.Vec of bytes.