Merge rust-bitcoin/rust-bitcoin#1349: Remove `into_bytes` from `impl_array_newtype`

8c4fa5ec3d Remove into_bytes from impl_array_newtype (Tobin C. Harding)

Pull request description:

  All the types that we define with `impl_array_newtype` are `Copy` so the correct conversion method to get the underlying byte array is `to_bytes`. We currently provide `into_bytes` as well as `to_bytes`, with one of them calling `clone` - this is unnecessary and against convention.

  Remove `into_bytes` and for `to_bytes` just return the inner field. Add code comment to remind devs of correct usage of the macro in regards to deriving `Copy` and `Clone`.

ACKs for top commit:
  Kixunil:
    ACK 8c4fa5ec3d
  apoelstra:
    ACK 8c4fa5ec3d

Tree-SHA512: 619acbccc5d86afeaea9f69e4b8d4290dcd8d2ccb09d5bb22f475a47f703d63a45c059b0261b1cc3d4c8d9ef0a508263e1e836fd3ba6fa08ff67ebb23eb63309
This commit is contained in:
Andrew Poelstra 2022-11-05 23:35:14 +00:00
commit fb22e5b9c3
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
1 changed files with 8 additions and 5 deletions

View File

@ -32,13 +32,16 @@ macro_rules! impl_array_newtype {
#[inline]
pub fn as_bytes(&self) -> &[$ty; $len] { &self.0 }
/// Returns a clone of the underlying bytes.
/// Returns the underlying bytes.
#[inline]
pub fn to_bytes(self) -> [$ty; $len] { self.0.clone() }
pub fn to_bytes(self) -> [$ty; $len] {
// We rely on `Copy` being implemented for $thing so conversion
// methods use the correct Rust naming conventions.
fn check_copy<T: Copy>() {}
check_copy::<$thing>();
/// Returns the underlying bytes (takes ownership).
#[inline]
pub fn into_bytes(self) -> [$ty; $len] { self.0 }
self.0
}
}
impl<'a> core::convert::From<&'a [$ty]> for $thing {