Skip to content

Commit 65bc766

Browse files
KamilaBorowskadpc
authored andcommitted
Use SliceIndex trait for SmallVec's Index trait (servo#166)
Requires Rust 1.28.0.
1 parent 860155d commit 65bc766

File tree

1 file changed

+11
-22
lines changed

1 file changed

+11
-22
lines changed

lib.rs

+11-22
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ use std::mem;
6767
use std::mem::MaybeUninit;
6868
use std::ops;
6969
use std::ptr;
70-
use std::slice;
70+
use std::slice::{self, SliceIndex};
7171

7272
/// Creates a [`SmallVec`] containing the arguments.
7373
///
@@ -1275,30 +1275,19 @@ impl<A: Array> From<A> for SmallVec<A> {
12751275
}
12761276
}
12771277

1278-
macro_rules! impl_index {
1279-
($index_type: ty, $output_type: ty) => {
1280-
impl<A: Array> ops::Index<$index_type> for SmallVec<A> {
1281-
type Output = $output_type;
1282-
#[inline]
1283-
fn index(&self, index: $index_type) -> &$output_type {
1284-
&(&**self)[index]
1285-
}
1286-
}
1278+
impl<A: Array, I: SliceIndex<[A::Item]>> ops::Index<I> for SmallVec<A> {
1279+
type Output = I::Output;
12871280

1288-
impl<A: Array> ops::IndexMut<$index_type> for SmallVec<A> {
1289-
#[inline]
1290-
fn index_mut(&mut self, index: $index_type) -> &mut $output_type {
1291-
&mut (&mut **self)[index]
1292-
}
1293-
}
1294-
};
1281+
fn index(&self, index: I) -> &I::Output {
1282+
&(**self)[index]
1283+
}
12951284
}
12961285

1297-
impl_index!(usize, A::Item);
1298-
impl_index!(ops::Range<usize>, [A::Item]);
1299-
impl_index!(ops::RangeFrom<usize>, [A::Item]);
1300-
impl_index!(ops::RangeTo<usize>, [A::Item]);
1301-
impl_index!(ops::RangeFull, [A::Item]);
1286+
impl<A: Array, I: SliceIndex<[A::Item]>> ops::IndexMut<I> for SmallVec<A> {
1287+
fn index_mut(&mut self, index: I) -> &mut I::Output {
1288+
&mut (&mut **self)[index]
1289+
}
1290+
}
13021291

13031292
impl<A: Array> ExtendFromSlice<A::Item> for SmallVec<A>
13041293
where

0 commit comments

Comments
 (0)