From 4d42e8e906a853c0254c02dc163c1437593eda30 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Fri, 18 Nov 2022 10:56:24 +1100 Subject: [PATCH] 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. --- secp256k1-sys/src/lib.rs | 4 ++++ secp256k1-sys/src/macros.rs | 10 ---------- secp256k1-sys/src/recovery.rs | 1 + src/key.rs | 1 + src/lib.rs | 1 + src/macros.rs | 9 --------- src/schnorr.rs | 1 + 7 files changed, 8 insertions(+), 19 deletions(-) diff --git a/secp256k1-sys/src/lib.rs b/secp256k1-sys/src/lib.rs index f92535e..6239d0b 100644 --- a/secp256k1-sys/src/lib.rs +++ b/secp256k1-sys/src/lib.rs @@ -133,6 +133,7 @@ impl SchnorrSigExtraParams { /// Library-internal representation of a Secp256k1 public key #[repr(C)] +#[derive(Copy, Clone)] pub struct PublicKey([c_uchar; 64]); impl_array_newtype!(PublicKey, c_uchar, 64); impl_raw_debug!(PublicKey); @@ -226,6 +227,7 @@ impl core::hash::Hash for PublicKey { /// Library-internal representation of a Secp256k1 signature #[repr(C)] +#[derive(Copy, Clone)] pub struct Signature([c_uchar; 64]); impl_array_newtype!(Signature, c_uchar, 64); impl_raw_debug!(Signature); @@ -313,6 +315,7 @@ impl core::hash::Hash for Signature { } #[repr(C)] +#[derive(Copy, Clone)] pub struct XOnlyPublicKey([c_uchar; 64]); impl_array_newtype!(XOnlyPublicKey, c_uchar, 64); impl_raw_debug!(XOnlyPublicKey); @@ -401,6 +404,7 @@ impl core::hash::Hash for XOnlyPublicKey { } #[repr(C)] +#[derive(Copy, Clone)] pub struct KeyPair([c_uchar; 96]); impl_array_newtype!(KeyPair, c_uchar, 96); impl_raw_debug!(KeyPair); diff --git a/secp256k1-sys/src/macros.rs b/secp256k1-sys/src/macros.rs index 247ee12..98499ff 100644 --- a/secp256k1-sys/src/macros.rs +++ b/secp256k1-sys/src/macros.rs @@ -17,8 +17,6 @@ #[macro_export] macro_rules! impl_array_newtype { ($thing:ident, $ty:ty, $len:expr) => { - impl Copy for $thing {} - impl $thing { /// 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 core::ops::Index for $thing where [$ty]: core::ops::Index, diff --git a/secp256k1-sys/src/recovery.rs b/secp256k1-sys/src/recovery.rs index 7a89112..76f0180 100644 --- a/secp256k1-sys/src/recovery.rs +++ b/secp256k1-sys/src/recovery.rs @@ -21,6 +21,7 @@ use core::fmt; /// Library-internal representation of a Secp256k1 signature + recovery ID #[repr(C)] +#[derive(Copy, Clone)] pub struct RecoverableSignature([c_uchar; 65]); impl_array_newtype!(RecoverableSignature, c_uchar, 65); diff --git a/src/key.rs b/src/key.rs index 910385b..8c3fae5 100644 --- a/src/key.rs +++ b/src/key.rs @@ -56,6 +56,7 @@ use crate::{hashes, ThirtyTwoByteHash}; /// ``` /// [`bincode`]: https://docs.rs/bincode /// [`cbor`]: https://docs.rs/cbor +#[derive(Copy, Clone)] pub struct SecretKey([u8; constants::SECRET_KEY_SIZE]); impl_array_newtype!(SecretKey, u8, constants::SECRET_KEY_SIZE); impl_display_secret!(SecretKey); diff --git a/src/lib.rs b/src/lib.rs index 6a334d5..d8c7862 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -233,6 +233,7 @@ impl ThirtyTwoByteHash for hashes::sha256t::Hash { } /// A (hashed) message input to an ECDSA signature. +#[derive(Copy, Clone)] pub struct Message([u8; constants::MESSAGE_SIZE]); impl_array_newtype!(Message, u8, constants::MESSAGE_SIZE); impl_pretty_debug!(Message); diff --git a/src/macros.rs b/src/macros.rs index 11ce90e..1ed2bd1 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -17,7 +17,6 @@ #[macro_export] macro_rules! impl_array_newtype { ($thing:ident, $ty:ty, $len:expr) => { - impl Copy for $thing {} impl AsRef<[$ty; $len]> for $thing { #[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 core::ops::Index for $thing where [$ty]: core::ops::Index, diff --git a/src/schnorr.rs b/src/schnorr.rs index 2d33ce9..4c208a0 100644 --- a/src/schnorr.rs +++ b/src/schnorr.rs @@ -14,6 +14,7 @@ use crate::SECP256K1; use crate::{constants, from_hex, impl_array_newtype, Error, Message, Secp256k1, Signing, Verification}; /// Represents a Schnorr signature. +#[derive(Copy, Clone)] pub struct Signature([u8; constants::SCHNORR_SIGNATURE_SIZE]); impl_array_newtype!(Signature, u8, constants::SCHNORR_SIGNATURE_SIZE); impl_pretty_debug!(Signature);