lineic

Trait Numeric

Source
pub trait Numeric:
    Copy
    + PartialOrd
    + Debug
    + Display {
    const MAX: Self;
    const ZERO: Self;
    const ONE: Self;

    // Required methods
    fn abs(self) -> Self;
    fn clamp(self, min: Self, max: Self) -> Self;
    fn checked_sub(self, other: Self) -> Option<Self>;
    fn checked_add(self, other: Self) -> Option<Self>;
    fn checked_mul(self, other: Self) -> Option<Self>;
    fn checked_div(self, other: Self) -> Option<Self>;
    fn from_usize(value: usize) -> Option<Self>;
    fn into_f64(self) -> f64;
    fn from_f64(value: f64) -> Option<Self>;

    // Provided methods
    fn abs_diff(self, other: Self) -> Self { ... }
    fn scale(self, factor: impl Numeric) -> Option<Self> { ... }
}
Expand description

Represents a numeric type that can be interpolated across By default, implemented for:

  • f32 f64
  • i8 i16 i32 i64 i128 isize
  • u8 u16 u32 u64 u128 usize

Required Associated Constants§

Source

const MAX: Self

The maximum value for this type

Source

const ZERO: Self

The zero value for this type

Source

const ONE: Self

The first whole value after zero for this type

Required Methods§

Source

fn abs(self) -> Self

Get the absolute value of this number

Source

fn clamp(self, min: Self, max: Self) -> Self

Clamp this number between a minimum and maximum value.
Differs from std::cmp::Ord::clamp in that it must handle cases where min > max

Source

fn checked_sub(self, other: Self) -> Option<Self>

Subtract another number from this one, returning None if the operation would overflow

Source

fn checked_add(self, other: Self) -> Option<Self>

Add two numbers together, returning None if the operation would overflow

Source

fn checked_mul(self, other: Self) -> Option<Self>

Multiply two numbers together, returning None if the operation would overflow

Source

fn checked_div(self, other: Self) -> Option<Self>

Divide two numbers, returning None if the operation would overflow

Source

fn from_usize(value: usize) -> Option<Self>

Convert a usize to this type

Source

fn into_f64(self) -> f64

Convert this number to an f64

Source

fn from_f64(value: f64) -> Option<Self>

Convert an f64 to this type

Provided Methods§

Source

fn abs_diff(self, other: Self) -> Self

Get the distance between two numbers, always as a positive value

Source

fn scale(self, factor: impl Numeric) -> Option<Self>

Scale this number by a factor of a different numeric type.

The built-in implementation uses f64 as a common go-between for scaling

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.

Implementations on Foreign Types§

Source§

impl Numeric for f32

Source§

const MAX: Self = 3.40282347E+38f32

Source§

const ZERO: Self = 0f32

Source§

const ONE: Self = 1f32

Source§

fn abs(self) -> Self

Source§

fn clamp(self, min: Self, max: Self) -> Self

Source§

fn checked_sub(self, other: Self) -> Option<Self>

Source§

fn checked_add(self, other: Self) -> Option<Self>

Source§

fn checked_mul(self, other: Self) -> Option<Self>

Source§

fn checked_div(self, other: Self) -> Option<Self>

Source§

fn from_usize(value: usize) -> Option<Self>

Source§

fn into_f64(self) -> f64

Source§

fn from_f64(value: f64) -> Option<Self>

Source§

impl Numeric for f64

Source§

const MAX: Self = 1.7976931348623157E+308f64

Source§

const ZERO: Self = 0f64

Source§

const ONE: Self = 1f64

Source§

fn abs(self) -> Self

Source§

fn clamp(self, min: Self, max: Self) -> Self

Source§

fn checked_sub(self, other: Self) -> Option<Self>

Source§

fn checked_add(self, other: Self) -> Option<Self>

Source§

fn checked_mul(self, other: Self) -> Option<Self>

Source§

fn checked_div(self, other: Self) -> Option<Self>

Source§

fn from_usize(value: usize) -> Option<Self>

Source§

fn into_f64(self) -> f64

Source§

fn from_f64(value: f64) -> Option<Self>

Source§

impl Numeric for i8

Source§

const MAX: Self = 127i8

Source§

const ZERO: Self = 0i8

Source§

const ONE: Self = 1i8

Source§

fn abs(self) -> Self

Source§

fn clamp(self, min: Self, max: Self) -> Self

