Skip to content

Commit b8c87fd

Browse files
committed
auto merge of #11130 : olsonjeffery/rust/master, r=alexcrichton
So that `Uuid` can be used as the key in a `HashMap` or in a `HashSet`, etc The only question I have about this is: Is endianness an issue, here? If so, what's the correct way to proceed?
2 parents e09a8e8 + 41cbbb6 commit b8c87fd

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/libextra/uuid.rs

+17
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ use std::rand;
6565
use std::rand::Rng;
6666
use std::cmp::Eq;
6767
use std::cast::{transmute,transmute_copy};
68+
use std::to_bytes::{IterBytes, Cb};
6869

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

@@ -104,6 +105,11 @@ pub struct Uuid {
104105
/// The 128-bit number stored in 16 bytes
105106
bytes: UuidBytes
106107
}
108+
impl IterBytes for Uuid {
109+
fn iter_bytes(&self, _: bool, f: Cb) -> bool {
110+
f(self.bytes.slice_from(0))
111+
}
112+
}
107113

108114
/// A UUID stored as fields (identical to UUID, used only for conversions)
109115
struct UuidFields {
@@ -796,6 +802,17 @@ mod test {
796802
let u2 = Decodable::decode(&mut ebml::reader::Decoder(doc));
797803
assert_eq!(u, u2);
798804
}
805+
806+
#[test]
807+
fn test_iterbytes_impl_for_uuid() {
808+
use std::hashmap::HashSet;
809+
let mut set = HashSet::new();
810+
let id1 = Uuid::new_v4();
811+
let id2 = Uuid::new_v4();
812+
set.insert(id1);
813+
assert!(set.contains(&id1));
814+
assert!(!set.contains(&id2));
815+
}
799816
}
800817

801818
#[cfg(test)]

0 commit comments

Comments
 (0)