Use generic implementation of Index

We can use a generic implementation of `core::ops::Index` which gives us
all the range impls for free.
This commit is contained in:
Tobin C. Harding 2022-11-03 14:31:08 +11:00
parent 15a8c20427
commit 91ac518d17
1 changed files with 6 additions and 45 deletions

View File

@ -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 {