pub trait Num {
type Inner;
type Out;
type Rhs;
Show 35 methods
// Required method
fn num_into(self) -> Self::Inner;
// Provided methods
fn num_from(value: Self::Inner) -> Result<Self>
where Self: Sized { ... }
fn num_from_ref(value: &Self::Inner) -> Result<Self>
where Self: Sized { ... }
fn num_set(&mut self, value: Self::Inner) -> Result<()> { ... }
fn num_set_ref(&mut self, value: &Self::Inner) -> Result<()> { ... }
fn num_is_zero(&self) -> Result<bool> { ... }
fn num_get_zero() -> Result<Self>
where Self: Sized { ... }
fn num_set_zero(&mut self) -> Result<()> { ... }
fn num_is_one(&self) -> Result<bool> { ... }
fn num_get_one() -> Result<Self>
where Self: Sized { ... }
fn num_set_one(&mut self) -> Result<()> { ... }
fn num_add(self, rhs: Self::Rhs) -> Result<Self::Out>
where Self: Sized { ... }
fn num_add_ref(self, rhs: &Self::Rhs) -> Result<Self::Out>
where Self: Sized { ... }
fn num_ref_add(&self, rhs: Self::Rhs) -> Result<Self::Out>
where Self: Sized { ... }
fn num_ref_add_ref(&self, rhs: &Self::Rhs) -> Result<Self::Out>
where Self: Sized { ... }
fn num_sub(self, rhs: Self::Rhs) -> Result<Self::Out>
where Self: Sized { ... }
fn num_sub_ref(self, rhs: &Self::Rhs) -> Result<Self::Out>
where Self: Sized { ... }
fn num_ref_sub(&self, rhs: Self::Rhs) -> Result<Self::Out>
where Self: Sized { ... }
fn num_ref_sub_ref(&self, rhs: &Self::Rhs) -> Result<Self::Out>
where Self: Sized { ... }
fn num_mul(self, rhs: Self::Rhs) -> Result<Self::Out>
where Self: Sized { ... }
fn num_mul_ref(self, rhs: &Self::Rhs) -> Result<Self::Out>
where Self: Sized { ... }
fn num_ref_mul(&self, rhs: Self::Rhs) -> Result<Self::Out>
where Self: Sized { ... }
fn num_ref_mul_ref(&self, rhs: &Self::Rhs) -> Result<Self::Out>
where Self: Sized { ... }
fn num_div(self, rhs: Self::Rhs) -> Result<Self::Out>
where Self: Sized { ... }
fn num_div_ref(self, rhs: &Self::Rhs) -> Result<Self::Out>
where Self: Sized { ... }
fn num_ref_div(&self, rhs: Self::Rhs) -> Result<Self::Out>
where Self: Sized { ... }
fn num_ref_div_ref(&self, rhs: &Self::Rhs) -> Result<Self::Out>
where Self: Sized { ... }
fn num_rem(self, rhs: Self::Rhs) -> Result<Self::Out>
where Self: Sized { ... }
fn num_rem_ref(self, rhs: &Self::Rhs) -> Result<Self::Out>
where Self: Sized { ... }
fn num_ref_rem(&self, rhs: Self::Rhs) -> Result<Self::Out>
where Self: Sized { ... }
fn num_ref_rem_ref(&self, rhs: &Self::Rhs) -> Result<Self::Out>
where Self: Sized { ... }
fn num_neg(self) -> Result<Self::Out>
where Self: Sized { ... }
fn num_ref_neg(&self) -> Result<Self::Out>
where Self: Sized { ... }
fn num_abs(self) -> Result<Self::Out>
where Self: Sized { ... }
fn num_ref_abs(&self) -> Result<Self::Out>
where Self: Sized { ... }
}num only.Expand description
Common trait for numeric types.
This trait doesn’t depend on having any operations implemented, and it
offers a common numeric API that returns NotImplemented
by default unless the methods are overriden.
Any concrete implementation must manually implement the operations it wants.
You could also ask for additional bounds like e.g. Add.
Binary operations offer two alternative methods, one for when you want to
transfer ownership of the second element, and another one for when you don’t.
Transferring ownership is more efficient for Copy types, and using a
reference is more appropriate for non-copy types.
For the default implementations we try to always offer a meaningful result, even if the concrete type doesn’t support it directly, we do the operation on the underlying primitive and try to construct the new type again.
The standard library offers different methods for signed and unsigned types, (e.g. abs, neg), and some are lacking for non-zero types (div, sub). This trait try to offer the same methods everywhere and give a result when a result is possible.
See also NumRef that is intended to be implemented for Num references.
Required Associated Types§
Required Methods§
Provided Methods§
sourcefn num_from(value: Self::Inner) -> Result<Self>where
Self: Sized,
fn num_from(value: Self::Inner) -> Result<Self>where
Self: Sized,
Returns Self if given a valid value.
sourcefn num_from_ref(value: &Self::Inner) -> Result<Self>where
Self: Sized,
fn num_from_ref(value: &Self::Inner) -> Result<Self>where
Self: Sized,
Returns Self if given a valid &value.
sourcefn num_set_ref(&mut self, value: &Self::Inner) -> Result<()>
fn num_set_ref(&mut self, value: &Self::Inner) -> Result<()>
Sets self to the given valid &value.
sourcefn num_is_zero(&self) -> Result<bool>
fn num_is_zero(&self) -> Result<bool>
Returns true if self is zero.
sourcefn num_get_zero() -> Result<Self>where
Self: Sized,
fn num_get_zero() -> Result<Self>where
Self: Sized,
Returns the number zero.
sourcefn num_set_zero(&mut self) -> Result<()>
fn num_set_zero(&mut self) -> Result<()>
Sets self to 0.
sourcefn num_is_one(&self) -> Result<bool>
fn num_is_one(&self) -> Result<bool>
Returns true if self is one.
sourcefn num_get_one() -> Result<Self>where
Self: Sized,
fn num_get_one() -> Result<Self>where
Self: Sized,
Returns the number one.
sourcefn num_set_one(&mut self) -> Result<()>
fn num_set_one(&mut self) -> Result<()>
Sets the number to one.
sourcefn num_add_ref(self, rhs: &Self::Rhs) -> Result<Self::Out>where
Self: Sized,
fn num_add_ref(self, rhs: &Self::Rhs) -> Result<Self::Out>where
Self: Sized,
Computes self + &other.
sourcefn num_ref_add(&self, rhs: Self::Rhs) -> Result<Self::Out>where
Self: Sized,
fn num_ref_add(&self, rhs: Self::Rhs) -> Result<Self::Out>where
Self: Sized,
Computes &self + other.
sourcefn num_ref_add_ref(&self, rhs: &Self::Rhs) -> Result<Self::Out>where
Self: Sized,
fn num_ref_add_ref(&self, rhs: &Self::Rhs) -> Result<Self::Out>where
Self: Sized,
Computes &self + &other.
sourcefn num_sub_ref(self, rhs: &Self::Rhs) -> Result<Self::Out>where
Self: Sized,
fn num_sub_ref(self, rhs: &Self::Rhs) -> Result<Self::Out>where
Self: Sized,
Computes self - &other.
sourcefn num_ref_sub(&self, rhs: Self::Rhs) -> Result<Self::Out>where
Self: Sized,
fn num_ref_sub(&self, rhs: Self::Rhs) -> Result<Self::Out>where
Self: Sized,
Computes &self - other.
sourcefn num_ref_sub_ref(&self, rhs: &Self::Rhs) -> Result<Self::Out>where
Self: Sized,
fn num_ref_sub_ref(&self, rhs: &Self::Rhs) -> Result<Self::Out>where
Self: Sized,
Computes &self - &other.
sourcefn num_mul_ref(self, rhs: &Self::Rhs) -> Result<Self::Out>where
Self: Sized,
fn num_mul_ref(self, rhs: &Self::Rhs) -> Result<Self::Out>where
Self: Sized,
Computes self * &other.
sourcefn num_ref_mul(&self, rhs: Self::Rhs) -> Result<Self::Out>where
Self: Sized,
fn num_ref_mul(&self, rhs: Self::Rhs) -> Result<Self::Out>where
Self: Sized,
Computes &self * other.
sourcefn num_ref_mul_ref(&self, rhs: &Self::Rhs) -> Result<Self::Out>where
Self: Sized,
fn num_ref_mul_ref(&self, rhs: &Self::Rhs) -> Result<Self::Out>where
Self: Sized,
Computes &self * &other.
sourcefn num_div_ref(self, rhs: &Self::Rhs) -> Result<Self::Out>where
Self: Sized,
fn num_div_ref(self, rhs: &Self::Rhs) -> Result<Self::Out>where
Self: Sized,
Computes self / &other.
sourcefn num_ref_div(&self, rhs: Self::Rhs) -> Result<Self::Out>where
Self: Sized,
fn num_ref_div(&self, rhs: Self::Rhs) -> Result<Self::Out>where
Self: Sized,
Computes &self / other.
sourcefn num_ref_div_ref(&self, rhs: &Self::Rhs) -> Result<Self::Out>where
Self: Sized,
fn num_ref_div_ref(&self, rhs: &Self::Rhs) -> Result<Self::Out>where
Self: Sized,
Computes &self / &other.
sourcefn num_rem_ref(self, rhs: &Self::Rhs) -> Result<Self::Out>where
Self: Sized,
fn num_rem_ref(self, rhs: &Self::Rhs) -> Result<Self::Out>where
Self: Sized,
Computes self % &other.
sourcefn num_ref_rem(&self, rhs: Self::Rhs) -> Result<Self::Out>where
Self: Sized,
fn num_ref_rem(&self, rhs: Self::Rhs) -> Result<Self::Out>where
Self: Sized,
Computes &self % other.
sourcefn num_ref_rem_ref(&self, rhs: &Self::Rhs) -> Result<Self::Out>where
Self: Sized,
fn num_ref_rem_ref(&self, rhs: &Self::Rhs) -> Result<Self::Out>where
Self: Sized,
Computes &self % &other.
sourcefn num_ref_neg(&self) -> Result<Self::Out>where
Self: Sized,
fn num_ref_neg(&self) -> Result<Self::Out>where
Self: Sized,
Computes - &self.
sourcefn num_ref_abs(&self) -> Result<Self::Out>where
Self: Sized,
fn num_ref_abs(&self) -> Result<Self::Out>where
Self: Sized,
Computes the absolute value of &self.