Skip to content

Commit

Permalink
[pdsl_core] Add KeyDiff::try_to_u128
Browse files Browse the repository at this point in the history
  • Loading branch information
Robbepop committed Jan 7, 2019
1 parent 966d29d commit bc711cb
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions pdsl_core/src/storage/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,22 @@ impl KeyDiff {
);
Some(value)
}

/// Tries to convert the key difference to a `u128` if possible.
///
/// Returns `None` if the resulting value is out of bounds.
pub fn try_to_u128(&self) -> Option<u128> {
const KEY_BYTES: usize = 32;
const U128_BYTES: usize = core::mem::size_of::<u128>();
if self.as_bytes().into_iter().take(KEY_BYTES - U128_BYTES).any(|&byte| byte != 0x0) {
return None
}
let value = byte_utils::bytes16_to_u128(
byte_utils::slice16_as_array16(&self.as_bytes()[(KEY_BYTES - U128_BYTES)..KEY_BYTES])
.unwrap()
);
Some(value)
}
}

macro_rules! impl_add_sub_for_key {
Expand Down

0 comments on commit bc711cb

Please sign in to comment.