Skip to content

Commit

Permalink
Use TryFrom in SharedSecret::from_slice
Browse files Browse the repository at this point in the history
Using `TryFrom` is more terse with no loss of clarity.

Refactor only, no logic changes.
  • Loading branch information
tcharding committed Nov 8, 2023
1 parent 08c330f commit 7bd28e1
Showing 1 changed file with 4 additions and 7 deletions.
11 changes: 4 additions & 7 deletions src/ecdh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -66,13 +67,9 @@ impl SharedSecret {
/// Creates a shared secret from `bytes` slice.
#[inline]
pub fn from_slice(bytes: &[u8]) -> Result<SharedSecret, Error> {
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),
}
}
}
Expand Down

0 comments on commit 7bd28e1

Please sign in to comment.