use std::error::Error;
use std::fmt;
pub trait TryInto<T>: Sized {
type Error;
fn try_into(self) -> Result<T, Self::Error>;
}
pub trait TryFrom<T>: Sized {
type Error;
fn try_from(value: T) -> Result<Self, Self::Error>;
}
impl<T, U> TryInto<U> for T
where
U: TryFrom<T>,
{
type Error = U::Error;
fn try_into(self) -> Result<U, U::Error> {
U::try_from(self)
}
}
#[derive(Debug, Copy, Clone)]
pub struct TryFromIntError(());
impl TryFromIntError {
pub fn __description(&self) -> &str {
"out of range integral type conversion attempted"
}
}
impl Error for TryFromIntError {
fn description(&self) -> &str {
self.__description()
}
}
impl fmt::Display for TryFromIntError {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
self.__description().fmt(fmt)
}
}
impl TryFrom<usize> for u32 {
type Error = TryFromIntError;
fn try_from(u: usize) -> Result<Self, TryFromIntError> {
if u > (<Self>::max_value() as usize) {
Err(TryFromIntError(()))
} else {
Ok(u as Self)
}
}
}