Struct BoxedFnConverter

Source
pub struct BoxedFnConverter<L, R, EL = Infallible, ER = Infallible> { /* private fields */ }
Expand description

A converter that uses boxed closures for conversions.

This is similar to FnConverter but uses trait objects, making its type always descriptable.

§Example

use cached_pair::{Pair, boxed_fn_converter, BoxedFnConverter};
use std::convert::Infallible;
use std::num::TryFromIntError;

let converter = boxed_fn_converter(
    // Right to left
    |i: &i32| -> Result<u8, TryFromIntError> { (*i - 100).try_into() },
    // Left to right
    |u: &u8| -> Result<i32, Infallible> { Ok((*u as i32) + 100) },
);

// The type of the converter is descriptable! This is not the case with [`fn_converter()`].
let pair: Pair<u8, i32, BoxedFnConverter<u8, i32, TryFromIntError, Infallible>> =
    Pair::from_right_conv(142i32, converter);
assert_eq!(pair.try_left(), Ok(&42u8));

Trait Implementations§

Source§

impl<L, R, EL, ER> Converter<L, R> for BoxedFnConverter<L, R, EL, ER>

Source§

type ToLeftError = EL

The error type returned when converting from right to left.
Source§

type ToRightError = ER

The error type returned when converting from left to right.
Source§

fn convert_to_left(&self, right: &R) -> Result<L, Self::ToLeftError>

Converts a reference to a right value into a left value.
Source§

fn convert_to_right(&self, left: &L) -> Result<R, Self::ToRightError>

Converts a reference to a left value into a right value.
Source§

impl<L, R, EL, ER> Debug for BoxedFnConverter<L, R, EL, ER>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<L, R, EL, ER> Freeze for BoxedFnConverter<L, R, EL, ER>

§

impl<L, R, EL = Infallible, ER = Infallible> !RefUnwindSafe for BoxedFnConverter<L, R, EL, ER>

§

impl<L, R, EL = Infallible, ER = Infallible> !Send for BoxedFnConverter<L, R, EL, ER>

§

impl<L, R, EL = Infallible, ER = Infallible> !Sync for BoxedFnConverter<L, R, EL, ER>

§

impl<L, R, EL, ER> Unpin for BoxedFnConverter<L, R, EL, ER>

§

impl<L, R, EL = Infallible, ER = Infallible> !UnwindSafe for BoxedFnConverter<L, R, EL, ER>

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> 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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.