Skip to content

Commit

Permalink
Derive Copy and Clone
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tcharding committed Nov 17, 2022
1 parent b38ae97 commit 4d42e8e
Show file tree
Hide file tree
Showing 7 changed files with 8 additions and 19 deletions.
4 changes: 4 additions & 0 deletions secp256k1-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
10 changes: 0 additions & 10 deletions secp256k1-sys/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down Expand Up @@ -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
where
[$ty]: core::ops::Index<I>,
Expand Down
1 change: 1 addition & 0 deletions secp256k1-sys/src/recovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
1 change: 1 addition & 0 deletions src/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ impl<T: hashes::sha256t::Tag> ThirtyTwoByteHash for hashes::sha256t::Hash<T> {
}

/// 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);
Expand Down
9 changes: 0 additions & 9 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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
where
[$ty]: core::ops::Index<I>,
Expand Down
1 change: 1 addition & 0 deletions src/schnorr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 4d42e8e

Please sign in to comment.