Skip to content

Commit

Permalink
Remove as_[mut_]ptr from impl_array_newtype macros
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tcharding committed Nov 17, 2022
1 parent 3e28070 commit 2bb08c2
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 50 deletions.
29 changes: 5 additions & 24 deletions secp256k1-sys/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down Expand Up @@ -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::<Self::Target>() as *mut _
} else {
self.as_mut_ptr()
}
let &mut $thing(ref mut dat) = self;
dat.as_mut_ptr()
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/ecdh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
)
Expand Down
31 changes: 6 additions & 25 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down Expand Up @@ -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::<Self::Target>() as *mut _
} else {
self.as_mut_ptr()
}
let &mut $thing(ref mut dat) = self;
dat.as_mut_ptr()
}
}
}
Expand Down

0 comments on commit 2bb08c2

Please sign in to comment.