Trait Converter

Source
pub trait Converter<L, R> {
    type ToLeftError;
    type ToRightError;

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

A trait for bidirectional conversion 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 = ParseIntError;
    type ToRightError = Infallible;

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

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

Required Associated Types§

Source

type ToLeftError

The error type returned when converting from right to left.

Source

type ToRightError

The error type returned when converting from left to right.

Required Methods§

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.

Implementors§

Source§

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

Source§

impl<L, R, EL, ER> Converter<L, R> for StdConverter
where for<'a> &'a L: TryInto<R, Error = ER>, for<'a> &'a R: TryInto<L, Error = EL>,

Source§

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