//! A crate that allows declaring an equality bound on two types and coercing between them. //! //! # Examples //! //! ``` //! # use eq_type::Is; //! # //! pub trait ResultExt: Sized + Is> { //! /// Given any `E` and `EI` that implement `Into`, converts from Result, E> to Result. //! fn flatten_into(self) -> Result //! where //! T: Is>, //! E: Into, //! EI: Into, //! { //! self.coerce().map_err(|e| e.into()).and_then(|x| x.coerce().map_err(|e| e.into())) //! } //! } //! # //! # fn main() { } //! ``` mod private { pub trait Sealed {} } pub trait Is: private::Sealed { fn coerce(self) -> Rhs where Self: Sized, Rhs: Sized; fn rcoerce(rhs: Rhs) -> Self where Self: Sized, Rhs: Sized; } impl private::Sealed for T {} impl Is for T { #[inline(always)] fn coerce(self) -> T where T: Sized, { self } #[inline(always)] fn rcoerce(t: T) -> T where T: Sized, { t } } #[cfg(test)] mod tests { use super::Is; fn forward(t: T) -> U where T: Is, { t.coerce() } fn backward(u: U) -> T where U: Is, { u.coerce() } #[test] fn test() { let mut x = 4; x = forward(x); x = backward(x); x = backward(forward(x)); x = forward(backward(x)); assert_eq!(x, 4); } }