diff options
| author | Brendan Zabarauskas <bjzaba@yahoo.com.au> | 2014-01-18 17:08:23 +1100 |
|---|---|---|
| committer | Brendan Zabarauskas <bjzaba@yahoo.com.au> | 2014-01-20 18:09:46 +1100 |
| commit | cf56624a4ad7703c8f3fc327b8c385da0a803ea5 (patch) | |
| tree | 0a46f95db2c26f4beb50f7785a82f46348ff9083 /src/libstd/num/mod.rs | |
| parent | 764f2cb6f3517869e31fc7b93ff11dd840db8d30 (diff) | |
| download | rust-cf56624a4ad7703c8f3fc327b8c385da0a803ea5.tar.gz rust-cf56624a4ad7703c8f3fc327b8c385da0a803ea5.zip | |
Add operator trait constraints to std::num::{Zero, One} and document their appropriate use
Zero and One have precise definitions in mathematics. Documentation has been added to describe the appropriate uses for these traits and the laws that they should satisfy. For more information regarding these identities, see the following wikipedia pages: - http://wikipedia.org/wiki/Additive_identity - http://wikipedia.org/wiki/Multiplicative_identity
Diffstat (limited to 'src/libstd/num/mod.rs')
| -rw-r--r-- | src/libstd/num/mod.rs | 62 |
1 files changed, 46 insertions, 16 deletions
diff --git a/src/libstd/num/mod.rs b/src/libstd/num/mod.rs index c374d6c2157..23a852cc357 100644 --- a/src/libstd/num/mod.rs +++ b/src/libstd/num/mod.rs @@ -50,19 +50,59 @@ pub trait Orderable: Ord { /// Returns the number constrained within the range `mn <= self <= mx`. #[inline(always)] pub fn clamp<T: Orderable>(value: T, mn: T, mx: T) -> T { value.clamp(&mn, &mx) } -pub trait Zero { - fn zero() -> Self; // FIXME (#5527): This should be an associated constant +/// Defines an additive identity element for `Self`. +/// +/// # Deriving +/// +/// This trait can be automatically be derived using `#[deriving(Zero)]` +/// attribute. If you choose to use this, make sure that the laws outlined in +/// the documentation for `Zero::zero` still hold. +pub trait Zero: Add<Self, Self> { + /// Returns the additive identity element of `Self`, `0`. + /// + /// # Laws + /// + /// ~~~ + /// a + 0 = a ∀ a ∈ Self + /// 0 + a = a ∀ a ∈ Self + /// ~~~ + /// + /// # 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. + // FIXME (#5527): This should be an associated constant + fn zero() -> Self; + + /// Returns `true` if `self` is equal to the additive identity. fn is_zero(&self) -> bool; } -/// Returns `0` of appropriate type. +/// Returns the additive identity, `0`. #[inline(always)] pub fn zero<T: Zero>() -> T { Zero::zero() } -pub trait One { - fn one() -> Self; // FIXME (#5527): This should be an associated constant +/// Defines a multiplicative identity element for `Self`. +pub trait One: Mul<Self, Self> { + /// Returns the multiplicative identity element of `Self`, `1`. + /// + /// # Laws + /// + /// ~~~ + /// a * 1 = a ∀ a ∈ Self + /// 1 * a = a ∀ a ∈ Self + /// ~~~ + /// + /// # 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. + // FIXME (#5527): This should be an associated constant + fn one() -> Self; } -/// Returns `1` of appropriate type. +/// Returns the multiplicative identity, `1`. #[inline(always)] pub fn one<T: One>() -> T { One::one() } pub trait Signed: Num @@ -993,16 +1033,6 @@ pub fn from_str_radix<T: FromStrRadix>(str: &str, radix: uint) -> Option<T> { FromStrRadix::from_str_radix(str, radix) } -impl<T: Zero + 'static> Zero for @T { - fn zero() -> @T { @Zero::zero() } - fn is_zero(&self) -> bool { (**self).is_zero() } -} - -impl<T: Zero> Zero for ~T { - fn zero() -> ~T { ~Zero::zero() } - fn is_zero(&self) -> bool { (**self).is_zero() } -} - /// Saturating math operations pub trait Saturating { /// Saturating addition operator. |
