diff options
| author | bors <bors@rust-lang.org> | 2014-01-20 10:16:30 -0800 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2014-01-20 10:16:30 -0800 |
| commit | bf89b68a3702e10b18e84ef37c3aa19f13418731 (patch) | |
| tree | 07477f37fb022086a050bfe99abcce0668f937c6 /src/libextra | |
| parent | f8efde148c05d0603f1a4ef60764c1910db20181 (diff) | |
| parent | 509283d149bb81cad728b2c1b81f7ab8ceb206e1 (diff) | |
| download | rust-bf89b68a3702e10b18e84ef37c3aa19f13418731.tar.gz rust-bf89b68a3702e10b18e84ef37c3aa19f13418731.zip | |
auto merge of #11664 : bjz/rust/identities, r=alexcrichton
`Zero` and `One` have precise definitions in mathematics as the identities of the `Add` and `Mul` operations respectively. As such, types that implement these identities are now also required to implement their respective operator traits. This should reduce their misuse whilst still enabling them to be used in generalized algebraic structures (not just numbers). Existing usages of `#[deriving(Zero)]` in client code could break under these new rules, but this is probably a sign that they should have been using something like `#[deriving(Default)]` in the first place. For more information regarding the mathematical definitions of the additive and multiplicative identities, see the following Wikipedia articles: - http://wikipedia.org/wiki/Additive_identity - http://wikipedia.org/wiki/Multiplicative_identity Note that for floating point numbers the laws specified in the doc comments of `Zero::zero` and `One::one` may not always hold. This is true however for many other traits currently implemented by floating point numbers. What traits floating point numbers should and should not implement is an open question that is beyond the scope of this pull request. The implementation of `std::num::pow` has been made more succinct and no longer requires `Clone`. The coverage of the associated unit test has also been increased to test for more combinations of bases, exponents, and expected results.
Diffstat (limited to 'src/libextra')
| -rw-r--r-- | src/libextra/uuid.rs | 45 |
1 files changed, 14 insertions, 31 deletions
diff --git a/src/libextra/uuid.rs b/src/libextra/uuid.rs index 2c48a7a4d3e..02930dc9c4c 100644 --- a/src/libextra/uuid.rs +++ b/src/libextra/uuid.rs @@ -57,7 +57,7 @@ Examples of string representations: use std::str; use std::vec; -use std::num::{FromStrRadix, Zero}; +use std::num::FromStrRadix; use std::char::Char; use std::container::Container; use std::to_str::ToStr; @@ -158,9 +158,8 @@ static UuidGroupLens: [uint, ..5] = [8u, 4u, 4u, 4u, 12u]; /// UUID support impl Uuid { - /// Returns a nil or empty UUID (containing all zeroes) - pub fn new_nil() -> Uuid { + pub fn nil() -> Uuid { let uuid = Uuid{ bytes: [0, .. 16] }; uuid } @@ -423,24 +422,17 @@ impl Uuid { Ok(Uuid::from_bytes(ub).unwrap()) } -} -impl Default for Uuid { - /// Returns the nil UUID, which is all zeroes - fn default() -> Uuid { - Uuid::new_nil() + /// Tests if the UUID is nil + pub fn is_nil(&self) -> bool { + return self.bytes.iter().all(|&b| b == 0); } } -impl Zero for Uuid { +impl Default for Uuid { /// Returns the nil UUID, which is all zeroes - fn zero() -> Uuid { - Uuid::new_nil() - } - - /// Tests if the UUID is nil or all zeroes - fn is_zero(&self) -> bool { - return self.bytes.iter().all(|&b| b == 0); + fn default() -> Uuid { + Uuid::nil() } } @@ -521,24 +513,15 @@ mod test { use super::*; use std::str; use std::rand; - use std::num::Zero; use std::io::MemWriter; #[test] - fn test_new_nil() { - let nil = Uuid::new_nil(); - let nb = nil.to_bytes(); - - assert!(nb.iter().all(|&b| b == 0)); - } - - #[test] - fn test_zero() { - let uz: Uuid = Zero::zero(); - let nz = Uuid::new_v4(); + fn test_nil() { + let nil = Uuid::nil(); + let not_nil = Uuid::new_v4(); - assert!(uz.is_zero()); - assert!(! nz.is_zero()); + assert!(nil.is_nil()); + assert!(!not_nil.is_nil()); } #[test] @@ -619,7 +602,7 @@ mod test { assert!(Uuid::parse_string("urn:uuid:67e55044-10b1-426f-9247-bb680e5fe0c8").is_ok()); // Nil - let nil = Uuid::new_nil(); + let nil = Uuid::nil(); assert!(Uuid::parse_string("00000000000000000000000000000000").unwrap() == nil); assert!(Uuid::parse_string("00000000-0000-0000-0000-000000000000").unwrap() == nil); |
