Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for converting u64 pair into UUID and back #518

Merged
merged 2 commits into from Apr 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/builder/mod.rs
Expand Up @@ -202,6 +202,28 @@ impl Uuid {
])
}

/// Creates a UUID from two 64bit values in big-endian order.
pub const fn from_u64_pair(high_bits: u64, low_bits: u64) -> Self {
Uuid::from_bytes([
(high_bits >> 56) as u8,
(high_bits >> 48) as u8,
(high_bits >> 40) as u8,
(high_bits >> 32) as u8,
(high_bits >> 24) as u8,
(high_bits >> 16) as u8,
(high_bits >> 8) as u8,
high_bits as u8,
(low_bits >> 56) as u8,
(low_bits >> 48) as u8,
(low_bits >> 40) as u8,
(low_bits >> 32) as u8,
(low_bits >> 24) as u8,
(low_bits >> 16) as u8,
(low_bits >> 8) as u8,
low_bits as u8,
])
}

/// Creates a UUID using the supplied big-endian bytes.
///
/// # Errors
Expand Down
50 changes: 50 additions & 0 deletions src/lib.rs
Expand Up @@ -512,6 +512,32 @@ impl Uuid {
| u128::from(self.as_bytes()[15]) << 120
}

/// Returns two 64bit values containing the UUID data.
///
/// The bytes in the UUID will be split into two `u64`.
/// The first u64 represents the 64 most significant bits,
/// the second one represents the 64 least significant.
///
/// # Examples
///
/// ```
/// use uuid::Uuid;
///
/// fn main() -> Result<(), uuid::Error> {
/// let uuid = Uuid::parse_str("936DA01F-9ABD-4D9D-80C7-02AF85C822A8")?;
/// assert_eq!(
/// uuid.as_u64_pair(),
/// (0x936DA01F9ABD4D9D,
/// 0x80C702AF85C822A8),
/// );
/// Ok(())
/// }
/// ```
pub fn as_u64_pair(&self) -> (u64, u64) {
let value = self.as_u128();
((value >> 64) as u64, value as u64)
}

/// Returns an array of 16 octets containing the UUID data.
pub const fn as_bytes(&self) -> &Bytes {
&self.0
Expand Down Expand Up @@ -985,6 +1011,18 @@ mod tests {
assert_eq!(result, expected);
}

#[test]
fn test_from_u64_pair() {
let high_in: u64 = 0xa1a2a3a4b1b2c1c2;
let low_in: u64 = 0xd1d2d3d4d5d6d7d8;

let u = Uuid::from_u64_pair(high_in, low_in);

let expected = "a1a2a3a4b1b2c1c2d1d2d3d4d5d6d7d8";
let result = u.to_simple().to_string();
assert_eq!(result, expected);
}

#[test]
fn test_u128_roundtrip() {
let v_in: u128 = 0xa1a2a3a4b1b2c1c2d1d2d3d4d5d6d7d8;
Expand All @@ -1005,6 +1043,18 @@ mod tests {
assert_eq!(v_in, v_out);
}

#[test]
fn test_u64_pair_roundtrip() {
let high_in: u64 = 0xa1a2a3a4b1b2c1c2;
let low_in: u64 = 0xd1d2d3d4d5d6d7d8;

let u = Uuid::from_u64_pair(high_in, low_in);
let (high_out, low_out) = u.as_u64_pair();

assert_eq!(high_in, high_out);
assert_eq!(low_in, low_out);
}

#[test]
fn test_u128_le_is_actually_le() {
let v_in: u128 = 0xa1a2a3a4b1b2c1c2d1d2d3d4d5d6d7d8;
Expand Down