use super::Result;
pub trait Conv<T>: Sized {
fn try_conv(v: T) -> Result<Self>;
fn conv(v: T) -> Self {
Self::try_conv(v).unwrap_or_else(|e| {
panic!("Conv::conv(_) failed: {}", e);
})
}
}
pub trait Cast<T> {
fn try_cast(self) -> Result<T>;
fn cast(self) -> T;
}
impl<S, T: Conv<S>> Cast<T> for S {
#[inline]
fn cast(self) -> T {
T::conv(self)
}
#[inline]
fn try_cast(self) -> Result<T> {
T::try_conv(self)
}
}
pub trait ConvApprox<T>: Sized {
fn try_conv_approx(x: T) -> Result<Self>;
#[inline]
fn conv_approx(x: T) -> Self {
Self::try_conv_approx(x).unwrap_or_else(|e| {
panic!("ConvApprox::conv_approx(_) failed: {}", e);
})
}
}
impl<S, T: Conv<S>> ConvApprox<S> for T {
#[inline]
fn try_conv_approx(x: S) -> Result<Self> {
T::try_conv(x)
}
#[inline]
fn conv_approx(x: S) -> Self {
T::conv(x)
}
}
pub trait CastApprox<T> {
fn try_cast_approx(self) -> Result<T>;
fn cast_approx(self) -> T;
}
impl<S, T: ConvApprox<S>> CastApprox<T> for S {
#[inline]
fn try_cast_approx(self) -> Result<T> {
T::try_conv_approx(self)
}
#[inline]
fn cast_approx(self) -> T {
T::conv_approx(self)
}
}
#[cfg(any(feature = "std", feature = "libm"))]
pub trait ConvFloat<T>: Sized {
fn try_conv_trunc(x: T) -> Result<Self>;
fn try_conv_nearest(x: T) -> Result<Self>;
fn try_conv_floor(x: T) -> Result<Self>;
fn try_conv_ceil(x: T) -> Result<Self>;
fn conv_trunc(x: T) -> Self {
Self::try_conv_trunc(x).unwrap_or_else(|e| panic!("ConvFloat::conv_trunc(_) failed: {}", e))
}
fn conv_nearest(x: T) -> Self {
Self::try_conv_nearest(x)
.unwrap_or_else(|e| panic!("ConvFloat::conv_nearest(_) failed: {}", e))
}
fn conv_floor(x: T) -> Self {
Self::try_conv_floor(x).unwrap_or_else(|e| panic!("ConvFloat::conv_floor(_) failed: {}", e))
}
fn conv_ceil(x: T) -> Self {
Self::try_conv_ceil(x).unwrap_or_else(|e| panic!("ConvFloat::conv_ceil(_) failed: {}", e))
}
}
#[cfg(any(feature = "std", feature = "libm"))]
pub trait CastFloat<T> {
fn cast_trunc(self) -> T;
fn cast_nearest(self) -> T;
fn cast_floor(self) -> T;
fn cast_ceil(self) -> T;
fn try_cast_trunc(self) -> Result<T>;
fn try_cast_nearest(self) -> Result<T>;
fn try_cast_floor(self) -> Result<T>;
fn try_cast_ceil(self) -> Result<T>;
}
#[cfg(any(feature = "std", feature = "libm"))]
impl<S, T: ConvFloat<S>> CastFloat<T> for S {
#[inline]
fn cast_trunc(self) -> T {
T::conv_trunc(self)
}
#[inline]
fn cast_nearest(self) -> T {
T::conv_nearest(self)
}
#[inline]
fn cast_floor(self) -> T {
T::conv_floor(self)
}
#[inline]
fn cast_ceil(self) -> T {
T::conv_ceil(self)
}
#[inline]
fn try_cast_trunc(self) -> Result<T> {
T::try_conv_trunc(self)
}
#[inline]
fn try_cast_nearest(self) -> Result<T> {
T::try_conv_nearest(self)
}
#[inline]
fn try_cast_floor(self) -> Result<T> {
T::try_conv_floor(self)
}
#[inline]
fn try_cast_ceil(self) -> Result<T> {
T::try_conv_ceil(self)
}
}