Skip to content

Commit 72ef809

Browse files
committed
Improving safe ptr type implementation
1 parent 800ffed commit 72ef809

File tree

2 files changed

+19
-25
lines changed

2 files changed

+19
-25
lines changed

secp256k1-sys/src/macros.rs

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
// If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
1414
//
1515

16-
/// Implements array accessing methods for a type that must be considered safe
16+
/// Implements newtype wrapping methods
1717
#[macro_export]
18-
macro_rules! impl_safe_array_newtype {
19-
($thing:ident, $ty:ty, $len:expr) => {
18+
macro_rules! impl_ptr_newtype {
19+
($thing:ident, $ty:ty) => {
2020
impl $thing {
2121
#[inline]
2222
#[allow(unused)]
@@ -33,35 +33,19 @@ macro_rules! impl_safe_array_newtype {
3333
let &mut $thing(ref mut dat) = self;
3434
dat.as_mut_ptr()
3535
}
36-
37-
#[inline]
38-
/// Returns the length of the object as an array
39-
pub fn len(&self) -> usize { $len }
40-
41-
#[inline]
42-
/// Returns whether the object as an array is empty
43-
pub fn is_empty(&self) -> bool { false }
4436
}
4537

4638
impl $crate::CPtr for $thing {
4739
type Target = $ty;
4840

4941
fn as_c_ptr(&self) -> *const Self::Target {
50-
if self.is_empty() {
51-
::core::ptr::null()
52-
} else {
53-
let &$thing(ref dat) = self;
54-
dat.as_ptr()
55-
}
42+
let &$thing(ref dat) = self;
43+
dat.as_ptr()
5644
}
5745

5846
fn as_mut_c_ptr(&mut self) -> *mut Self::Target {
59-
if self.is_empty() {
60-
::core::ptr::null::<Self::Target>() as *mut _
61-
} else {
62-
let &mut $thing(ref mut dat) = self;
63-
dat.as_mut_ptr()
64-
}
47+
let &mut $thing(ref mut dat) = self;
48+
dat.as_mut_ptr()
6549
}
6650
}
6751
}
@@ -72,7 +56,17 @@ macro_rules! impl_safe_array_newtype {
7256
#[macro_export]
7357
macro_rules! impl_array_newtype {
7458
($thing:ident, $ty:ty, $len:expr) => {
75-
impl_safe_array_newtype!($thing, $ty, $len);
59+
impl_ptr_newtype!($thing, $ty);
60+
61+
impl $thing {
62+
#[inline]
63+
/// Returns the length of the object as an array
64+
pub fn len(&self) -> usize { $len }
65+
66+
#[inline]
67+
/// Returns whether the object as an array is empty
68+
pub fn is_empty(&self) -> bool { false }
69+
}
7670

7771
impl Copy for $thing {}
7872

src/key.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use ffi::{self, CPtr};
2929
/// Secret 256-bit key used as `x` in an ECDSA signature
3030
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord)]
3131
pub struct SecretKey(pub(crate) [u8; constants::SECRET_KEY_SIZE]);
32-
impl_safe_array_newtype!(SecretKey, u8, constants::SECRET_KEY_SIZE);
32+
impl_ptr_newtype!(SecretKey, u8);
3333
impl_safe_debug!(SecretKey);
3434

3535
impl str::FromStr for SecretKey {

0 commit comments

Comments
 (0)