NumLike

Trait NumLike 

Source
pub trait NumLike:
    Add<Output = Self>
    + AddAssign
    + Sub<Output = Self>
    + SubAssign
    + Mul<Output = Self>
    + MulAssign
    + Div<Output = Self>
    + DivAssign
    + Neg<Output = Self>
    + Clone
    + Debug
    + Display { }
Expand description

The NumLike trait is just a shorthand definition for any “number-like” type in Rust. “Number-like” means that a type implements the traits for standard arithmatic (Add, Sub, Mul, Div, AddAssign, SubAssign, MulAssign, DivAssign, and Neg), plus Clone, Debug, and Display. Most number types also implement the Copy marker trait, but that is not required (for example, an arbitrary-precision number type must dynamically allocate memory and thus cannot implement Copy).

This trait is not meant to be implemented, just for making generic type templates more ergonomic. E.g.

use simple_si_units::NumLike;
 
fn delta_squared<T>(a: T, b: T) -> T where T: NumLike {
  let delta = b - a;
  return delta.clone() * delta;
}

The NumLike trait is just a shorthand definition for any “number-like” type in Rust. “Number-like” means that a type implements the traits for standard arithmatic (Add, Sub, Mul, Div, AddAssign, SubAssign, MulAssign, DivAssign, and Neg), plus Clone, Debug, and Display. Most number types also implement the Copy marker trait, but that is not required (for example, an arbitrary-precision number type must dynamically allocate memory and thus cannot implement Copy).

This trait is not meant to be implemented, just for making generic type templates more ergonomic. E.g.

use simple_si_units_core::NumLike;
 
fn delta_squared<T>(a: T, b: T) -> T where T: NumLike {
  let delta = b - a;
  return delta.clone() * delta;
}

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<T> NumLike for T
where T: Add<Output = T> + SubAssign + Mul<Output = T> + MulAssign + Div<Output = T> + AddAssign + DivAssign + Neg<Output = T> + Sub<Output = T> + Clone + Debug + Display,