pub trait TryUnpack {
type Unpacked;
type Error;
// Required method
fn try_unpack(self) -> Result<Self::Unpacked, Self::Error>;
}
Expand description
A trait for attempting to unpack the values of one type into another.
§Examples
Basic usage:
impl<T, U> TryUnpack for (Option<T>, Option<U>) {
type Unpacked = (T, U);
type Error = ();
fn try_unpack(self) -> Result<Self::Unpacked, Self::Error> {
match self {
(Some(t), Some(u)) => Ok((t, u)),
_ => Err(()),
}
}
}
assert_eq!(Ok((1i8, 2u8)), (Some(1i8), Some(2u8)).try_unpack());
assert_eq!(Err(()), (Some(1i8), None).try_unpack());
Required Associated Types§
Required Methods§
Sourcefn try_unpack(self) -> Result<Self::Unpacked, Self::Error>
fn try_unpack(self) -> Result<Self::Unpacked, Self::Error>
Tries to unpack value(s) from self
and returns a Self::Error
if the operation fails.
Implementations on Foreign Types§
Source§impl<T1, A1: Display, T2, A2: Display> TryUnpack for (NamedField<Option<T1>, A1>, NamedField<Option<T2>, A2>)
impl<T1, A1: Display, T2, A2: Display> TryUnpack for (NamedField<Option<T1>, A1>, NamedField<Option<T2>, A2>)
Source§fn try_unpack(self) -> Result<Self::Unpacked, Self::Error>
fn try_unpack(self) -> Result<Self::Unpacked, Self::Error>
Tries to unpack values from a tuple of n fields each with type
NamedField<Option<Tᵢ>, Aᵢ: Display>
.
If every Option<Tᵢ>
contains a value, then the returned value has the type
(T₁, T₂, …, Tₙ)
, i.e., every value is unwrapped, and every name is
discarded. Otherwise, the returned value is a Self::Error
containing name of
each field missing a value.
type Unpacked = (T1, T2)
type Error = Error
Source§impl<T1, A1: Display, T2, A2: Display, T3, A3: Display> TryUnpack for (NamedField<Option<T1>, A1>, NamedField<Option<T2>, A2>, NamedField<Option<T3>, A3>)
impl<T1, A1: Display, T2, A2: Display, T3, A3: Display> TryUnpack for (NamedField<Option<T1>, A1>, NamedField<Option<T2>, A2>, NamedField<Option<T3>, A3>)
Source§fn try_unpack(self) -> Result<Self::Unpacked, Self::Error>
fn try_unpack(self) -> Result<Self::Unpacked, Self::Error>
Tries to unpack values from a tuple of n fields each with type
NamedField<Option<Tᵢ>, Aᵢ: Display>
.
If every Option<Tᵢ>
contains a value, then the returned value has the type
(T₁, T₂, …, Tₙ)
, i.e., every value is unwrapped, and every name is
discarded. Otherwise, the returned value is a Self::Error
containing name of
each field missing a value.
type Unpacked = (T1, T2, T3)
type Error = Error
Source§impl<T1, A1: Display, T2, A2: Display, T3, A3: Display, T4, A4: Display> TryUnpack for (NamedField<Option<T1>, A1>, NamedField<Option<T2>, A2>, NamedField<Option<T3>, A3>, NamedField<Option<T4>, A4>)
impl<T1, A1: Display, T2, A2: Display, T3, A3: Display, T4, A4: Display> TryUnpack for (NamedField<Option<T1>, A1>, NamedField<Option<T2>, A2>, NamedField<Option<T3>, A3>, NamedField<Option<T4>, A4>)
Source§fn try_unpack(self) -> Result<Self::Unpacked, Self::Error>
fn try_unpack(self) -> Result<Self::Unpacked, Self::Error>
Tries to unpack values from a tuple of n fields each with type
NamedField<Option<Tᵢ>, Aᵢ: Display>
.
If every Option<Tᵢ>
contains a value, then the returned value has the type
(T₁, T₂, …, Tₙ)
, i.e., every value is unwrapped, and every name is
discarded. Otherwise, the returned value is a Self::Error
containing name of
each field missing a value.
type Unpacked = (T1, T2, T3, T4)
type Error = Error
Source§impl<T1, A1: Display, T2, A2: Display, T3, A3: Display, T4, A4: Display, T5, A5: Display> TryUnpack for (NamedField<Option<T1>, A1>, NamedField<Option<T2>, A2>, NamedField<Option<T3>, A3>, NamedField<Option<T4>, A4>, NamedField<Option<T5>, A5>)
impl<T1, A1: Display, T2, A2: Display, T3, A3: Display, T4, A4: Display, T5, A5: Display> TryUnpack for (NamedField<Option<T1>, A1>, NamedField<Option<T2>, A2>, NamedField<Option<T3>, A3>, NamedField<Option<T4>, A4>, NamedField<Option<T5>, A5>)
Source§fn try_unpack(self) -> Result<Self::Unpacked, Self::Error>
fn try_unpack(self) -> Result<Self::Unpacked, Self::Error>
Tries to unpack values from a tuple of n fields each with type
NamedField<Option<Tᵢ>, Aᵢ: Display>
.
If every Option<Tᵢ>
contains a value, then the returned value has the type
(T₁, T₂, …, Tₙ)
, i.e., every value is unwrapped, and every name is
discarded. Otherwise, the returned value is a Self::Error
containing name of
each field missing a value.