Skip to content

Commit

Permalink
Faster as_u128 and to_u128_le
Browse files Browse the repository at this point in the history
  • Loading branch information
pkoenig10 committed Apr 29, 2023
1 parent f725e58 commit 7978c5a
Showing 1 changed file with 2 additions and 32 deletions.
34 changes: 2 additions & 32 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -687,22 +687,7 @@ impl Uuid {
/// # }
/// ```
pub const fn as_u128(&self) -> u128 {
(self.as_bytes()[0] as u128) << 120
| (self.as_bytes()[1] as u128) << 112
| (self.as_bytes()[2] as u128) << 104
| (self.as_bytes()[3] as u128) << 96
| (self.as_bytes()[4] as u128) << 88
| (self.as_bytes()[5] as u128) << 80
| (self.as_bytes()[6] as u128) << 72
| (self.as_bytes()[7] as u128) << 64
| (self.as_bytes()[8] as u128) << 56
| (self.as_bytes()[9] as u128) << 48
| (self.as_bytes()[10] as u128) << 40
| (self.as_bytes()[11] as u128) << 32
| (self.as_bytes()[12] as u128) << 24
| (self.as_bytes()[13] as u128) << 16
| (self.as_bytes()[14] as u128) << 8
| (self.as_bytes()[15] as u128)
u128::from_be_bytes(*self.as_bytes())
}

/// Returns a 128bit little-endian value containing the value.
Expand Down Expand Up @@ -731,22 +716,7 @@ impl Uuid {
/// # }
/// ```
pub const fn to_u128_le(&self) -> u128 {
(self.as_bytes()[0] as u128)
| (self.as_bytes()[1] as u128) << 8
| (self.as_bytes()[2] as u128) << 16
| (self.as_bytes()[3] as u128) << 24
| (self.as_bytes()[4] as u128) << 32
| (self.as_bytes()[5] as u128) << 40
| (self.as_bytes()[6] as u128) << 48
| (self.as_bytes()[7] as u128) << 56
| (self.as_bytes()[8] as u128) << 64
| (self.as_bytes()[9] as u128) << 72
| (self.as_bytes()[10] as u128) << 80
| (self.as_bytes()[11] as u128) << 88
| (self.as_bytes()[12] as u128) << 96
| (self.as_bytes()[13] as u128) << 104
| (self.as_bytes()[14] as u128) << 112
| (self.as_bytes()[15] as u128) << 120
u128::from_le_bytes(*self.as_bytes())
}

/// Returns two 64bit values containing the value.
Expand Down

0 comments on commit 7978c5a

Please sign in to comment.