From 91ac518d178731c3969c20fcb43c17aba650eb31 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Thu, 3 Nov 2022 14:31:08 +1100 Subject: [PATCH 1/2] Use generic implementation of Index We can use a generic implementation of `core::ops::Index` which gives us all the range impls for free. --- secp256k1-sys/src/macros.rs | 51 +++++-------------------------------- 1 file changed, 6 insertions(+), 45 deletions(-) diff --git a/secp256k1-sys/src/macros.rs b/secp256k1-sys/src/macros.rs index 1089e79..3ae70af 100644 --- a/secp256k1-sys/src/macros.rs +++ b/secp256k1-sys/src/macros.rs @@ -89,55 +89,16 @@ macro_rules! impl_array_newtype { } } - impl core::ops::Index for $thing { - type Output = $ty; + impl core::ops::Index for $thing + where + [$ty]: core::ops::Index, + { + type Output = <[$ty] as core::ops::Index>::Output; #[inline] - fn index(&self, index: usize) -> &$ty { - let &$thing(ref dat) = self; - &dat[index] - } + fn index(&self, index: I) -> &Self::Output { &self.0[index] } } - impl core::ops::Index> for $thing { - type Output = [$ty]; - - #[inline] - fn index(&self, index: core::ops::Range) -> &[$ty] { - let &$thing(ref dat) = self; - &dat[index] - } - } - - impl core::ops::Index> for $thing { - type Output = [$ty]; - - #[inline] - fn index(&self, index: core::ops::RangeTo) -> &[$ty] { - let &$thing(ref dat) = self; - &dat[index] - } - } - - impl core::ops::Index> for $thing { - type Output = [$ty]; - - #[inline] - fn index(&self, index: core::ops::RangeFrom) -> &[$ty] { - let &$thing(ref dat) = self; - &dat[index] - } - } - - impl core::ops::Index for $thing { - type Output = [$ty]; - - #[inline] - fn index(&self, _: core::ops::RangeFull) -> &[$ty] { - let &$thing(ref dat) = self; - &dat[..] - } - } impl $crate::CPtr for $thing { type Target = $ty; fn as_c_ptr(&self) -> *const Self::Target { From 1c17d0f215dfa705e11eb1a0d8e1205bce794388 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Thu, 3 Nov 2022 14:36:17 +1100 Subject: [PATCH 2/2] Improve docs on impl_array_newtype Improve the rustdocs on the `impl_array_newtype` macro by adding full stops and re-writing the outer comment. --- secp256k1-sys/src/macros.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/secp256k1-sys/src/macros.rs b/secp256k1-sys/src/macros.rs index 3ae70af..e3850f0 100644 --- a/secp256k1-sys/src/macros.rs +++ b/secp256k1-sys/src/macros.rs @@ -13,32 +13,32 @@ // If not, see . // -// This is a macro that routinely comes in handy +/// Implement methods and traits for types that contain an inner array. #[macro_export] macro_rules! impl_array_newtype { ($thing:ident, $ty:ty, $len:expr) => { impl Copy for $thing {} impl $thing { - /// Converts the object to a raw pointer for FFI interfacing + /// 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 + /// 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 + /// Returns the length of the object as an array. #[inline] pub fn len(&self) -> usize { $len } - /// Returns whether the object as an array is empty + /// Returns whether the object as an array is empty. #[inline] pub fn is_empty(&self) -> bool { false } }