Skip to content

extra: impl IterBytes for uuid::Uuid #11130

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/libextra/uuid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ use std::rand;
use std::rand::Rng;
use std::cmp::Eq;
use std::cast::{transmute,transmute_copy};
use std::to_bytes::{IterBytes, Cb};

use serialize::{Encoder, Encodable, Decoder, Decodable};

Expand Down Expand Up @@ -104,6 +105,11 @@ pub struct Uuid {
/// The 128-bit number stored in 16 bytes
bytes: UuidBytes
}
impl IterBytes for Uuid {
fn iter_bytes(&self, _: bool, f: Cb) -> bool {
f(self.bytes.slice_from(0))
}
}

/// A UUID stored as fields (identical to UUID, used only for conversions)
struct UuidFields {
Expand Down Expand Up @@ -796,6 +802,17 @@ mod test {
let u2 = Decodable::decode(&mut ebml::reader::Decoder(doc));
assert_eq!(u, u2);
}

#[test]
fn test_iterbytes_impl_for_uuid() {
use std::hashmap::HashSet;
let mut set = HashSet::new();
let id1 = Uuid::new_v4();
let id2 = Uuid::new_v4();
set.insert(id1);
assert!(set.contains(&id1));
assert!(!set.contains(&id2));
}
}

#[cfg(test)]
Expand Down