From 591ffec1ebb287f3cc5bf154b3c58cc765d78b76 Mon Sep 17 00:00:00 2001 From: Ariel Miculas Date: Sat, 29 Jun 2024 22:40:57 +0300 Subject: [PATCH] Avoid allocation when decoding hex strings 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 --- puzzlefs-lib/src/format/types.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/puzzlefs-lib/src/format/types.rs b/puzzlefs-lib/src/format/types.rs index c472927..da1d276 100644 --- a/puzzlefs-lib/src/format/types.rs +++ b/puzzlefs-lib/src/format/types.rs @@ -794,10 +794,8 @@ impl Serialize for Digest { impl TryFrom<&str> for Digest { type Error = FromHexError; fn try_from(s: &str) -> std::result::Result { - 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)) } }