Skip to content

Commit

Permalink
Merge #193
Browse files Browse the repository at this point in the history
193: Refactor/core support r=Dylan-DPC a=kinggoesgaming

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

# Description
* Refactor `prelude` items' core trait implementations. No breaking changes included.
* Add `<core|std>::fmt::Display` support for `UuidVariant`

# Motivation
Part of the grand refactor efforts

# Tests
<!-- How are these changes tested? -->
TODO

# Related Issue(s)
#124 


Co-authored-by: Hunar Roop Kahlon <hunar.roop@gmail.com>
  • Loading branch information
bors[bot] and kinggoesgaming committed Apr 13, 2018
2 parents 4d4591c + 01786ad commit b1eac3d
Show file tree
Hide file tree
Showing 3 changed files with 183 additions and 96 deletions.
173 changes: 173 additions & 0 deletions src/core_support.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
use prelude::*;

cfg_if! {
if #[cfg(feature = "std")] {
use std::fmt;
use std::str;
} else {
use core::fmt;
use core::str;
}
}

impl fmt::Display for Uuid {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::LowerHex::fmt(self, f)
}
}

impl fmt::Display for UuidVariant {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
UuidVariant::NCS => write!(f, "NCS"),
UuidVariant::RFC4122 => write!(f, "RFC4122"),
UuidVariant::Microsoft => write!(f, "Microsoft"),
UuidVariant::Future => write!(f, "Future"),
}
}
}

impl fmt::LowerHex for Uuid {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
<super::Hyphenated as fmt::LowerHex>::fmt(&self.hyphenated(), f)
}
}

impl fmt::UpperHex for Uuid {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
<super::Hyphenated as fmt::UpperHex>::fmt(&self.hyphenated(), f)
}
}

impl str::FromStr for Uuid {
type Err = super::ParseError;

fn from_str(uuid_str: &str) -> Result<Uuid, super::ParseError> {
Uuid::parse_str(uuid_str)
}
}

impl Default for Uuid {
fn default() -> Self {
Uuid::nil()
}
}

#[cfg(test)]
mod tests {
extern crate std;

use self::std::prelude::v1::*;
use prelude::*;
use test_util;

macro_rules! check {
($buf:ident, $format:expr, $target:expr, $len:expr, $cond:expr) => {
$buf.clear();
write!($buf, $format, $target).unwrap();
assert!($buf.len() == $len);
assert!($buf.chars().all($cond), "{}", $buf);
};
}

#[test]
fn test_uuid_compare() {
let uuid1 = test_util::new();
let uuid2 = test_util::new2();

assert_eq!(uuid1, uuid1);
assert_eq!(uuid2, uuid2);

assert_ne!(uuid1, uuid2);
assert_ne!(uuid2, uuid1);
}

#[test]
fn test_uuid_default() {
let default_uuid = Uuid::default();
let nil_uuid = Uuid::nil();

assert_eq!(default_uuid, nil_uuid);
}

#[test]
fn test_uuid_display() {
use super::fmt::Write;

let uuid = test_util::new();
let s = uuid.to_string();
let mut buffer = String::new();

assert_eq!(s, uuid.hyphenated().to_string());

check!(
buffer,
"{}",
uuid,
36,
|c| c.is_lowercase() || c.is_digit(10) || c == '-'
);
}

#[test]
fn test_uuid_lowerhex() {
use super::fmt::Write;

let mut buffer = String::new();
let uuid = test_util::new();

check!(
buffer,
"{:x}",
uuid,
36,
|c| c.is_lowercase() || c.is_digit(10) || c == '-'
);
}

#[test]
fn test_uuid_operator_eq() {
let uuid1 = test_util::new();
let uuid1_dup = uuid1.clone();
let uuid2 = test_util::new2();

assert!(uuid1 == uuid1);
assert!(uuid1 == uuid1_dup);
assert!(uuid1_dup == uuid1);

assert!(uuid1 != uuid2);
assert!(uuid2 != uuid1);
assert!(uuid1_dup != uuid2);
assert!(uuid2 != uuid1_dup);
}

#[test]
fn test_uuid_to_string() {
use super::fmt::Write;

let uuid = test_util::new();
let s = uuid.to_string();
let mut buffer = String::new();

assert_eq!(s.len(), 36);

check!(buffer, "{}", s, 36, |c| c.is_lowercase() || c.is_digit(10)
|| c == '-');
}

#[test]
fn test_uuid_upperhex() {
use super::fmt::Write;

let mut buffer = String::new();
let uuid = test_util::new();

check!(
buffer,
"{:X}",
uuid,
36,
|c| c.is_uppercase() || c.is_digit(10) || c == '-'
);
}
}
104 changes: 9 additions & 95 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,11 @@
//! * [RFC4122: A Universally Unique IDentifier (UUID) URN Namespace](
//! http://tools.ietf.org/html/rfc4122)
#![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
html_favicon_url = "https://www.rust-lang.org/favicon.ico",
html_root_url = "https://docs.rs/uuid")]
#![doc(
html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
html_favicon_url = "https://www.rust-lang.org/favicon.ico",
html_root_url = "https://docs.rs/uuid"
)]
#![deny(warnings)]
#![cfg_attr(not(feature = "std"), no_std)]

