-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
4 changed files
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
|
||
} |