-
Notifications
You must be signed in to change notification settings - Fork 275
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Deprecate from_slice methods in favor of arrays #737
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only did shallow review, but everything apart from try_from
thing looks good to me, my reviews don't carry much weight in this repo though.
fd5649b
to
d612dc0
Compare
There are currently not formatting issues on master so there should be no need for a separate formatting patch in this PR bro. |
d612dc0
to
0cb6b30
Compare
Third patch removed and incorporated in first two |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hard no deprecating PublicKey::from_slice
(but only that one). Other suggestions are not that critical but I'd obviously prefer to fix them.
0cb6b30
to
1a6339a
Compare
Removed deprecation of |
1a6339a
to
46e06da
Compare
code review ack 46e06da except that the |
Functions have been added to PrivateKey, PublicKey and XOnlyPublicKey to allow the creation of a key directly from a byte array.
The PrivateKey and XOnlyPublicKey from_slice functions have been deprecated and calls to them from within the crate have been changed to use the equivalent array function.
46e06da
to
537b85b
Compare
Thanks for the review. I have replaced |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ACK 537b85b successfully ran local tests
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ACK 537b85b
My suggestions are just because we all enjoy coding round here and it amused me to find a way to re-write your changes using combinators.
pub fn from_slice(data: &[u8]) -> Result<XOnlyPublicKey, Error> { | ||
if data.is_empty() || data.len() != constants::SCHNORR_PUBLIC_KEY_SIZE { | ||
return Err(Error::InvalidPublicKey); | ||
match <[u8; constants::SCHNORR_PUBLIC_KEY_SIZE]>::try_from(data) { | ||
Ok(data) => Self::from_byte_array(&data), | ||
Err(_) => Err(InvalidPublicKey), | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function could be written with combinators like this:
pub fn from_slice(data: &[u8]) -> Result<XOnlyPublicKey, Error> {
<[u8; constants::SCHNORR_PUBLIC_KEY_SIZE]>::try_from(data)
.map_err(|_| InvalidPublicKey)
.and_then(|data| Self::from_byte_array(&data))
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It does look quite neat with a combinator
@@ -64,6 +64,7 @@ impl SharedSecret { | |||
pub fn from_bytes(bytes: [u8; SHARED_SECRET_SIZE]) -> SharedSecret { SharedSecret(bytes) } | |||
|
|||
/// Creates a shared secret from `bytes` slice. | |||
#[deprecated(since = "TBD", note = "Use `from_bytes` instead.")] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would have expected with this addition to see a change to call from_bytes
. If you like the combinator style you could use
pub fn from_slice(bytes: &[u8]) -> Result<SharedSecret, Error> {
<[u8; SHARED_SECRET_SIZE]>::try_from(bytes)
.map_err(|_| Error::InvalidSharedSecret)
.and_then(|a| Ok(Self::from_bytes(a)))
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I need to change anything again I'll add this too, thanks
I also prefer the combinator version, though of course I won't hold up the PR over it. But @Kixunil we need you to take a look at this one again since your status is "change requested". |
I find |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ACK 537b85b
Ok(constants::UNCOMPRESSED_PUBLIC_KEY_SIZE) => PublicKey::from_slice(&res), | ||
Ok(constants::PUBLIC_KEY_SIZE) => { | ||
let bytes: [u8; constants::PUBLIC_KEY_SIZE] = | ||
res[0..constants::PUBLIC_KEY_SIZE].try_into().unwrap(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Strictly speaking, it'd be better to create a reference here (simply annotating the type with &
should work because the conversion is defined) to avoid useless copying but there's a good chance compiler will optimize it out anyway.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you saying you can convert a &[u8]
to a &[u8; N]
via try_into
and the compiler will just cast the pointers rather than copying?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Exactly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wow, that's a huge win and I feel like a clippy lint should exist for it. (Though it'd be a bit tough in general to tell when the array is only used by reference.)
@@ -1156,8 +1161,7 @@ impl str::FromStr for XOnlyPublicKey { | |||
fn from_str(s: &str) -> Result<XOnlyPublicKey, Error> { | |||
let mut res = [0u8; constants::SCHNORR_PUBLIC_KEY_SIZE]; | |||
match from_hex(s, &mut res) { | |||
Ok(constants::SCHNORR_PUBLIC_KEY_SIZE) => | |||
XOnlyPublicKey::from_slice(&res[0..constants::SCHNORR_PUBLIC_KEY_SIZE]), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LOL, the previous code...
As brought up in issue rust-bitcoin/rust-bitcoin#3102 support for Rust arrays is now much better so slice-accepting methods that require a fixed length can be replaced with a method that accepts an array.
from_slice()
methods have been deprecated and calls to it from within the crate have been changed to use the equivalent array method.