Skip to content

Commit

Permalink
Use SliceIndex trait for SmallVec's Index trait
Browse files Browse the repository at this point in the history
Requires Rust 1.28.0.
  • Loading branch information
KamilaBorowska committed Oct 4, 2019
1 parent 3ee6d1e commit 4f127e5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: rust
rust:
- 1.20.0
- 1.36.0
- nightly
- beta
- stable
Expand Down
30 changes: 10 additions & 20 deletions lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ use std::mem::ManuallyDrop;
use std::ops;
use std::ptr;
use std::slice;
use std::slice::SliceIndex;
#[cfg(feature = "std")]
use std::io;
#[cfg(feature = "serde")]
Expand Down Expand Up @@ -1291,30 +1292,19 @@ impl<A: Array> From<A> for SmallVec<A> {
}
}

macro_rules! impl_index {
($index_type: ty, $output_type: ty) => {
impl<A: Array> ops::Index<$index_type> for SmallVec<A> {
type Output = $output_type;
#[inline]
fn index(&self, index: $index_type) -> &$output_type {
&(&**self)[index]
}
}
impl<A: Array, I: SliceIndex<[A::Item]>> ops::Index<I> for SmallVec<A> {
type Output = I::Output;

impl<A: Array> ops::IndexMut<$index_type> for SmallVec<A> {
#[inline]
fn index_mut(&mut self, index: $index_type) -> &mut $output_type {
&mut (&mut **self)[index]
}
}
fn index(&self, index: I) -> &I::Output {
&(**self)[index]
}
}

impl_index!(usize, A::Item);
impl_index!(ops::Range<usize>, [A::Item]);
impl_index!(ops::RangeFrom<usize>, [A::Item]);
impl_index!(ops::RangeTo<usize>, [A::Item]);
impl_index!(ops::RangeFull, [A::Item]);
impl<A: Array, I: SliceIndex<[A::Item]>> ops::IndexMut<I> for SmallVec<A> {
fn index_mut(&mut self, index: I) -> &mut I::Output {
&mut (&mut **self)[index]
}
}

impl<A: Array> ExtendFromSlice<A::Item> for SmallVec<A> where A::Item: Copy {
fn extend_from_slice(&mut self, other: &[A::Item]) {
Expand Down

0 comments on commit 4f127e5

Please sign in to comment.