Expand Down Expand Up @@ -170,6 +172,8 @@ cfg_if! {

pub mod prelude;

mod core_support;

cfg_if! {
if #[cfg(feature = "serde")] {
mod serde_support;
Expand Down Expand Up @@ -213,7 +217,7 @@ pub enum UuidVersion {
}

/// The reserved variants of UUIDs.
#[derive(Debug, PartialEq, Copy, Clone)]
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum UuidVariant {
/// Reserved by the NCS for backward compatibility
NCS,
Expand All @@ -226,7 +230,7 @@ pub enum UuidVariant {
}

/// A Universally Unique Identifier (UUID).
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct Uuid {
/// The 128-bit number stored in 16 bytes
bytes: UuidBytes,
Expand Down Expand Up @@ -1050,49 +1054,6 @@ impl Uuid {
}
}

impl Default for Uuid {
/// Returns the nil UUID, which is all zeroes
fn default() -> Uuid {
Uuid::nil()
}
}

impl str::FromStr for Uuid {
type Err = ParseError;

/// Parse a hex string and interpret as a `Uuid`.
///
/// Accepted formats are a sequence of 32 hexadecimal characters,
/// with or without hyphens (grouped as 8, 4, 4, 4, 12).
fn from_str(us: &str) -> Result<Uuid, ParseError> {
Uuid::parse_str(us)
}
}

impl fmt::Debug for Uuid {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Uuid(\"{}\")", self.hyphenated())
}
}

impl fmt::Display for Uuid {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::LowerHex::fmt(self, f)
}
}

impl fmt::UpperHex for Uuid {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::UpperHex::fmt(&self.hyphenated(), f)
}
}

impl fmt::LowerHex for Uuid {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.hyphenated().fmt(f)
}
}

impl<'a> fmt::Display for Simple<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::LowerHex::fmt(self, f)
Expand Down Expand Up @@ -1620,23 +1581,6 @@ mod tests {
assert!(s.chars().all(|c| c.is_digit(16)));
}

#[test]
fn test_to_string() {
let uuid1 = test_util::new();
let s = uuid1.to_string();

assert_eq!(s.len(), 36);
assert!(s.chars().all(|c| c.is_digit(16) || c == '-'));
}

#[test]
fn test_display() {
let uuid1 = test_util::new();
let s = uuid1.to_string();

assert_eq!(s, uuid1.hyphenated().to_string());
}

#[test]
fn test_to_hyphenated_string() {
let uuid1 = test_util::new();
Expand Down Expand Up @@ -1674,8 +1618,6 @@ mod tests {
check!(buf, "{:X}", u.simple(), 32, |c| c.is_uppercase()
|| c.is_digit(10));

check!(buf, "{:x}", u, 36, |c| c.is_lowercase() || c.is_digit(10)
|| c == '-');
check!(
buf,
"{:x}",
Expand Down Expand Up @@ -1741,18 +1683,6 @@ mod tests {
assert_eq!(uuid_ss, uuid);
}

#[test]
fn test_compare() {
let uuid1 = test_util::new();
let uuid2 = test_util::new2();

assert_eq!(uuid1, uuid1);
assert_eq!(uuid2, uuid2);

assert_ne!(uuid1, uuid2);
assert_ne!(uuid2, uuid1);
}

#[test]
fn test_from_fields() {
let d1: u32 = 0xa1a2a3a4;
Expand Down Expand Up @@ -1857,22 +1787,6 @@ mod tests {
assert_eq!(u.simple().to_string(), expected);
}

#[test]
fn test_operator_eq() {
let u1 = test_util::new();
let u2 = u1.clone();
let u3 = test_util::new2();

assert_eq!(u1, u1);
assert_eq!(u1, u2);
assert_eq!(u2, u1);

assert_ne!(u1, u3);
assert_ne!(u3, u1);
assert_ne!(u2, u3);
assert_ne!(u3, u2);
}

#[test]
fn test_iterbytes_impl_for_uuid() {
let mut set = std::collections::HashSet::new();
Expand Down
2 changes: 1 addition & 1 deletion src/test_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ pub fn new2() -> Uuid {
0xA1, 0xE4,
],
}
}
}

0 comments on commit b1eac3d

Please sign in to comment.