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§
Sourcetype ToLeftError
type ToLeftError
The error type returned when converting from right to left.
Sourcetype ToRightError
type ToRightError
The error type returned when converting from left to right.
Required Methods§
Sourcefn convert_to_left(&self, right: &R) -> Result<L, Self::ToLeftError>
fn convert_to_left(&self, right: &R) -> Result<L, Self::ToLeftError>
Converts a reference to a right value into a left value.
Sourcefn convert_to_right(&self, left: &L) -> Result<R, Self::ToRightError>
fn convert_to_right(&self, left: &L) -> Result<R, Self::ToRightError>
Converts a reference to a left value into a right value.