Skip to content

Commit 547d489

Browse files
committed
impl Index<(Bound<usize>, Bound<usize>)> like Rust 1.53
1 parent 19d7983 commit 547d489

File tree

2 files changed

+64
-2
lines changed

2 files changed

+64
-2
lines changed

src/map/slice.rs

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
use super::{Bucket, Entries, IndexMap, Iter, IterMut, Keys, Values, ValuesMut};
2+
use crate::util::simplify_range;
23

34
use core::cmp::Ordering;
45
use core::fmt;
56
use core::hash::{Hash, Hasher};
6-
use core::ops::{self, Index, IndexMut};
7+
use core::ops::{self, Bound, Index, IndexMut};
78

89
/// A dynamically-sized slice of key-value pairs in an `IndexMap`.
910
///
@@ -317,3 +318,41 @@ impl_index!(
317318
ops::RangeTo<usize>,
318319
ops::RangeToInclusive<usize>
319320
);
321+
322+
// NB: with MSRV 1.53, we can forward `Bound` pairs to direct slice indexing like other ranges
323+
324+
impl<K, V, S> Index<(Bound<usize>, Bound<usize>)> for IndexMap<K, V, S> {
325+
type Output = Slice<K, V>;
326+
327+
fn index(&self, range: (Bound<usize>, Bound<usize>)) -> &Self::Output {
328+
let entries = self.as_entries();
329+
let range = simplify_range(range, entries.len());
330+
Slice::from_slice(&entries[range])
331+
}
332+
}
333+
334+
impl<K, V, S> IndexMut<(Bound<usize>, Bound<usize>)> for IndexMap<K, V, S> {
335+
fn index_mut(&mut self, range: (Bound<usize>, Bound<usize>)) -> &mut Self::Output {
336+
let entries = self.as_entries_mut();
337+
let range = simplify_range(range, entries.len());
338+
Slice::from_mut_slice(&mut entries[range])
339+
}
340+
}
341+
342+
impl<K, V> Index<(Bound<usize>, Bound<usize>)> for Slice<K, V> {
343+
type Output = Slice<K, V>;
344+
345+
fn index(&self, range: (Bound<usize>, Bound<usize>)) -> &Self {
346+
let entries = &self.entries;
347+
let range = simplify_range(range, entries.len());
348+
Slice::from_slice(&entries[range])
349+
}
350+
}
351+
352+
impl<K, V> IndexMut<(Bound<usize>, Bound<usize>)> for Slice<K, V> {
353+
fn index_mut(&mut self, range: (Bound<usize>, Bound<usize>)) -> &mut Self {
354+
let entries = &mut self.entries;
355+
let range = simplify_range(range, entries.len());
356+
Slice::from_mut_slice(&mut entries[range])
357+
}
358+
}

src/set/slice.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
use super::{Bucket, Entries, IndexSet, Iter};
2+
use crate::util::simplify_range;
23

34
use core::cmp::Ordering;
45
use core::fmt;
56
use core::hash::{Hash, Hasher};
6-
use core::ops::{self, Index};
7+
use core::ops::{self, Bound, Index};
78

89
/// A dynamically-sized slice of values in an `IndexSet`.
910
///
@@ -191,3 +192,25 @@ impl_index!(
191192
ops::RangeTo<usize>,
192193
ops::RangeToInclusive<usize>
193194
);
195+
196+
// NB: with MSRV 1.53, we can forward `Bound` pairs to direct slice indexing like other ranges
197+
198+
impl<T, S> Index<(Bound<usize>, Bound<usize>)> for IndexSet<T, S> {
199+
type Output = Slice<T>;
200+
201+
fn index(&self, range: (Bound<usize>, Bound<usize>)) -> &Self::Output {
202+
let entries = self.as_entries();
203+
let range = simplify_range(range, entries.len());
204+
Slice::from_slice(&entries[range])
205+
}
206+
}
207+
208+
impl<T> Index<(Bound<usize>, Bound<usize>)> for Slice<T> {
209+
type Output = Self;
210+
211+
fn index(&self, range: (Bound<usize>, Bound<usize>)) -> &Self::Output {
212+
let entries = &self.entries;
213+
let range = simplify_range(range, entries.len());
214+
Slice::from_slice(&entries[range])
215+
}
216+
}

0 commit comments

Comments
 (0)