1 // Copyright 2013-2014 The Rust Project Developers.
2 // Copyright 2018 The Uuid Project Developers.
3 //
4 // See the COPYRIGHT file at the top-level directory of this distribution.
5 //
6 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
7 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
8 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
9 // option. This file may not be copied, modified, or distributed
10 // except according to those terms.
11 
12 use byteorder;
13 use prelude::*;
14 
15 impl Uuid {
16     /// Creates a new [`Uuid`] from a `u128` value.
17     ///
18     /// To create a [`Uuid`] from `u128`s, you need `u128` feature enabled for
19     /// this crate.
20     ///
21     /// [`Uuid`]: ../struct.Uuid.html
22     #[inline]
from_u128(quad: u128) -> Self23     pub fn from_u128(quad: u128) -> Self {
24         Uuid::from(quad)
25     }
26 }
27 
28 impl From<u128> for Uuid {
from(f: u128) -> Self29     fn from(f: u128) -> Self {
30         let mut bytes: ::Bytes = [0; 16];
31 
32         {
33             use byteorder::ByteOrder;
34 
35             byteorder::NativeEndian::write_u128(&mut bytes[..], f);
36         }
37 
38         Uuid::from_bytes(bytes)
39     }
40 }
41 
42 #[cfg(test)]
43 mod tests {
44     use prelude::*;
45 
46     #[test]
test_from_u128()47     fn test_from_u128() {
48         const U128: u128 = 0x3a0724b4_93a0_4d87_ac28_759c6caa13c4;
49 
50         let uuid = Uuid::from(U128);
51 
52         let uuid2: Uuid = U128.into();
53 
54         assert_eq!(uuid, uuid2)
55     }
56 
57 }
58