pub trait Cast<T> {
fn cast(self) -> T;
}
pub trait CastFrom<T> {
fn cast_from(value: T) -> Self;
}
pub trait LossyCast<T> {
fn lossy_cast(self) -> T;
}
pub trait LossyCastFrom<T> {
fn lossy_cast_from(value: T) -> Self;
}
pub trait SaturatingCast<T> {
fn saturating_cast(self) -> T;
}
pub trait SaturatingCastFrom<T> {
fn saturating_cast_from(value: T) -> Self;
}
pub trait SignedCast<T> {
fn signed_cast(self) -> T;
}
pub trait SignedCastFrom<T> {
fn signed_cast_from(value: T) -> Self;
}
pub trait UnsignedCast<T> {
fn unsigned_cast(self) -> T;
}
pub trait UnsignedCastFrom<T> {
fn unsigned_cast_from(value: T) -> Self;
}
pub trait TryExactCast<T> {
fn try_exact_cast(self) -> Result<T, CastError>;
}
pub trait TryExactCastFrom<T>
where
Self: Sized,
{
fn try_exact_cast_from(value: T) -> Result<Self, CastError>;
}
pub trait TryCast<T> {
fn try_cast(self) -> Result<T, CastError>;
}
pub trait TryCastFrom<T>
where
Self: Sized,
{
fn try_cast_from(value: T) -> Result<Self, CastError>;
}
#[derive(Debug, PartialEq)]
pub enum CastError {
OutOfRange,
Fractional,
NonFinite,
Inexact,
}
impl<T, U> CastFrom<T> for U
where
T: Cast<U>,
{
#[inline]
fn cast_from(value: T) -> Self {
value.cast()
}
}
impl<T, U> LossyCastFrom<T> for U
where
T: LossyCast<U>,
{
#[inline]
fn lossy_cast_from(value: T) -> Self {
value.lossy_cast()
}
}
impl<T, U> SaturatingCastFrom<T> for U
where
T: SaturatingCast<U>,
{
#[inline]
fn saturating_cast_from(value: T) -> Self {
value.saturating_cast()
}
}
impl<T, U> SignedCastFrom<T> for U
where
T: SignedCast<U>,
{
#[inline]
fn signed_cast_from(value: T) -> Self {
value.signed_cast()
}
}
impl<T, U> UnsignedCastFrom<T> for U
where
T: UnsignedCast<U>,
{
#[inline]
fn unsigned_cast_from(value: T) -> Self {
value.unsigned_cast()
}
}
impl<T, U> TryCastFrom<T> for U
where
T: TryCast<U>,
{
#[inline]
fn try_cast_from(value: T) -> Result<Self, CastError> {
value.try_cast()
}
}
impl<T, U> TryExactCastFrom<T> for U
where
T: TryExactCast<U>,
{
#[inline]
fn try_exact_cast_from(value: T) -> Result<Self, CastError> {
value.try_exact_cast()
}
}