Source§

fn checked_sub(self, other: Self) -> Option<Self>

Source§

fn checked_add(self, other: Self) -> Option<Self>

Source§

fn checked_mul(self, other: Self) -> Option<Self>

Source§

fn checked_div(self, other: Self) -> Option<Self>

Source§

fn from_usize(value: usize) -> Option<Self>

Source§

fn into_f64(self) -> f64

Source§

fn from_f64(value: f64) -> Option<Self>

Source§

impl Numeric for i16

Source§

const MAX: Self = 32_767i16

Source§

const ZERO: Self = 0i16

Source§

const ONE: Self = 1i16

Source§

fn abs(self) -> Self

Source§

fn clamp(self, min: Self, max: Self) -> Self

Source§

fn checked_sub(self, other: Self) -> Option<Self>

Source§

fn checked_add(self, other: Self) -> Option<Self>

Source§

fn checked_mul(self, other: Self) -> Option<Self>

Source§

fn checked_div(self, other: Self) -> Option<Self>

Source§

fn from_usize(value: usize) -> Option<Self>

Source§

fn into_f64(self) -> f64

Source§

fn from_f64(value: f64) -> Option<Self>

Source§

impl Numeric for i32

Source§

const MAX: Self = 2_147_483_647i32

Source§

const ZERO: Self = 0i32

Source§

const ONE: Self = 1i32

Source§

fn abs(self) -> Self

Source§

fn clamp(self, min: Self, max: Self) -> Self

Source§

fn checked_sub(self, other: Self) -> Option<Self>

Source§

fn checked_add(self, other: Self) -> Option<Self>

Source§

fn checked_mul(self, other: Self) -> Option<Self>

Source§

fn checked_div(self, other: Self) -> Option<Self>

Source§

fn from_usize(value: usize) -> Option<Self>

Source§

fn into_f64(self) -> f64

Source§

fn from_f64(value: f64) -> Option<Self>

Source§

impl Numeric for i64

Source§

const MAX: Self = 9_223_372_036_854_775_807i64

Source§

const ZERO: Self = 0i64

Source§

const ONE: Self = 1i64

Source§

fn abs(self) -> Self

Source§

fn clamp(self, min: Self, max: Self) -> Self

Source§

fn checked_sub(self, other: Self) -> Option<Self>

Source§

fn checked_add(self, other: Self) -> Option<Self>

Source§

fn checked_mul(self, other: Self) -> Option<Self>

Source§

fn checked_div(self, other: Self) -> Option<Self>

Source§

fn from_usize(value: usize) -> Option<Self>

Source§

fn into_f64(self) -> f64

Source§

fn from_f64(value: f64) -> Option<Self>

Source§

impl Numeric for i128

Source§

const MAX: Self = 170_141_183_460_469_231_731_687_303_715_884_105_727i128

Source§

const ZERO: Self = 0i128

Source§

const ONE: Self = 1i128

Source§

fn abs(self) -> Self

Source§

fn clamp(self, min: Self, max: Self) -> Self

Source§

fn checked_sub(self, other: Self) -> Option<Self>

Source§

fn checked_add(self, other: Self) -> Option<Self>

Source§

fn checked_mul(self, other: Self) -> Option<Self>

Source§

fn checked_div(self, other: Self) -> Option<Self>

Source§

fn from_usize(value: usize) -> Option<Self>

Source§

fn into_f64(self) -> f64

Source§

fn from_f64(value: f64) -> Option<Self>

Source§

impl Numeric for isize

Source§

const MAX: Self = 2_147_483_647isize

Source§

const ZERO: Self = 0isize

Source§

const ONE: Self = 1isize

Source§

fn abs(self) -> Self

Source§

fn clamp(self, min: Self, max: Self) -> Self

Source§

fn checked_sub(self, other: Self) -> Option<Self>

Source§

fn checked_add(self, other: Self) -> Option<Self>

Source§

fn checked_mul(self, other: Self) -> Option<Self>

Source§

fn checked_div(self, other: Self) -> Option<Self>

Source§

fn from_usize(value: usize) -> Option<Self>

Source§

fn into_f64(self) -> f64

Source§

fn from_f64(value: f64) -> Option<Self>

Source§

impl Numeric for u8

Source§

const MAX: Self = 255u8

Source§

const ZERO: Self = 0u8

Source§

