#[cfg(all(feature = "num-traits", feature = "std"))]
mod inner {
use num_traits::Float;
pub trait Scalar: Float + core::fmt::Debug + Copy + 'static {
fn zero() -> Self;
fn one() -> Self;
fn from_usize(n: usize) -> Self;
}
impl<T: Float + core::fmt::Debug + Copy + 'static> Scalar for T {
fn zero() -> Self {
T::zero()
}
fn one() -> Self {
T::one()
}
fn from_usize(n: usize) -> Self {
num_traits::NumCast::from(n).unwrap_or(Self::one())
}
}
}
#[cfg(all(feature = "num-traits", not(feature = "std")))]
mod inner {
use num_traits::float::FloatCore;
pub trait Scalar: FloatCore + core::fmt::Debug + Copy + 'static {
fn zero() -> Self;
fn one() -> Self;
fn from_usize(n: usize) -> Self;
}
impl<T: FloatCore + core::fmt::Debug + Copy + 'static> Scalar for T {
fn zero() -> Self {
T::zero()
}
fn one() -> Self {
T::one()
}
fn from_usize(n: usize) -> Self {
num_traits::NumCast::from(n).unwrap_or(Self::one())
}
}
}
#[cfg(not(feature = "num-traits"))]
mod inner {
pub trait Scalar:
core::ops::Add<Output = Self>
+ core::ops::Sub<Output = Self>
+ core::ops::Mul<Output = Self>
+ core::ops::Div<Output = Self>
+ core::ops::Neg<Output = Self>
+ PartialOrd
+ core::fmt::Debug
+ Copy
+ 'static
{
fn zero() -> Self;
fn one() -> Self;
fn from_usize(n: usize) -> Self;
}
macro_rules! impl_scalar {
($($t:ty),+) => {
$(
impl Scalar for $t {
fn zero() -> Self { 0.0 }
fn one() -> Self { 1.0 }
fn from_usize(n: usize) -> Self { n as $t }
}
)+
};
}
impl_scalar!(f32, f64);
}
pub use inner::Scalar;