From 7bd28e1077ddfc6b5ef16a8da591795db3d82758 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Wed, 8 Nov 2023 15:54:43 +1100 Subject: [PATCH] Use TryFrom in SharedSecret::from_slice Using `TryFrom` is more terse with no loss of clarity. Refactor only, no logic changes. --- src/ecdh.rs | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/ecdh.rs b/src/ecdh.rs index 6b4c7c285..9679f722d 100644 --- a/src/ecdh.rs +++ b/src/ecdh.rs @@ -4,6 +4,7 @@ //! use core::borrow::Borrow; +use core::convert::TryFrom; use core::{ptr, str}; use secp256k1_sys::types::{c_int, c_uchar, c_void}; @@ -66,13 +67,9 @@ impl SharedSecret { /// Creates a shared secret from `bytes` slice. #[inline] pub fn from_slice(bytes: &[u8]) -> Result { - match bytes.len() { - SHARED_SECRET_SIZE => { - let mut ret = [0u8; SHARED_SECRET_SIZE]; - ret[..].copy_from_slice(bytes); - Ok(SharedSecret(ret)) - } - _ => Err(Error::InvalidSharedSecret), + match <[u8; SHARED_SECRET_SIZE]>::try_from(bytes) { + Ok(bytes) => Ok(SharedSecret(bytes)), + Err(_) => Err(Error::InvalidSharedSecret), } } }