pub struct TryFromInto<U>(/* private fields */);Expand description
Converts from and into another type with fallible conversions.
This is like FromInto but uses TryFrom and TryInto. Failed
conversions are reported as errors. Like with FromInto the value is
cloned and converted once more with #[deser(skip_serializing_optionals)]
to find out if it’s optional.
use deser::{Deserialize, Serialize};
use deser::adapters::TryFromInto;
#[derive(Clone)]
pub struct Percent(u8);
impl TryFrom<u64> for Percent {
type Error = &'static str;
fn try_from(value: u64) -> Result<Percent, Self::Error> {
if value <= 100 {
Ok(Percent(value as u8))
} else {
Err("out of range")
}
}
}
impl TryFrom<Percent> for u64 {
type Error = std::convert::Infallible;
fn try_from(value: Percent) -> Result<u64, Self::Error> {
Ok(value.0 as u64)
}
}
#[derive(Serialize, Deserialize)]
pub struct Progress {
#[deser(as = TryFromInto<u64>)]
done: Percent,
}Trait Implementations§
Source§impl<'de, T, U> Deserialize<'de, T> for TryFromInto<U>
impl<'de, T, U> Deserialize<'de, T> for TryFromInto<U>
Source§fn deserialize_into<'out>(
out: &'out mut Option<T>,
state: &mut State,
) -> SinkHandle<'out, 'de>
fn deserialize_into<'out>( out: &'out mut Option<T>, state: &mut State, ) -> SinkHandle<'out, 'de>
Creates a sink that deserializes the value into the given slot. Read more
Source§fn expecting() -> Cow<'static, str>
fn expecting() -> Cow<'static, str>
Returns what the value expects, for error messages. Read more
Source§fn initial_value() -> Option<T>
fn initial_value() -> Option<T>
Provides the value of a missing struct field. Read more
Source§fn deserialize_atom(
slot: &mut Slot<T, Self>,
atom: Atom<'_>,
state: &mut State,
) -> Result<(), Error>
fn deserialize_atom( slot: &mut Slot<T, Self>, atom: Atom<'_>, state: &mut State, ) -> Result<(), Error>
Deserializes an atom into the slot. Read more
Source§fn deserialize_borrowed_atom(
slot: &mut Slot<T, Self>,
atom: Atom<'de>,
state: &mut State,
) -> Result<(), Error>
fn deserialize_borrowed_atom( slot: &mut Slot<T, Self>, atom: Atom<'de>, state: &mut State, ) -> Result<(), Error>
Deserializes an atom that borrows from the data being deserialized
into the slot. Read more
Source§fn describe_type(d: &mut dyn Describe)
fn describe_type(d: &mut dyn Describe)
Describes the Rust shape of the type. Read more
Source§fn deserialize_update<'out>(
value: &'out mut T,
state: &mut State,
) -> SinkHandle<'out, 'de>
fn deserialize_update<'out>( value: &'out mut T, state: &mut State, ) -> SinkHandle<'out, 'de>
Creates a sink that updates an existing value. Read more
Source§impl<T, U> Serialize<T> for TryFromInto<U>
impl<T, U> Serialize<T> for TryFromInto<U>
Source§fn serialize<'a>(value: &'a T, state: &mut State) -> Result<Emit<'a>, Error>
fn serialize<'a>(value: &'a T, state: &mut State) -> Result<Emit<'a>, Error>
Serializes the value.
Source§fn finish(value: &T, state: &mut State) -> Result<(), Error>
fn finish(value: &T, state: &mut State) -> Result<(), Error>
Invoked after the serialization finished. Read more
Source§fn container_shape(value: &T) -> ContainerShape
fn container_shape(value: &T) -> ContainerShape
Returns the shape of the value if it’s a map or sequence. Read more
Auto Trait Implementations§
impl<U> Freeze for TryFromInto<U>
impl<U> RefUnwindSafe for TryFromInto<U>
impl<U> Send for TryFromInto<U>
impl<U> Sync for TryFromInto<U>
impl<U> Unpin for TryFromInto<U>
impl<U> UnsafeUnpin for TryFromInto<U>
impl<U> UnwindSafe for TryFromInto<U>
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