Skip to content

Commit

Permalink
Merge #201
Browse files Browse the repository at this point in the history
201: support u128 r=Dylan-DPC a=kinggoesgaming

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

# Description
* features introduced: `u128`, `nightly`
* rust channel needed: `nightly`
* dependency introduced: `byteorder`

This introduced implementation for `u128` in the from of a `Uuid::from_u128` and `impl From`

# Motivation
`Uuid`s are 128 bits in size and rust natively provides a `u128` integer type. This 

# Tests
`u128_support::test_from_u128()` introduced and passes

# Related Issue(s)
N/A

Co-authored-by: Hunar Roop Kahlon <hunar.roop@gmail.com>
  • Loading branch information
bors[bot] and kinggoesgaming committed Apr 23, 2018
2 parents 5144c0e + 0631734 commit aedc943
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ matrix:
- rust: beta
- rust: nightly

- rust: nightly
env: FEATURES="u128"

- rust: nightly
script: cargo bench --all-features

Expand Down
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,17 @@ sha1 = { version = "0.6", optional = true }
md5 = { version = "0.3", optional = true }
slog = { version = "2", optional = true }
cfg-if = "0.1"
byteorder = { version = "1", optional = true, default-features = false, features = ["i128"]}

[dev-dependencies]
serde_test = "1.0.19"

[features]
default = ["std"]
nightly = []
std = []
use_std = ["std"] # Compatibility shim for 0.5 and earlier
u128 = ["nightly"]
v1 = []
v3 = ["md5", "rand"]
v4 = ["rand"]
Expand Down
13 changes: 13 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,17 @@
)]
#![deny(warnings)]
#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(all(feature = "u128", nightly), feature(i128_type))]

#[macro_use]
extern crate cfg_if;

cfg_if! {
if #[cfg(feature = "byteorder")] {
extern crate byteorder;
}
}

cfg_if! {
if #[cfg(feature = "md5")] {
extern crate md5;
Expand Down Expand Up @@ -193,6 +200,12 @@ cfg_if! {
}
}

cfg_if! {
if #[cfg(all(feature = "u128"), nightly)] {
mod u128_support;
}
}

cfg_if! {
if #[cfg(feature = "v4")] {
mod v4;
Expand Down
46 changes: 46 additions & 0 deletions src/u128_support.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use byteorder;
use prelude::*;

impl Uuid {
/// Creates a new [`Uuid`] from a `u128` value.
///
/// To create a [`Uuid`] from `u128`s, you need `u128` feature enabled for this crate.
///
/// [`Uuid`]: ../struct.Uuid.html
#[inline(always)]
pub fn from_u128(quad: u128) -> Self {
Uuid::from(quad)
}
}

impl From<u128> for Uuid {
fn from(f: u128) -> Self {
let mut uuid = Uuid::default();

{
use byteorder::ByteOrder;

byteorder::NativeEndian::write_u128(&mut uuid.bytes.as_mut(), f);
}

uuid
}
}


#[cfg(test)]
mod tests {
use prelude::*;

#[test]
fn test_from_u128() {
const U128: u128 = 0x3a0724b4_93a0_4d87_ac28_759c6caa13c4;

let uuid = Uuid::from(U128);

let uuid2: Uuid = U128.into();

assert_eq!(uuid, uuid2)
}

}

0 comments on commit aedc943

Please sign in to comment.