Trait Converter

Source
pub trait Converter<L, R> {
    type ToLeftError<'a>
       where R: 'a;
    type ToRightError<'a>
       where L: 'a;

    // Required methods
    fn convert_to_left<'a>(
        &self,
        right: &'a R,
    ) -> Result<L, Self::ToLeftError<'a>>;
    fn convert_to_right<'a>(
        &self,
        left: &'a L,
    ) -> Result<R, Self::ToRightError<'a>>;
}
Expand description

A trait for converting between two types. This trait is used by Pair to convert between its left and right values.

§Example

use cached_pair::Converter;
use std::convert::Infallible;
use std::num::ParseIntError;

struct MyConverter;

impl Converter<i32, String> for MyConverter {
    type ToLeftError<'a> = ParseIntError;
    type ToRightError<'a> = Infallible;

    fn convert_to_right(&self, left: &i32) -> Result<String, Self::ToRightError<'_>> {
        Ok(left.to_string())
    }

    fn convert_to_left(&self, right: &String) -> Result<i32, Self::ToLeftError<'_>> {
        right.parse()
    }
}

Required Associated Types§

Source

type ToLeftError<'a> where R: 'a

The error type returned when converting from right to left.

Source

type ToRightError<'a> where L: 'a

The error type returned when converting from left to right.

Required Methods§

Source

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

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

Source

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

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

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<L, R> Converter<L, R> for StdConverter<L, R>
where for<'a> &'a L: TryInto<R>, for<'a> &'a R: TryInto<L>,

Source§

type ToLeftError<'a> = <&'a R as TryInto<L>>::Error where R: 'a

Source§

type ToRightError<'a> = <&'a L as TryInto<R>>::Error where L: 'a

Source§

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

Source§

type ToLeftError<'a> = EL where R: 'a

Source§

type ToRightError<'a> = ER where L: 'a

Source§

impl<L, R, F, G, EL, ER> Converter<L, R> for FnConverter<L, R, F, G, EL, ER>
where for<'a> F: Fn(&'a R) -> Result<L, EL>, for<'a> G: Fn(&'a L) -> Result<R, ER>,

Source§

type ToLeftError<'a> = EL where R: 'a

Source§

type ToRightError<'a> = ER where L: 'a