Skip to content

Commit 7bd28e1

Browse files
committed
Use TryFrom in SharedSecret::from_slice
Using `TryFrom` is more terse with no loss of clarity. Refactor only, no logic changes.
1 parent 08c330f commit 7bd28e1

File tree

1 file changed

+4
-7
lines changed

1 file changed

+4
-7
lines changed

src/ecdh.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
//!
55
66
use core::borrow::Borrow;
7+
use core::convert::TryFrom;
78
use core::{ptr, str};
89

910
use secp256k1_sys::types::{c_int, c_uchar, c_void};
@@ -66,13 +67,9 @@ impl SharedSecret {
6667
/// Creates a shared secret from `bytes` slice.
6768
#[inline]
6869
pub fn from_slice(bytes: &[u8]) -> Result<SharedSecret, Error> {
69-
match bytes.len() {
70-
SHARED_SECRET_SIZE => {
71-
let mut ret = [0u8; SHARED_SECRET_SIZE];
72-
ret[..].copy_from_slice(bytes);
73-
Ok(SharedSecret(ret))
74-
}
75-
_ => Err(Error::InvalidSharedSecret),
70+
match <[u8; SHARED_SECRET_SIZE]>::try_from(bytes) {
71+
Ok(bytes) => Ok(SharedSecret(bytes)),
72+
Err(_) => Err(Error::InvalidSharedSecret),
7673
}
7774
}
7875
}

0 commit comments

Comments
 (0)