From 2bb08c21e5b8f360929ac6d16401285ac1aa26a9 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Thu, 17 Nov 2022 10:52:36 +1100 Subject: [PATCH] Remove as_[mut_]ptr from impl_array_newtype macros For interfacing with the FFI layer we implement `ffi::CPtr`, there is not need to provide methods `as_ptr` and `as_mut_ptr` as well. --- secp256k1-sys/src/macros.rs | 29 +++++------------------------ src/ecdh.rs | 2 +- src/macros.rs | 31 ++++++------------------------- 3 files changed, 12 insertions(+), 50 deletions(-) diff --git a/secp256k1-sys/src/macros.rs b/secp256k1-sys/src/macros.rs index e3850f0..6d2a0c3 100644 --- a/secp256k1-sys/src/macros.rs +++ b/secp256k1-sys/src/macros.rs @@ -20,20 +20,6 @@ macro_rules! impl_array_newtype { impl Copy for $thing {} impl $thing { - /// Converts the object to a raw pointer for FFI interfacing. - #[inline] - pub fn as_ptr(&self) -> *const $ty { - let &$thing(ref dat) = self; - dat.as_ptr() - } - - /// Converts the object to a mutable raw pointer for FFI interfacing. - #[inline] - pub fn as_mut_ptr(&mut self) -> *mut $ty { - let &mut $thing(ref mut dat) = self; - dat.as_mut_ptr() - } - /// Returns the length of the object as an array. #[inline] pub fn len(&self) -> usize { $len } @@ -101,20 +87,15 @@ macro_rules! impl_array_newtype { impl $crate::CPtr for $thing { type Target = $ty; + fn as_c_ptr(&self) -> *const Self::Target { - if self.is_empty() { - core::ptr::null() - } else { - self.as_ptr() - } + let &$thing(ref dat) = self; + dat.as_ptr() } fn as_mut_c_ptr(&mut self) -> *mut Self::Target { - if self.is_empty() { - core::ptr::null::() as *mut _ - } else { - self.as_mut_ptr() - } + let &mut $thing(ref mut dat) = self; + dat.as_mut_ptr() } } } diff --git a/src/ecdh.rs b/src/ecdh.rs index c93a5e6..31e7039 100644 --- a/src/ecdh.rs +++ b/src/ecdh.rs @@ -146,7 +146,7 @@ pub fn shared_secret_point(point: &PublicKey, scalar: &SecretKey) -> [u8; 64] { ffi::secp256k1_context_no_precomp, xy.as_mut_ptr(), point.as_c_ptr(), - scalar.as_ptr(), + scalar.as_c_ptr(), Some(c_callback), ptr::null_mut(), ) diff --git a/src/macros.rs b/src/macros.rs index fbeb6b7..c028e23 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -20,20 +20,6 @@ macro_rules! impl_array_newtype { impl Copy for $thing {} impl $thing { - /// Converts the object to a raw pointer for FFI interfacing. - #[inline] - pub fn as_ptr(&self) -> *const $ty { - let &$thing(ref dat) = self; - dat.as_ptr() - } - - /// Converts the object to a mutable raw pointer for FFI interfacing. - #[inline] - pub fn as_mut_ptr(&mut self) -> *mut $ty { - let &mut $thing(ref mut dat) = self; - dat.as_mut_ptr() - } - /// Returns the length of the object as an array. #[inline] pub fn len(&self) -> usize { $len } @@ -99,22 +85,17 @@ macro_rules! impl_array_newtype { fn index(&self, index: I) -> &Self::Output { &self.0[index] } } - impl $crate::CPtr for $thing { + impl $crate::ffi::CPtr for $thing { type Target = $ty; + fn as_c_ptr(&self) -> *const Self::Target { - if self.is_empty() { - core::ptr::null() - } else { - self.as_ptr() - } + let &$thing(ref dat) = self; + dat.as_ptr() } fn as_mut_c_ptr(&mut self) -> *mut Self::Target { - if self.is_empty() { - core::ptr::null::() as *mut _ - } else { - self.as_mut_ptr() - } + let &mut $thing(ref mut dat) = self; + dat.as_mut_ptr() } } }