use crate::{ConvExact, ConvTo, Error, RangeError};
use core::convert::Infallible;
pub trait Rounding: Copy + Default {
type MaximumError: From<Infallible> + Into<Error> + core::error::Error;
}
#[derive(Clone, Copy, Debug, Default)]
pub struct Exact;
impl Rounding for Exact {
type MaximumError = Error;
}
#[derive(Clone, Copy, Debug, Default)]
pub struct Approx;
impl Rounding for Approx {
type MaximumError = RangeError;
}
#[derive(Clone, Copy, Debug, Default)]
pub struct Trunc;
impl Rounding for Trunc {
type MaximumError = RangeError;
}
#[cfg(any(feature = "std", feature = "libm"))]
#[derive(Clone, Copy, Debug, Default)]
pub struct Nearest;
#[cfg(any(feature = "std", feature = "libm"))]
impl Rounding for Nearest {
type MaximumError = RangeError;
}
#[cfg(any(feature = "std", feature = "libm"))]
#[derive(Clone, Copy, Debug, Default)]
pub struct Floor;
#[cfg(any(feature = "std", feature = "libm"))]
impl Rounding for Floor {
type MaximumError = RangeError;
}
#[cfg(any(feature = "std", feature = "libm"))]
#[derive(Clone, Copy, Debug, Default)]
pub struct Ceil;
#[cfg(any(feature = "std", feature = "libm"))]
impl Rounding for Ceil {
type MaximumError = RangeError;
}
impl<S, T: ConvExact<S>> ConvTo<S, Exact> for T {
type Error = T::Error;
#[inline]
fn try_conv_to(_: Exact, s: S) -> Result<Self, Self::Error> {
T::try_conv_exact(s)
}
#[inline]
fn conv_to(_: Exact, s: S) -> Self {
T::conv_exact(s)
}
}
impl<S, T: ConvExact<S>> ConvTo<S, Approx> for T {
type Error = T::Error;
#[inline]
fn try_conv_to(_: Approx, s: S) -> Result<Self, Self::Error> {
T::try_conv_exact(s)
}
#[inline]
fn conv_to(_: Approx, s: S) -> Self {
T::conv_exact(s)
}
}
#[cfg(any(feature = "std", feature = "libm"))]
impl<S, T: ConvExact<S>> ConvTo<S, Trunc> for T {
type Error = T::Error;
#[inline]
fn try_conv_to(_: Trunc, s: S) -> Result<Self, Self::Error> {
T::try_conv_exact(s)
}
#[inline]
fn conv_to(_: Trunc, s: S) -> Self {
T::conv_exact(s)
}
}
#[cfg(any(feature = "std", feature = "libm"))]
impl<S, T: ConvExact<S>> ConvTo<S, Nearest> for T {
type Error = T::Error;
#[inline]
fn try_conv_to(_: Nearest, s: S) -> Result<Self, Self::Error> {
T::try_conv_exact(s)
}
#[inline]
fn conv_to(_: Nearest, s: S) -> Self {
T::conv_exact(s)
}
}
#[cfg(any(feature = "std", feature = "libm"))]
impl<S, T: ConvExact<S>> ConvTo<S, Floor> for T {
type Error = T::Error;
#[inline]
fn try_conv_to(_: Floor, s: S) -> Result<Self, Self::Error> {
T::try_conv_exact(s)
}
#[inline]
fn conv_to(_: Floor, s: S) -> Self {
T::conv_exact(s)
}
}
#[cfg(any(feature = "std", feature = "libm"))]
impl<S, T: ConvExact<S>> ConvTo<S, Ceil> for T {
type Error = T::Error;
#[inline]
fn try_conv_to(_: Ceil, s: S) -> Result<Self, Self::Error> {
T::try_conv_exact(s)
}
#[inline]
fn conv_to(_: Ceil, s: S) -> Self {
T::conv_exact(s)
}
}