Skip to content

Commit

Permalink
Merge #181
Browse files Browse the repository at this point in the history
181: Add Nil variant r=Dylan-DPC a=kinggoesgaming

<!--
    As we are working towards a stable version of uuid, we require that you 
    open an issue, before submitting a pull request. If the pull request is 
    imcomplete, prepend the Title with WIP: 
-->

**I'm submitting a ...**
  - [ ] bug fix
  - [x] feature enhancement
  - [ ] deprecation or removal
  - [ ] refactor

# Description
Adds `Nil` variant to the `UuidVersion`.

# Motivation
`nil` `Uuid`, containing all zeros, is a valid `Uuid`, as per the specification. This will allow users doing pattern matching to explicitly check for it.

# Tests
Updated `test_nil` to ensure that 

# Related Issue(s)
closes #133
supersedes #176
  • Loading branch information
bors[bot] committed Mar 25, 2018
2 parents a2a1754 + 4e9c33f commit a9b5afb
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 29 deletions.
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

0 comments on commit a9b5afb

Please sign in to comment.