diff options
| author | bors <bors@rust-lang.org> | 2013-10-16 21:41:23 -0700 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2013-10-16 21:41:23 -0700 |
| commit | 6c08cc2db4f98e9f07ae7d50338396c4123c2f0a (patch) | |
| tree | 0d60c8fa839b0a99cf37958d948232ed19603009 | |
| parent | 5d8e494a8c7d668eaac80fea59091a6e8fd70368 (diff) | |
| parent | 75cedf8e62a0c8d8bfd9e15d4a1d3b554eccbdc2 (diff) | |
| download | rust-6c08cc2db4f98e9f07ae7d50338396c4123c2f0a.tar.gz rust-6c08cc2db4f98e9f07ae7d50338396c4123c2f0a.zip | |
auto merge of #9846 : cmr/rust/serialize_uuid, r=alexcrichton
| -rw-r--r-- | src/libextra/uuid.rs | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/src/libextra/uuid.rs b/src/libextra/uuid.rs index a219b8fb557..345cf64f128 100644 --- a/src/libextra/uuid.rs +++ b/src/libextra/uuid.rs @@ -66,6 +66,8 @@ use std::rand::Rng; use std::cmp::Eq; use std::cast::{transmute,transmute_copy}; +use serialize::{Encoder, Encodable, Decoder, Decodable}; + /// A 128-bit (16 byte) buffer containing the ID pub type UuidBytes = [u8, ..16]; @@ -486,6 +488,21 @@ impl TotalEq for Uuid { } } +// FIXME #9845: Test these more thoroughly +impl<T: Encoder> Encodable<T> for Uuid { + /// Encode a UUID as a hypenated string + fn encode(&self, e: &mut T) { + e.emit_str(self.to_hyphenated_str()); + } +} + +impl<T: Decoder> Decodable<T> for Uuid { + /// Decode a UUID from a string + fn decode(d: &mut T) -> Uuid { + from_str(d.read_str()).unwrap() + } +} + /// Generates a random instance of UUID (V4 conformant) impl rand::Rand for Uuid { #[inline] @@ -770,6 +787,20 @@ mod test { assert!(ub.len() == 16); assert!(! ub.iter().all(|&b| b == 0)); } + + #[test] + fn test_serialize_round_trip() { + use std; + use ebml; + use serialize::{Encodable, Decodable}; + + let u = Uuid::new_v4(); + let bytes = do std::io::with_bytes_writer |wr| { + u.encode(&mut ebml::writer::Encoder(wr)); + }; + let u2 = Decodable::decode(&mut ebml::reader::Decoder(ebml::reader::Doc(@bytes))); + assert_eq!(u, u2); + } } #[cfg(test)] |
