Merge rust-bitcoin/rust-secp256k1#503: Use generic implementation of `core::ops::Index`

1c17d0f215 Improve docs on impl_array_newtype (Tobin C. Harding)
91ac518d17 Use generic implementation of Index (Tobin C. Harding)

Pull request description:

  Instead of all the manual implementations of `Index` for ranged types we can just use a generic implementation as we do in `rust-bitcoin/internals/src/macros.rs`.

  Patch 2 does some trivial docs improvements to the `impl_array_newtype` macro since we are touching it anyways.

ACKs for top commit:
  apoelstra:
    ACK 1c17d0f215

Tree-SHA512: 6b37933659841af51c8abed3caeca83e63972d82be0a6483d7cdb804242986075f3d93e72b73072d496097224ed8130b6eee6858bf9d76205df4016ff012fa00
This commit is contained in:
Andrew Poelstra 2022-11-04 23:42:36 +00:00
commit 6f2edc7eee
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
1 changed files with 11 additions and 50 deletions

View File

@ -13,32 +13,32 @@
// If not, see <http://creativecommons.org/publicdomain/zero/1.0/>. // If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
// //
// This is a macro that routinely comes in handy /// Implement methods and traits for types that contain an inner array.
#[macro_export] #[macro_export]
macro_rules! impl_array_newtype { macro_rules! impl_array_newtype {
($thing:ident, $ty:ty, $len:expr) => { ($thing:ident, $ty:ty, $len:expr) => {
impl Copy for $thing {} impl Copy for $thing {}
impl $thing { impl $thing {
/// Converts the object to a raw pointer for FFI interfacing /// Converts the object to a raw pointer for FFI interfacing.
#[inline] #[inline]
pub fn as_ptr(&self) -> *const $ty { pub fn as_ptr(&self) -> *const $ty {
let &$thing(ref dat) = self; let &$thing(ref dat) = self;
dat.as_ptr() 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] #[inline]
pub fn as_mut_ptr(&mut self) -> *mut $ty { pub fn as_mut_ptr(&mut self) -> *mut $ty {
let &mut $thing(ref mut dat) = self; let &mut $thing(ref mut dat) = self;
dat.as_mut_ptr() dat.as_mut_ptr()
} }
/// Returns the length of the object as an array /// Returns the length of the object as an array.
#[inline] #[inline]
pub fn len(&self) -> usize { $len } 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] #[inline]
pub fn is_empty(&self) -> bool { false } pub fn is_empty(&self) -> bool { false }
} }
@ -89,55 +89,16 @@ macro_rules! impl_array_newtype {
} }
} }
impl core::ops::Index<usize> for $thing { impl<I> core::ops::Index<I> for $thing
type Output = $ty; where
[$ty]: core::ops::Index<I>,
{
type Output = <[$ty] as core::ops::Index<I>>::Output;
#[inline] #[inline]
fn index(&self, index: usize) -> &$ty { fn index(&self, index: I) -> &Self::Output { &self.0[index] }
let &$thing(ref dat) = self;
&dat[index]
}
} }
impl core::ops::Index<core::ops::Range<usize>> for $thing {
type Output = [$ty];
#[inline]
fn index(&self, index: core::ops::Range<usize>) -> &[$ty] {
let &$thing(ref dat) = self;
&dat[index]
}
}
impl core::ops::Index<core::ops::RangeTo<usize>> for $thing {
type Output = [$ty];
#[inline]
fn index(&self, index: core::ops::RangeTo<usize>) -> &[$ty] {
let &$thing(ref dat) = self;
&dat[index]
}
}
impl core::ops::Index<core::ops::RangeFrom<usize>> for $thing {
type Output = [$ty];
#[inline]
fn index(&self, index: core::ops::RangeFrom<usize>) -> &[$ty] {
let &$thing(ref dat) = self;
&dat[index]
}
}
impl core::ops::Index<core::ops::RangeFull> for $thing {
type Output = [$ty];
#[inline]
fn index(&self, _: core::ops::RangeFull) -> &[$ty] {
let &$thing(ref dat) = self;
&dat[..]
}
}
impl $crate::CPtr for $thing { impl $crate::CPtr for $thing {
type Target = $ty; type Target = $ty;
fn as_c_ptr(&self) -> *const Self::Target { fn as_c_ptr(&self) -> *const Self::Target {