Skip to content

Commit 4d42e8e

Browse files
committed
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.
1 parent b38ae97 commit 4d42e8e

File tree

7 files changed

+8
-19
lines changed

7 files changed

+8
-19
lines changed

secp256k1-sys/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ impl SchnorrSigExtraParams {
133133

134134
/// Library-internal representation of a Secp256k1 public key
135135
#[repr(C)]
136+
#[derive(Copy, Clone)]
136137
pub struct PublicKey([c_uchar; 64]);
137138
impl_array_newtype!(PublicKey, c_uchar, 64);
138139
impl_raw_debug!(PublicKey);
@@ -226,6 +227,7 @@ impl core::hash::Hash for PublicKey {
226227

227228
/// Library-internal representation of a Secp256k1 signature
228229
#[repr(C)]
230+
#[derive(Copy, Clone)]
229231
pub struct Signature([c_uchar; 64]);
230232
impl_array_newtype!(Signature, c_uchar, 64);
231233
impl_raw_debug!(Signature);
@@ -313,6 +315,7 @@ impl core::hash::Hash for Signature {
313315
}
314316

315317
#[repr(C)]
318+
#[derive(Copy, Clone)]
316319
pub struct XOnlyPublicKey([c_uchar; 64]);
317320
impl_array_newtype!(XOnlyPublicKey, c_uchar, 64);
318321
impl_raw_debug!(XOnlyPublicKey);
@@ -401,6 +404,7 @@ impl core::hash::Hash for XOnlyPublicKey {
401404
}
402405

403406
#[repr(C)]
407+
#[derive(Copy, Clone)]
404408
pub struct KeyPair([c_uchar; 96]);
405409
impl_array_newtype!(KeyPair, c_uchar, 96);
406410
impl_raw_debug!(KeyPair);

secp256k1-sys/src/macros.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
#[macro_export]
1818
macro_rules! impl_array_newtype {
1919
($thing:ident, $ty:ty, $len:expr) => {
20-
impl Copy for $thing {}
21-
2220
impl $thing {
2321
/// Like `cmp::Ord` but faster and with no guarantees across library versions.
2422
///
@@ -92,14 +90,6 @@ macro_rules! impl_array_newtype {
9290
}
9391
}
9492

95-
impl Clone for $thing {
96-
#[inline]
97-
fn clone(&self) -> $thing {
98-
let &$thing(ref dat) = self;
99-
$thing(dat.clone())
100-
}
101-
}
102-
10393
impl<I> core::ops::Index<I> for $thing
10494
where
10595
[$ty]: core::ops::Index<I>,

secp256k1-sys/src/recovery.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use core::fmt;
2121

2222
/// Library-internal representation of a Secp256k1 signature + recovery ID
2323
#[repr(C)]
24+
#[derive(Copy, Clone)]
2425
pub struct RecoverableSignature([c_uchar; 65]);
2526
impl_array_newtype!(RecoverableSignature, c_uchar, 65);
2627

src/key.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ use crate::{hashes, ThirtyTwoByteHash};
5656
/// ```
5757
/// [`bincode`]: https://docs.rs/bincode
5858
/// [`cbor`]: https://docs.rs/cbor
59+
#[derive(Copy, Clone)]
5960
pub struct SecretKey([u8; constants::SECRET_KEY_SIZE]);
6061
impl_array_newtype!(SecretKey, u8, constants::SECRET_KEY_SIZE);
6162
impl_display_secret!(SecretKey);

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ impl<T: hashes::sha256t::Tag> ThirtyTwoByteHash for hashes::sha256t::Hash<T> {
233233
}
234234

235235
/// A (hashed) message input to an ECDSA signature.
236+
#[derive(Copy, Clone)]
236237
pub struct Message([u8; constants::MESSAGE_SIZE]);
237238
impl_array_newtype!(Message, u8, constants::MESSAGE_SIZE);
238239
impl_pretty_debug!(Message);

src/macros.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#[macro_export]
1818
macro_rules! impl_array_newtype {
1919
($thing:ident, $ty:ty, $len:expr) => {
20-
impl Copy for $thing {}
2120

2221
impl AsRef<[$ty; $len]> for $thing {
2322
#[inline]
@@ -59,14 +58,6 @@ macro_rules! impl_array_newtype {
5958
}
6059
}
6160

62-
impl Clone for $thing {
63-
#[inline]
64-
fn clone(&self) -> $thing {
65-
let &$thing(ref dat) = self;
66-
$thing(dat.clone())
67-
}
68-
}
69-
7061
impl<I> core::ops::Index<I> for $thing
7162
where
7263
[$ty]: core::ops::Index<I>,

src/schnorr.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use crate::SECP256K1;
1414
use crate::{constants, from_hex, impl_array_newtype, Error, Message, Secp256k1, Signing, Verification};
1515

1616
/// Represents a Schnorr signature.
17+
#[derive(Copy, Clone)]
1718
pub struct Signature([u8; constants::SCHNORR_SIGNATURE_SIZE]);
1819
impl_array_newtype!(Signature, u8, constants::SCHNORR_SIGNATURE_SIZE);
1920
impl_pretty_debug!(Signature);

0 commit comments

Comments
 (0)