Skip to content

Commit 6f2edc7

Browse files
committed
Merge #503: Use generic implementation of core::ops::Index
1c17d0f Improve docs on impl_array_newtype (Tobin C. Harding) 91ac518 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 1c17d0f Tree-SHA512: 6b37933659841af51c8abed3caeca83e63972d82be0a6483d7cdb804242986075f3d93e72b73072d496097224ed8130b6eee6858bf9d76205df4016ff012fa00
2 parents a08029e + 1c17d0f commit 6f2edc7

File tree

1 file changed

+11
-50
lines changed

1 file changed

+11
-50
lines changed

secp256k1-sys/src/macros.rs

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

16-
// This is a macro that routinely comes in handy
16+
/// Implement methods and traits for types that contain an inner array.
1717
#[macro_export]
1818
macro_rules! impl_array_newtype {
1919
($thing:ident, $ty:ty, $len:expr) => {
2020
impl Copy for $thing {}
2121

2222
impl $thing {
23-
/// Converts the object to a raw pointer for FFI interfacing
23+
/// Converts the object to a raw pointer for FFI interfacing.
2424
#[inline]
2525
pub fn as_ptr(&self) -> *const $ty {
2626
let &$thing(ref dat) = self;
2727
dat.as_ptr()
2828
}
2929

30-
/// Converts the object to a mutable raw pointer for FFI interfacing
30+
/// Converts the object to a mutable raw pointer for FFI interfacing.
3131
#[inline]
3232
pub fn as_mut_ptr(&mut self) -> *mut $ty {
3333
let &mut $thing(ref mut dat) = self;
3434
dat.as_mut_ptr()
3535
}
3636

37-
/// Returns the length of the object as an array
37+
/// Returns the length of the object as an array.
3838
#[inline]
3939
pub fn len(&self) -> usize { $len }
4040

41-
/// Returns whether the object as an array is empty
41+
/// Returns whether the object as an array is empty.
4242
#[inline]
4343
pub fn is_empty(&self) -> bool { false }
4444
}
@@ -89,55 +89,16 @@ macro_rules! impl_array_newtype {
8989
}
9090
}
9191

92-
impl core::ops::Index<usize> for $thing {
93-
type Output = $ty;
92+
impl<I> core::ops::Index<I> for $thing
93+
where
94+
[$ty]: core::ops::Index<I>,
95+
{
96+
type Output = <[$ty] as core::ops::Index<I>>::Output;
9497

9598
#[inline]
96-
fn index(&self, index: usize) -> &$ty {
97-
let &$thing(ref dat) = self;
98-
&dat[index]
99-
}
99+
fn index(&self, index: I) -> &Self::Output { &self.0[index] }
100100
}
101101

102-
impl core::ops::Index<core::ops::Range<usize>> for $thing {
103-
type Output = [$ty];
104-
105-
#[inline]
106-
fn index(&self, index: core::ops::Range<usize>) -> &[$ty] {
107-
let &$thing(ref dat) = self;
108-
&dat[index]
109-
}
110-
}
111-
112-
impl core::ops::Index<core::ops::RangeTo<usize>> for $thing {
113-
type Output = [$ty];
114-
115-
#[inline]
116-
fn index(&self, index: core::ops::RangeTo<usize>) -> &[$ty] {
117-
let &$thing(ref dat) = self;
118-
&dat[index]
119-
}
120-
}
121-
122-
impl core::ops::Index<core::ops::RangeFrom<usize>> for $thing {
123-
type Output = [$ty];
124-
125-
#[inline]
126-
fn index(&self, index: core::ops::RangeFrom<usize>) -> &[$ty] {
127-
let &$thing(ref dat) = self;
128-
&dat[index]
129-
}
130-
}
131-
132-
impl core::ops::Index<core::ops::RangeFull> for $thing {
133-
type Output = [$ty];
134-
135-
#[inline]
136-
fn index(&self, _: core::ops::RangeFull) -> &[$ty] {
137-
let &$thing(ref dat) = self;
138-
&dat[..]
139-
}
140-
}
141102
impl $crate::CPtr for $thing {
142103
type Target = $ty;
143104
fn as_c_ptr(&self) -> *const Self::Target {

0 commit comments

Comments
 (0)