use core::num::Wrapping; use core::ops::{Add, Mul};
/// Defines an additive identity element for `Self`. /// /// # Laws /// /// ```text /// a + 0 = a ∀ a ∈ Self /// 0 + a = a ∀ a ∈ Self /// ``` pubtrait Zero: Sized + Add<Self, Output = Self> { /// Returns the additive identity element of `Self`, `0`. /// # Purity /// /// This function should return the same result at all times regardless of /// external mutable state, for example values stored in TLS or in /// `static mut`s. // This cannot be an associated constant, because of bignums. fn zero() -> Self;
/// Sets `self` to the additive identity element of `Self`, `0`. fn set_zero(&mutself) {
*self = Zero::zero();
}
/// Returns `true` if `self` is equal to the additive identity. fn is_zero(&self) -> bool;
}
/// Defines an associated constant representing the additive identity element /// for `Self`. pubtrait ConstZero: Zero { /// The additive identity element of `Self`, `0`. const ZERO: Self;
}
impl<T: Zero> Zero for Wrapping<T> where
Wrapping<T>: Add<Output = Wrapping<T>>,
{ fn is_zero(&self) -> bool { self.0.is_zero()
}
fn set_zero(&mutself) { self.0.set_zero();
}
fn zero() -> Self {
Wrapping(T::zero())
}
}
impl<T: ConstZero> ConstZero for Wrapping<T> where
Wrapping<T>: Add<Output = Wrapping<T>>,
{ const ZERO: Self = Wrapping(T::ZERO);
}
/// Defines a multiplicative identity element for `Self`. /// /// # Laws /// /// ```text /// a * 1 = a ∀ a ∈ Self /// 1 * a = a ∀ a ∈ Self /// ``` pubtrait One: Sized + Mul<Self, Output = Self> { /// Returns the multiplicative identity element of `Self`, `1`. /// /// # Purity /// /// This function should return the same result at all times regardless of /// external mutable state, for example values stored in TLS or in /// `static mut`s. // This cannot be an associated constant, because of bignums. fn one() -> Self;
/// Sets `self` to the multiplicative identity element of `Self`, `1`. fn set_one(&mutself) {
*self = One::one();
}
/// Returns `true` if `self` is equal to the multiplicative identity. /// /// For performance reasons, it's best to implement this manually. /// After a semver bump, this method will be required, and the /// `where Self: PartialEq` bound will be removed. #[inline] fn is_one(&self) -> bool where Self: PartialEq,
{
*self == Self::one()
}
}
/// Defines an associated constant representing the multiplicative identity /// element for `Self`. pubtrait ConstOne: One { /// The multiplicative identity element of `Self`, `1`. const ONE: Self;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.