Skip to content
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

Add Nil variant #181

Merged
merged 6 commits into from
Mar 25, 2018
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions benches/parse_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
extern crate slog;
extern crate test;
extern crate uuid;
use test::Bencher;
use uuid::Uuid;
#[cfg(feature = "slog")]
use slog::Drain;
use test::Bencher;
use uuid::Uuid;

#[bench]
fn bench_parse(b: &mut Bencher) {
Expand Down
62 changes: 36 additions & 26 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,10 @@ use core::fmt;
use core::hash;
use core::str::FromStr;

#[cfg(feature = "std")]
mod std_support;
#[cfg(feature = "serde")]
mod serde;
#[cfg(feature = "std")]
mod std_support;

#[cfg(feature = "v1")]
use core::sync::atomic::{AtomicUsize, Ordering};
Expand All @@ -151,6 +151,10 @@ pub type UuidBytes = [u8; 16];
/// The version of the UUID, denoting the generating algorithm.
#[derive(Debug, PartialEq, Copy, Clone)]
pub enum UuidVersion {
/// Special case for `nil` [`Uuid`].
///
/// [`Uuid`]: struct.Uuid.html
Nil,
/// Version 1: MAC address
Mac = 1,
/// Version 2: DCE Security
Expand Down Expand Up @@ -697,6 +701,7 @@ impl Uuid {
pub fn get_version(&self) -> Option<UuidVersion> {
let v = self.bytes[6] >> 4;
match v {
0 if self.is_nil() => Some(UuidVersion::Nil),
1 => Some(UuidVersion::Mac),
2 => Some(UuidVersion::Dce),
3 => Some(UuidVersion::Md5),
Expand Down Expand Up @@ -1044,29 +1049,27 @@ impl<'a> fmt::Display for Hyphenated<'a> {
}

macro_rules! hyphnated_write {
($f:expr, $format:expr, $bytes:expr) => {{
let data1 = (($bytes[0] as u32) << 24) |
(($bytes[1] as u32) << 16) |
(($bytes[2] as u32) << 8) |
(($bytes[3] as u32) << 0);
let data2 = (($bytes[4] as u16) << 8) |
(($bytes[5] as u16) << 0);
let data3 = (($bytes[6] as u16) << 8) |
(($bytes[7] as u16) << 0);

write!($f,
$format,
data1,
data2,
data3,
$bytes[8],
$bytes[9],
$bytes[10],
$bytes[11],
$bytes[12],
$bytes[13],
$bytes[14],
$bytes[15])
($f: expr, $format: expr, $bytes: expr) => {{
let data1 = (($bytes[0] as u32) << 24) | (($bytes[1] as u32) << 16)
| (($bytes[2] as u32) << 8) | (($bytes[3] as u32) << 0);
let data2 = (($bytes[4] as u16) << 8) | (($bytes[5] as u16) << 0);
let data3 = (($bytes[6] as u16) << 8) | (($bytes[7] as u16) << 0);

write!(
$f,
$format,
data1,
data2,
data3,
$bytes[8],
$bytes[9],
$bytes[10],
$bytes[11],
$bytes[12],
$bytes[13],
$bytes[14],
$bytes[15]
)
}};
}

Expand Down Expand Up @@ -1272,9 +1275,16 @@ mod tests {
fn test_nil() {
let nil = Uuid::nil();
let not_nil = new();
let from_bytes =
Uuid::from_uuid_bytes([4, 54, 67, 12, 43, 2, 2, 76, 32, 50, 87, 5, 1, 33, 43, 87]);

assert_eq!(from_bytes.get_version(), None);

assert!(nil.is_nil());
assert!(!not_nil.is_nil());

assert_eq!(nil.get_version(), Some(UuidVersion::Nil));
assert_eq!(not_nil.get_version(), Some(UuidVersion::Random))
}

#[test]
Expand Down Expand Up @@ -1571,7 +1581,7 @@ mod tests {
let u = new();

macro_rules! check {
($buf:ident, $format:expr, $target:expr, $len:expr, $cond:expr) => {
($buf: ident, $format: expr, $target: expr, $len: expr, $cond: expr) => {
$buf.clear();
write!($buf, $format, $target).unwrap();
assert!(buf.len() == $len);
Expand Down
2 changes: 1 addition & 1 deletion src/serde.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
extern crate serde;

use core::fmt;
use self::serde::{de, Deserialize, Deserializer, Serialize, Serializer};
use core::fmt;

use Uuid;

Expand Down