1 pub trait TryFromWithErr<T, E>: Sized { try_from_with_err(value: T, error: E) -> Result<Self, E>2 fn try_from_with_err(value: T, error: E) -> Result<Self, E>; 3 } 4 5 impl<T, U, E> TryFromWithErr<U, E> for T 6 where 7 T: TryFrom<U>, 8 { try_from_with_err(value: U, error: E) -> Result<T, E>9 fn try_from_with_err(value: U, error: E) -> Result<T, E> { 10 T::try_from(value).map_err(|_| error) 11 } 12 } 13 14 pub trait TryIntoWithErr<T, E>: Sized { try_into_with_err(self, error: E) -> Result<T, E>15 fn try_into_with_err(self, error: E) -> Result<T, E>; 16 } 17 18 impl<T, U, E> TryIntoWithErr<T, E> for U 19 where 20 T: TryFromWithErr<U, E>, 21 { try_into_with_err(self, error: E) -> Result<T, E>22 fn try_into_with_err(self, error: E) -> Result<T, E> { 23 T::try_from_with_err(self, error) 24 } 25 } 26