Skip to content

Commit

Permalink
Avoid allocation when decoding hex strings
Browse files Browse the repository at this point in the history
Not particularly useful in user space, but we avoid allocation as much
as possible in kernel space and we also want to share the code between
the two implementations.

Signed-off-by: Ariel Miculas <[email protected]>
  • Loading branch information
ariel-miculas committed Jun 29, 2024
1 parent 7c766f7 commit 591ffec
Showing 1 changed file with 2 additions and 4 deletions.
6 changes: 2 additions & 4 deletions puzzlefs-lib/src/format/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -794,10 +794,8 @@ impl Serialize for Digest {
impl TryFrom<&str> for Digest {
type Error = FromHexError;
fn try_from(s: &str) -> std::result::Result<Self, Self::Error> {
let digest = hex::decode(s)?;
let digest: [u8; SHA256_BLOCK_SIZE] = digest
.try_into()
.map_err(|_| FromHexError::InvalidStringLength)?;
let mut digest: [u8; SHA256_BLOCK_SIZE] = [0; SHA256_BLOCK_SIZE];
hex::decode_to_slice(s, &mut digest)?;
Ok(Digest(digest))
}
}
Expand Down

0 comments on commit 591ffec

Please sign in to comment.