Derive Copy and Clone

There is no obvious reason why not to derive `Copy` and `Clone` for
types that use the `impl_newtype_macro`. Derives are less surprising so
deriving makes the code marginally easier to read.
This commit is contained in:
Tobin C. Harding 2022-11-18 10:56:24 +11:00
parent b38ae97eaf
commit 4d42e8e906
7 changed files with 8 additions and 19 deletions

View File

@ -133,6 +133,7 @@ impl SchnorrSigExtraParams {
/// Library-internal representation of a Secp256k1 public key /// Library-internal representation of a Secp256k1 public key
#[repr(C)] #[repr(C)]
#[derive(Copy, Clone)]
pub struct PublicKey([c_uchar; 64]); pub struct PublicKey([c_uchar; 64]);
impl_array_newtype!(PublicKey, c_uchar, 64); impl_array_newtype!(PublicKey, c_uchar, 64);
impl_raw_debug!(PublicKey); impl_raw_debug!(PublicKey);
@ -226,6 +227,7 @@ impl core::hash::Hash for PublicKey {
/// Library-internal representation of a Secp256k1 signature /// Library-internal representation of a Secp256k1 signature
#[repr(C)] #[repr(C)]
#[derive(Copy, Clone)]
pub struct Signature([c_uchar; 64]); pub struct Signature([c_uchar; 64]);
impl_array_newtype!(Signature, c_uchar, 64); impl_array_newtype!(Signature, c_uchar, 64);
impl_raw_debug!(Signature); impl_raw_debug!(Signature);
@ -313,6 +315,7 @@ impl core::hash::Hash for Signature {
} }
#[repr(C)] #[repr(C)]
#[derive(Copy, Clone)]
pub struct XOnlyPublicKey([c_uchar; 64]); pub struct XOnlyPublicKey([c_uchar; 64]);
impl_array_newtype!(XOnlyPublicKey, c_uchar, 64); impl_array_newtype!(XOnlyPublicKey, c_uchar, 64);
impl_raw_debug!(XOnlyPublicKey); impl_raw_debug!(XOnlyPublicKey);
@ -401,6 +404,7 @@ impl core::hash::Hash for XOnlyPublicKey {
} }
#[repr(C)] #[repr(C)]
#[derive(Copy, Clone)]
pub struct KeyPair([c_uchar; 96]); pub struct KeyPair([c_uchar; 96]);
impl_array_newtype!(KeyPair, c_uchar, 96); impl_array_newtype!(KeyPair, c_uchar, 96);
impl_raw_debug!(KeyPair); impl_raw_debug!(KeyPair);

View File

@ -17,8 +17,6 @@
#[macro_export] #[macro_export]
macro_rules! impl_array_newtype { macro_rules! impl_array_newtype {
($thing:ident, $ty:ty, $len:expr) => { ($thing:ident, $ty:ty, $len:expr) => {
impl Copy for $thing {}
impl $thing { impl $thing {
/// Like `cmp::Ord` but faster and with no guarantees across library versions. /// Like `cmp::Ord` but faster and with no guarantees across library versions.
/// ///
@ -92,14 +90,6 @@ macro_rules! impl_array_newtype {
} }
} }
impl Clone for $thing {
#[inline]
fn clone(&self) -> $thing {
let &$thing(ref dat) = self;
$thing(dat.clone())
}
}
impl<I> core::ops::Index<I> for $thing impl<I> core::ops::Index<I> for $thing
where where
[$ty]: core::ops::Index<I>, [$ty]: core::ops::Index<I>,

View File

@ -21,6 +21,7 @@ use core::fmt;
/// Library-internal representation of a Secp256k1 signature + recovery ID /// Library-internal representation of a Secp256k1 signature + recovery ID
#[repr(C)] #[repr(C)]
#[derive(Copy, Clone)]
pub struct RecoverableSignature([c_uchar; 65]); pub struct RecoverableSignature([c_uchar; 65]);
impl_array_newtype!(RecoverableSignature, c_uchar, 65); impl_array_newtype!(RecoverableSignature, c_uchar, 65);

View File

@ -56,6 +56,7 @@ use crate::{hashes, ThirtyTwoByteHash};
/// ``` /// ```
/// [`bincode`]: https://docs.rs/bincode /// [`bincode`]: https://docs.rs/bincode
/// [`cbor`]: https://docs.rs/cbor /// [`cbor`]: https://docs.rs/cbor
#[derive(Copy, Clone)]
pub struct SecretKey([u8; constants::SECRET_KEY_SIZE]); pub struct SecretKey([u8; constants::SECRET_KEY_SIZE]);
impl_array_newtype!(SecretKey, u8, constants::SECRET_KEY_SIZE); impl_array_newtype!(SecretKey, u8, constants::SECRET_KEY_SIZE);
impl_display_secret!(SecretKey); impl_display_secret!(SecretKey);

View File

@ -233,6 +233,7 @@ impl<T: hashes::sha256t::Tag> ThirtyTwoByteHash for hashes::sha256t::Hash<T> {
} }
/// A (hashed) message input to an ECDSA signature. /// A (hashed) message input to an ECDSA signature.
#[derive(Copy, Clone)]
pub struct Message([u8; constants::MESSAGE_SIZE]); pub struct Message([u8; constants::MESSAGE_SIZE]);
impl_array_newtype!(Message, u8, constants::MESSAGE_SIZE); impl_array_newtype!(Message, u8, constants::MESSAGE_SIZE);
impl_pretty_debug!(Message); impl_pretty_debug!(Message);

View File

@ -17,7 +17,6 @@
#[macro_export] #[macro_export]
macro_rules! impl_array_newtype { macro_rules! impl_array_newtype {
($thing:ident, $ty:ty, $len:expr) => { ($thing:ident, $ty:ty, $len:expr) => {
impl Copy for $thing {}
impl AsRef<[$ty; $len]> for $thing { impl AsRef<[$ty; $len]> for $thing {
#[inline] #[inline]
@ -59,14 +58,6 @@ macro_rules! impl_array_newtype {
} }
} }
impl Clone for $thing {
#[inline]
fn clone(&self) -> $thing {
let &$thing(ref dat) = self;
$thing(dat.clone())
}
}
impl<I> core::ops::Index<I> for $thing impl<I> core::ops::Index<I> for $thing
where where
[$ty]: core::ops::Index<I>, [$ty]: core::ops::Index<I>,

View File

@ -14,6 +14,7 @@ use crate::SECP256K1;
use crate::{constants, from_hex, impl_array_newtype, Error, Message, Secp256k1, Signing, Verification}; use crate::{constants, from_hex, impl_array_newtype, Error, Message, Secp256k1, Signing, Verification};
/// Represents a Schnorr signature. /// Represents a Schnorr signature.
#[derive(Copy, Clone)]
pub struct Signature([u8; constants::SCHNORR_SIGNATURE_SIZE]); pub struct Signature([u8; constants::SCHNORR_SIGNATURE_SIZE]);
impl_array_newtype!(Signature, u8, constants::SCHNORR_SIGNATURE_SIZE); impl_array_newtype!(Signature, u8, constants::SCHNORR_SIGNATURE_SIZE);
impl_pretty_debug!(Signature); impl_pretty_debug!(Signature);