const ONE: Self = 1u8

Source§

fn abs(self) -> Self

Source§

fn clamp(self, min: Self, max: Self) -> Self

Source§

fn checked_sub(self, other: Self) -> Option<Self>

Source§

fn checked_add(self, other: Self) -> Option<Self>

Source§

fn checked_mul(self, other: Self) -> Option<Self>

Source§

fn checked_div(self, other: Self) -> Option<Self>

Source§

fn from_usize(value: usize) -> Option<Self>

Source§

fn into_f64(self) -> f64

Source§

fn from_f64(value: f64) -> Option<Self>

Source§

impl Numeric for u16

Source§

const MAX: Self = 65_535u16

Source§

const ZERO: Self = 0u16

Source§

const ONE: Self = 1u16

Source§

fn abs(self) -> Self

Source§

fn clamp(self, min: Self, max: Self) -> Self

Source§

fn checked_sub(self, other: Self) -> Option<Self>

Source§

fn checked_add(self, other: Self) -> Option<Self>

Source§

fn checked_mul(self, other: Self) -> Option<Self>

Source§

fn checked_div(self, other: Self) -> Option<Self>

Source§

fn from_usize(value: usize) -> Option<Self>

Source§

fn into_f64(self) -> f64

Source§

fn from_f64(value: f64) -> Option<Self>

Source§

impl Numeric for u32

Source§

const MAX: Self = 4_294_967_295u32

Source§

const ZERO: Self = 0u32

Source§

const ONE: Self = 1u32

Source§

fn abs(self) -> Self

Source§

fn clamp(self, min: Self, max: Self) -> Self

Source§

fn checked_sub(self, other: Self) -> Option<Self>

Source§

fn checked_add(self, other: Self) -> Option<Self>

Source§

fn checked_mul(self, other: Self) -> Option<Self>

Source§

fn checked_div(self, other: Self) -> Option<Self>

Source§

fn from_usize(value: usize) -> Option<Self>

Source§

fn into_f64(self) -> f64

Source§

fn from_f64(value: f64) -> Option<Self>

Source§

impl Numeric for u64

Source§

const MAX: Self = 18_446_744_073_709_551_615u64

Source§

const ZERO: Self = 0u64

Source§

const ONE: Self = 1u64

Source§

fn abs(self) -> Self

Source§

fn clamp(self, min: Self, max: Self) -> Self

Source§

fn checked_sub(self, other: Self) -> Option<Self>

Source§

fn checked_add(self, other: Self) -> Option<Self>

Source§

fn checked_mul(self, other: Self) -> Option<Self>

Source§

fn checked_div(self, other: Self) -> Option<Self>

Source§

fn from_usize(value: usize) -> Option<Self>

Source§

fn into_f64(self) -> f64

Source§

fn from_f64(value: f64) -> Option<Self>

Source§

impl Numeric for u128

Source§

const MAX: Self = 340_282_366_920_938_463_463_374_607_431_768_211_455u128

Source§

const ZERO: Self = 0u128

Source§

const ONE: Self = 1u128

Source§

fn abs(self) -> Self

Source§

fn clamp(self, min: Self, max: Self) -> Self

Source§

fn checked_sub(self, other: Self) -> Option<Self>

Source§

fn checked_add(self, other: Self) -> Option<Self>

Source§

fn checked_mul(self, other: Self) -> Option<Self>

Source§

fn checked_div(self, other: Self) -> Option<Self>

Source§

fn from_usize(value: usize) -> Option<Self>

Source§

fn into_f64(self) -> f64

Source§

fn from_f64(value: f64) -> Option<Self>

Source§

impl Numeric for usize

Source§

const MAX: Self = 4_294_967_295usize

Source§

const ZERO: Self = 0usize

Source§

const ONE: Self = 1usize

Source§

fn abs(self) -> Self

Source§

fn clamp(self, min: Self, max: Self) -> Self

Source§

fn checked_sub(self, other: Self) -> Option<Self>

Source§

fn checked_add(self, other: Self) -> Option<Self>

Source§

fn checked_mul(self, other: Self) -> Option<Self>

Source§

fn checked_div(self, other: Self) -> Option<Self>

Source§

fn from_usize(value: usize) -> Option<Self>

Source§

fn into_f64(self) -> f64

Source§

fn from_f64(value: f64) -> Option<Self>

Implementors§