pub trait Number: Copy + Default {
// Required methods
fn parse(literal: &str) -> Option<Self>;
fn add(self, rhs: Self) -> Result<Self, ErrorKind>;
fn subtract(self, rhs: Self) -> Result<Self, ErrorKind>;
fn multiply(self, rhs: Self) -> Result<Self, ErrorKind>;
fn divide(self, rhs: Self) -> Result<Self, ErrorKind>;
fn remainder(self, rhs: Self) -> Result<Self, ErrorKind>;
fn power(self, rhs: Self) -> Result<Self, ErrorKind>;
fn negate(self) -> Result<Self, ErrorKind>;
// Provided methods
fn compare(self, rhs: Self) -> Result<Ordering, ErrorKind> { ... }
fn is_zero(self) -> Result<bool, ErrorKind> { ... }
fn from_bool(value: bool) -> Result<Self, ErrorKind> { ... }
fn factorial(self) -> Result<Self, ErrorKind> { ... }
}Expand description
A value type expressions are evaluated in.
Implemented for f32 and f64 with the libm feature, and for every primitive integer type. Implement it for
your own type, such as a fixed-point number, to evaluate expressions in it.
Each operation returns the ErrorKind to report instead of producing a silent NaN, infinity or wrapped value.
Comparisons (<, ==…), logic operators (&&, ||, !, ? :) and factorials (n!) need
Number::compare, Number::is_zero and Number::from_bool. They have default implementations, so a type
that only implements arithmetic keeps working: override Number::compare to support comparisons, logic and
factorials, as the others are derived from it.
Required Methods§
Sourcefn parse(literal: &str) -> Option<Self>
fn parse(literal: &str) -> Option<Self>
Parses a literal such as 42, 3.5 or 1e3, or returns None if the type cannot represent it.
Provided Methods§
Sourcefn compare(self, rhs: Self) -> Result<Ordering, ErrorKind>
fn compare(self, rhs: Self) -> Result<Ordering, ErrorKind>
Returns how self orders against rhs.
Return an error for values that cannot be ordered, such as ErrorKind::Domain for a float NaN. The
default returns ErrorKind::Unsupported.
Sourcefn is_zero(self) -> Result<bool, ErrorKind>
fn is_zero(self) -> Result<bool, ErrorKind>
Returns whether self is zero, which logic operators read as false.
The default compares self with Default::default, which it assumes to be zero.
Sourcefn from_bool(value: bool) -> Result<Self, ErrorKind>
fn from_bool(value: bool) -> Result<Self, ErrorKind>
Returns 1 for true and 0 for false, the results of comparisons and logic operators.
The default parses the literals 1 and 0, and returns ErrorKind::Unsupported if that fails.
Sourcefn factorial(self) -> Result<Self, ErrorKind>
fn factorial(self) -> Result<Self, ErrorKind>
Returns the factorial self!, the product 1 * 2 * ... * self, with 0! = 1.
Return ErrorKind::Domain when self is negative or not an integer, and ErrorKind::Overflow when
the result does not fit. The default checks that self is a non-negative integer with Number::compare
and a Number::remainder by one, then counts from zero to self with Number::add, multiplying as it
goes, so it needs Number::compare and rounds each product as Number::multiply does. It gives up
with ErrorKind::Overflow after 10 000 multiplications, so a type with a huge or unbounded range cannot
compute for ever. Override it to round only once, as Decimal does.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".