Skip to main content

TryFromIntoRef

Struct TryFromIntoRef 

Source
pub struct TryFromIntoRef<U>(/* private fields */);
Expand description

Converts from another type and into it from a reference with fallible conversions.

This is like TryFromInto but the value is serialized by converting a reference to it (&T: TryInto<U>), so it does not need to be cloned. Failed conversions are reported as errors.

use deser::{Deserialize, Serialize};
use deser::adapters::TryFromIntoRef;

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 = TryFromIntoRef<u64>)]
    done: Percent,
}

Trait Implementations§

Source§

impl<'de, T, U> Deserialize<'de, T> for TryFromIntoRef<U>
where U: Deserialize<'de> + 'static, T: TryFrom<U> + Send, T::Error: Display,

Source§

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>

Returns what the value expects, for error messages. Read more
Source§

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>

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>

Deserializes an atom that borrows from the data being deserialized into the slot. Read more
Source§

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>

Creates a sink that updates an existing value. Read more
Source§

impl<T, U> Serialize<T> for TryFromIntoRef<U>
where T: Sync + ?Sized, for<'x> &'x T: TryInto<U>, for<'x> <&'x T as TryInto<U>>::Error: Display, U: Serialize + Send + 'static,

Source§

fn serialize<'a>(value: &'a T, state: &mut State) -> Result<Emit<'a>, Error>

Serializes the value.
Source§

fn is_optional(value: &T) -> bool

Checks if the value represents an optional value. Read more
Source§

fn finish(value: &T, state: &mut State) -> Result<(), Error>

Invoked after the serialization finished. Read more
Source§

fn describe(value: &T, d: &mut dyn Describe)

Describes the Rust shape of the value. Read more
Source§

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 TryFromIntoRef<U>
where PhantomData<fn() -> U>: Freeze,

§

impl<U> RefUnwindSafe for TryFromIntoRef<U>
where PhantomData<fn() -> U>: RefUnwindSafe,

§

impl<U> Send for TryFromIntoRef<U>
where PhantomData<fn() -> U>: Send,

§

impl<U> Sync for TryFromIntoRef<U>
where PhantomData<fn() -> U>: Sync,

§

impl<U> Unpin for TryFromIntoRef<U>
where PhantomData<fn() -> U>: Unpin,

§

impl<U> UnsafeUnpin for TryFromIntoRef<U>
where PhantomData<fn() -> U>: UnsafeUnpin,

§

impl<U> UnwindSafe for TryFromIntoRef<U>
where PhantomData<fn() -> U>: 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<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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.