pub trait TransitiveTryFrom<Error, Initial> {
// Provided method
fn transitive_try_from<Transitive>(src: Initial) -> Result<Self, Error>
where Transitive: TryFrom<Initial>,
Self: TryFrom<Transitive, Error = Error>,
Error: From<<Transitive as TryFrom<Initial>>::Error> { ... }
}
Expand description
A trait to perform a transitive try_from
conversion.
This trait allows for a two-step conversion process where an initial type Initial
is first converted to an intermediate type Transitive
, and then to the final type Self
.
§Type Parameters
Error
: The error type that can be produced during the conversion.Initial
: The initial type from which the conversion starts.
§Requirements
Transitive
must implementTryFrom<Initial>
.Self
must implementTryFrom<Transitive>
with the same error type.Error
must implementFrom<<Transitive as TryFrom<Initial>>::Error>
.
§Example
use proper_path_tools::TransitiveTryFrom;
use std::convert::TryFrom;
struct InitialType;
struct IntermediateType;
struct FinalType;
struct ConversionError;
impl TryFrom< InitialType > for IntermediateType
{
type Error = ConversionError;
fn try_from( value : InitialType ) -> Result< Self, Self::Error >
{
// Conversion logic here
Ok( IntermediateType )
}
}
impl TryFrom< IntermediateType > for FinalType
{
type Error = ConversionError;
fn try_from( value : IntermediateType ) -> Result< Self, Self::Error >
{
// Conversion logic here
Ok( FinalType )
}
}
let initial = InitialType;
let final_result : Result< FinalType, ConversionError > = FinalType::transitive_try_from::< IntermediateType >( initial );
Provided Methods§
Sourcefn transitive_try_from<Transitive>(src: Initial) -> Result<Self, Error>
fn transitive_try_from<Transitive>(src: Initial) -> Result<Self, Error>
Performs a transitive try_from
conversion.
This method first converts the src
of type Initial
to the intermediate type Transitive
,
and then converts the intermediate type to the final type Self
.
§Arguments
src
: The initial value to be converted.
§Returns
Ok(Self)
: If both conversions succeed.Err(Error)
: If either conversion fails.
§Example
See the trait-level documentation for an example.