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, C: ConfigCore> SchemaRead<'de, C> for Pod<T>where
T: Copy + 'static,
impl<'de, T, C: ConfigCore> SchemaRead<'de, C> for Pod<T>where
T: Copy + 'static,
Source§impl<T, C: ConfigCore> SchemaWrite<C> for Pod<T>where
T: Copy + 'static,
impl<T, C: ConfigCore> SchemaWrite<C> for Pod<T>where
T: Copy + 'static,
Source§impl<T, C: ConfigCore> ZeroCopy<C> for Pod<T>where
T: Copy + 'static,
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,
fn from_bytes<'de>(bytes: &'de [u8], config: C) -> ReadResult<&'de Self>where
Self: SchemaRead<'de, C, Dst = Self> + Sized,
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,
fn from_bytes_mut<'de>(
bytes: &'de mut [u8],
config: C,
) -> ReadResult<&'de mut Self>where
Self: SchemaRead<'de, C, Dst = Self> + Sized,
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> 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, Configuration>,
impl<'de, T> Deserialize<'de> for Twhere
T: SchemaRead<'de, Configuration>,
Source§fn deserialize(src: &'de [u8]) -> ReadResult<Self::Dst>
fn deserialize(src: &'de [u8]) -> ReadResult<Self::Dst>
src 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<()>
src bytes into dst.Source§impl<'de, T, C> Deserialize<'de, C> for Twhere
C: Config,
T: SchemaRead<'de, C>,
impl<'de, T, C> Deserialize<'de, C> for Twhere
C: Config,
T: SchemaRead<'de, C>,
Source§fn deserialize(src: &'de [u8], config: C) -> ReadResult<Self::Dst>
fn deserialize(src: &'de [u8], config: C) -> ReadResult<Self::Dst>
Self::Dst.Source§fn deserialize_into(
src: &'de [u8],
dst: &mut MaybeUninit<Self::Dst>,
config: C,
) -> ReadResult<()>
fn deserialize_into( src: &'de [u8], dst: &mut MaybeUninit<Self::Dst>, config: C, ) -> ReadResult<()>
dst.Source§impl<T, C> DeserializeOwned<C> for Twhere
C: Config,
T: SchemaReadOwned<C>,
impl<T, C> DeserializeOwned<C> for Twhere
C: Config,
T: SchemaReadOwned<C>,
Source§fn deserialize_from<'de>(
src: impl Reader<'de>,
) -> ReadResult<<Self as SchemaRead<'de, C>>::Dst>
fn deserialize_from<'de>( src: impl Reader<'de>, ) -> ReadResult<<Self as SchemaRead<'de, C>>::Dst>
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<()>
fn deserialize_from_into<'de>( src: impl Reader<'de>, dst: &mut MaybeUninit<<Self as SchemaRead<'de, C>>::Dst>, ) -> ReadResult<()>
Reader into dst.Source§impl<T> DeserializeOwned for Twhere
T: SchemaReadOwned<Configuration>,
impl<T> DeserializeOwned for Twhere
T: SchemaReadOwned<Configuration>,
Source§fn deserialize_from<'de>(
src: impl Reader<'de>,
) -> ReadResult<<Self as SchemaRead<'de, DefaultConfig>>::Dst>
fn deserialize_from<'de>( src: impl Reader<'de>, ) -> ReadResult<<Self as SchemaRead<'de, DefaultConfig>>::Dst>
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<()>
fn deserialize_from_into<'de>( src: impl Reader<'de>, dst: &mut MaybeUninit<<Self as SchemaRead<'de, DefaultConfig>>::Dst>, ) -> ReadResult<()>
Reader into dst.Source§impl<T, C> Serialize<C> for T
impl<T, C> Serialize<C> for T
Source§fn serialize(src: &Self::Src, config: C) -> WriteResult<Vec<u8>>
fn serialize(src: &Self::Src, config: C) -> WriteResult<Vec<u8>>
alloc only.Vec of bytes.Source§fn serialize_into(
dst: impl Writer,
src: &Self::Src,
config: C,
) -> WriteResult<()>
fn serialize_into( dst: impl Writer, src: &Self::Src, config: C, ) -> WriteResult<()>
Writer.Source§fn serialized_size(src: &Self::Src, config: C) -> WriteResult<u64>
fn serialized_size(src: &Self::Src, config: C) -> WriteResult<u64>
Source§impl<T> Serialize for T
impl<T> Serialize for T
Source§fn serialize(src: &Self::Src) -> WriteResult<Vec<u8>>
fn serialize(src: &Self::Src) -> WriteResult<Vec<u8>>
alloc only.Vec of bytes.Source§fn serialize_into(dst: impl Writer, src: &Self::Src) -> WriteResult<()>
fn serialize_into(dst: impl Writer, src: &Self::Src) -> WriteResult<()>
Source§fn serialized_size(src: &Self::Src) -> WriteResult<u64>
fn serialized_size(src: &Self::Src) -> WriteResult<u64>
Source§impl<T, Target, C> TagEncoding<C> for Twhere
C: ConfigCore,
T: for<'de> SchemaRead<'de, C, Dst = Target> + SchemaWrite<C, Src = Target> + 'static,
Target: TryFrom<u32>,
u32: TryFrom<Target>,
impl<T, Target, C> TagEncoding<C> for Twhere
C: ConfigCore,
T: for<'de> SchemaRead<'de, C, Dst = Target> + SchemaWrite<C, Src = Target> + 'static,
Target: TryFrom<u32>,
u32: TryFrom<Target>,
type Target = Target
Source§fn try_from_u32(
value: u32,
) -> Result<<T as TagEncoding<C>>::Target, TagEncodingOverflow>
fn try_from_u32( value: u32, ) -> Result<<T as TagEncoding<C>>::Target, TagEncodingOverflow>
u32 to the encoding target.Source§fn try_into_u32(
x: <T as TagEncoding<C>>::Target,
) -> Result<u32, TagEncodingOverflow>
fn try_into_u32( x: <T as TagEncoding<C>>::Target, ) -> Result<u32, TagEncodingOverflow>
u32.Source§fn size_of_from_u32(value: u32) -> WriteResult<usize>
fn size_of_from_u32(value: u32) -> WriteResult<usize>
u32. Read more