pub struct FromInto<Wire, Target>(/* private fields */);Expand description
Serialize a Target by going through an intermediate wire representation
using the standard library From/Into conversions.
Wire is a schema (any SchemaWrite/SchemaRead implementor), and the
intermediate value is the type that schema serializes — its
SchemaWrite::Src on write and SchemaRead::Dst on read. On write the
Target is converted into that value (via From<&Target>) and serialized
with Wire; on read the value is deserialized with Wire and converted back
into the Target (via Target: From<…>).
Because Wire is a schema rather than the value itself, the wire side may be:
- a self-describing type, where the value is the type itself — e.g.
i64, or any type derivingSchemaWrite/SchemaRead; - a schema adapter over some other value — e.g.
containers::Vec<u8, UseIntLen<u16>>, whose value isVec<u8>.
The Target is the type of the annotated field. Use the _ inference token
to have it filled in automatically from the field type:
// Our in-memory type with a layout we don't want to serialize directly.
#[derive(Debug, PartialEq, Clone, Copy)]
struct Celsius(f64);
// The on-the-wire representation: hundredths of a degree as an integer.
impl From<&Celsius> for i64 {
fn from(c: &Celsius) -> i64 {
(c.0 * 100.0) as i64
}
}
impl From<i64> for Celsius {
fn from(raw: i64) -> Celsius {
Celsius(raw as f64 / 100.0)
}
}
#[derive(SchemaWrite, SchemaRead, Debug, PartialEq)]
struct Reading {
#[wincode(with = "FromInto<i64, _>")]
temp: Celsius,
}
let reading = Reading { temp: Celsius(21.5) };
let bytes = wincode::serialize(&reading).unwrap();
assert_eq!(reading, wincode::deserialize(&bytes).unwrap());Trait Implementations§
Source§impl<'de, C, Wire, Target> SchemaRead<'de, C> for FromInto<Wire, Target>
impl<'de, C, Wire, Target> SchemaRead<'de, C> for FromInto<Wire, Target>
Auto Trait Implementations§
impl<Wire, Target> Freeze for FromInto<Wire, Target>
impl<Wire, Target> RefUnwindSafe for FromInto<Wire, Target>
impl<Wire, Target> Send for FromInto<Wire, Target>
impl<Wire, Target> Sync for FromInto<Wire, Target>
impl<Wire, Target> Unpin for FromInto<Wire, Target>
impl<Wire, Target> UnsafeUnpin for FromInto<Wire, Target>
impl<Wire, Target> UnwindSafe for FromInto<Wire, Target>
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
Mutably borrows from an owned value. Read more
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>
Deserialize the input bytes into a new
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<()>
Deserialize the input bytes into
dst.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>
Deserialize the input
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<()>
Deserialize the input
src bytes 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>
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<()>
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, 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>
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<()>
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.impl<T, C> SchemaReadOwned<C> for Twhere
C: ConfigCore,
T: for<'de> SchemaRead<'de, C>,
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>>
Serialize a serializable type into a
Vec of bytes.Source§fn serialize_into(dst: impl Writer, src: &Self::Src) -> WriteResult<()>
fn serialize_into(dst: impl Writer, src: &Self::Src) -> WriteResult<()>
Serialize a serializable type into the given byte buffer. Read more
Source§fn serialized_size(src: &Self::Src) -> WriteResult<u64>
fn serialized_size(src: &Self::Src) -> WriteResult<u64>
Get the size in bytes of the type when serialized.
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>>
Serialize a serializable type into a
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<()>
Source§fn serialized_size(src: &Self::Src, config: C) -> WriteResult<u64>
fn serialized_size(src: &Self::Src, config: C) -> WriteResult<u64>
Get the size in bytes of the type when serialized.
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>
Convert a
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>
Convert the encoding target to a
u32.Source§fn size_of_from_u32(value: u32) -> WriteResult<usize>
fn size_of_from_u32(value: u32) -> WriteResult<usize>
Get the size of the encoding target from the given
u32. Read more