Skip to content

Commit 4a0c7fc

Browse files
committed
Do not use impl_array_newtype for SecretKey
In preparation for changing the logic of comparison trait (Ord, Eq) implementations on the `SecretKey` copy all the code out of `impl_array_newtype` and implement it directly in `key.rs`. Refactor only, no logic changes (although I removed a few unneeded references).
1 parent d5065cc commit 4a0c7fc

File tree

1 file changed

+64
-3
lines changed

1 file changed

+64
-3
lines changed

src/key.rs

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
//!
1818
1919
use core::convert::TryFrom;
20-
use core::ops::BitXor;
20+
use core::ops::{self, BitXor};
2121
use core::{fmt, ptr, str};
2222

2323
#[cfg(feature = "serde")]
@@ -28,7 +28,7 @@ use crate::ffi::{self, CPtr};
2828
#[cfg(all(feature = "global-context", feature = "rand-std"))]
2929
use crate::schnorr;
3030
use crate::Error::{self, InvalidPublicKey, InvalidPublicKeySum, InvalidSecretKey};
31-
use crate::{constants, from_hex, impl_array_newtype, Scalar, Secp256k1, Signing, Verification};
31+
use crate::{constants, from_hex, Scalar, Secp256k1, Signing, Verification};
3232
#[cfg(feature = "global-context")]
3333
use crate::{ecdsa, Message, SECP256K1};
3434
#[cfg(feature = "bitcoin-hashes")]
@@ -58,9 +58,70 @@ use crate::{hashes, ThirtyTwoByteHash};
5858
/// [`cbor`]: https://docs.rs/cbor
5959
#[derive(Copy, Clone)]
6060
pub struct SecretKey([u8; constants::SECRET_KEY_SIZE]);
61-
impl_array_newtype!(SecretKey, u8, constants::SECRET_KEY_SIZE);
6261
impl_display_secret!(SecretKey);
6362

63+
impl PartialEq for SecretKey {
64+
#[inline]
65+
fn eq(&self, other: &Self) -> bool {
66+
self[..] == other[..]
67+
}
68+
}
69+
70+
impl Eq for SecretKey {}
71+
72+
impl core::hash::Hash for SecretKey {
73+
fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
74+
self[..].hash(state)
75+
}
76+
}
77+
78+
impl PartialOrd for SecretKey {
79+
#[inline]
80+
fn partial_cmp(&self, other: &SecretKey) -> Option<core::cmp::Ordering> {
81+
self[..].partial_cmp(&other[..])
82+
}
83+
}
84+
85+
impl Ord for SecretKey {
86+
#[inline]
87+
fn cmp(&self, other: &SecretKey) -> core::cmp::Ordering {
88+
self[..].cmp(&other[..])
89+
}
90+
}
91+
92+
impl AsRef<[u8; constants::SECRET_KEY_SIZE]> for SecretKey {
93+
/// Gets a reference to the underlying array
94+
#[inline]
95+
fn as_ref(&self) -> &[u8; constants::SECRET_KEY_SIZE] {
96+
let &SecretKey(ref dat) = self;
97+
dat
98+
}
99+
}
100+
101+
impl<I> ops::Index<I> for SecretKey
102+
where
103+
[u8]: ops::Index<I>,
104+
{
105+
type Output = <[u8] as ops::Index<I>>::Output;
106+
107+
#[inline]
108+
fn index(&self, index: I) -> &Self::Output { &self.0[index] }
109+
}
110+
111+
impl ffi::CPtr for SecretKey {
112+
type Target = u8;
113+
114+
fn as_c_ptr(&self) -> *const Self::Target {
115+
let &SecretKey(ref dat) = self;
116+
dat.as_ptr()
117+
}
118+
119+
fn as_mut_c_ptr(&mut self) -> *mut Self::Target {
120+
let &mut SecretKey(ref mut dat) = self;
121+
dat.as_mut_ptr()
122+
}
123+
}
124+
64125
impl str::FromStr for SecretKey {
65126
type Err = Error;
66127
fn from_str(s: &str) -> Result<SecretKey, Error> {

0 commit comments

Comments
 (0)