Skip to content

Commit e9024a3

Browse files
committed
Move some slice methods for better doc order
1 parent 949c9a9 commit e9024a3

File tree

4 files changed

+82
-73
lines changed

4 files changed

+82
-73
lines changed

src/map.rs

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use std::collections::hash_map::RandomState;
2323

2424
use self::core::IndexMapCore;
2525
use crate::equivalent::Equivalent;
26-
use crate::util::third;
26+
use crate::util::{third, try_simplify_range};
2727
use crate::{Bucket, Entries, HashValue};
2828

2929
pub use self::core::{Entry, OccupiedEntry, VacantEntry};
@@ -760,6 +760,20 @@ where
760760
}
761761

762762
impl<K, V, S> IndexMap<K, V, S> {
763+
/// Returns a slice of all the key-value pairs in the map.
764+
///
765+
/// Computes in **O(1)** time.
766+
pub fn as_slice(&self) -> &Slice<K, V> {
767+
Slice::from_slice(self.as_entries())
768+
}
769+
770+
/// Returns a mutable slice of all the key-value pairs in the map.
771+
///
772+
/// Computes in **O(1)** time.
773+
pub fn as_mut_slice(&mut self) -> &mut Slice<K, V> {
774+
Slice::from_mut_slice(self.as_entries_mut())
775+
}
776+
763777
/// Get a key-value pair by index
764778
///
765779
/// Valid indices are *0 <= index < self.len()*
@@ -778,6 +792,28 @@ impl<K, V, S> IndexMap<K, V, S> {
778792
self.as_entries_mut().get_mut(index).map(Bucket::ref_mut)
779793
}
780794

795+
/// Returns a slice of key-value pairs in the given range of indices.
796+
///
797+
/// Valid indices are *0 <= index < self.len()*
798+
///
799+
/// Computes in **O(1)** time.
800+
pub fn get_range<R: RangeBounds<usize>>(&self, range: R) -> Option<&Slice<K, V>> {
801+
let entries = self.as_entries();
802+
let range = try_simplify_range(range, entries.len())?;
803+
entries.get(range).map(Slice::from_slice)
804+
}
805+
806+
/// Returns a mutable slice of key-value pairs in the given range of indices.
807+
///
808+
/// Valid indices are *0 <= index < self.len()*
809+
///
810+
/// Computes in **O(1)** time.
811+
pub fn get_range_mut<R: RangeBounds<usize>>(&mut self, range: R) -> Option<&mut Slice<K, V>> {
812+
let entries = self.as_entries_mut();
813+
let range = try_simplify_range(range, entries.len())?;
814+
entries.get_mut(range).map(Slice::from_mut_slice)
815+
}
816+
781817
/// Get the first key-value pair
782818
///
783819
/// Computes in **O(1)** time.
@@ -1047,6 +1083,13 @@ pub struct Iter<'a, K, V> {
10471083
iter: SliceIter<'a, Bucket<K, V>>,
10481084
}
10491085

1086+
impl<'a, K, V> Iter<'a, K, V> {
1087+
/// Returns a slice of the remaining entries in the iterator.
1088+
pub fn as_slice(&self) -> &'a Slice<K, V> {
1089+
Slice::from_slice(self.iter.as_slice())
1090+
}
1091+
}
1092+
10501093
impl<'a, K, V> Iterator for Iter<'a, K, V> {
10511094
type Item = (&'a K, &'a V);
10521095

@@ -1091,6 +1134,15 @@ pub struct IterMut<'a, K, V> {
10911134
iter: SliceIterMut<'a, Bucket<K, V>>,
10921135
}
10931136

1137+
impl<'a, K, V> IterMut<'a, K, V> {
1138+
/// Returns a slice of the remaining entries in the iterator.
1139+
///
1140+
/// To avoid creating `&mut` references that alias, this is forced to consume the iterator.
1141+
pub fn into_slice(self) -> &'a mut Slice<K, V> {
1142+
Slice::from_mut_slice(self.iter.into_slice())
1143+
}
1144+
}
1145+
10941146
impl<'a, K, V> Iterator for IterMut<'a, K, V> {
10951147
type Item = (&'a K, &'a mut V);
10961148

src/map/slice.rs

Lines changed: 2 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -20,65 +20,19 @@ pub struct Slice<K, V> {
2020

2121
#[allow(unsafe_code)]
2222
impl<K, V> Slice<K, V> {
23-
fn from_slice(entries: &[Bucket<K, V>]) -> &Self {
23+
pub(super) fn from_slice(entries: &[Bucket<K, V>]) -> &Self {
2424
// SAFETY: `Slice<K, V>` is a transparent wrapper around `[Bucket<K, V>]`,
2525
// and the lifetimes are bound together by this function's signature.
2626
unsafe { &*(entries as *const [Bucket<K, V>] as *const Self) }
2727
}
2828

29-
fn from_mut_slice(entries: &mut [Bucket<K, V>]) -> &mut Self {
29+
pub(super) fn from_mut_slice(entries: &mut [Bucket<K, V>]) -> &mut Self {
3030
// SAFETY: `Slice<K, V>` is a transparent wrapper around `[Bucket<K, V>]`,
3131
// and the lifetimes are bound together by this function's signature.
3232
unsafe { &mut *(entries as *mut [Bucket<K, V>] as *mut Self) }
3333
}
3434
}
3535

36-
impl<K, V, S> IndexMap<K, V, S> {
37-
/// Returns a slice of all the entries in the map.
38-
pub fn as_slice(&self) -> &Slice<K, V> {
39-
Slice::from_slice(self.as_entries())
40-
}
41-
42-
/// Returns a mutable slice of all the entries in the map.
43-
pub fn as_mut_slice(&mut self) -> &mut Slice<K, V> {
44-
Slice::from_mut_slice(self.as_entries_mut())
45-
}
46-
47-
/// Returns a slice of entries in the given range of indices.
48-
///
49-
/// Valid indices are *0 <= index < self.len()*
50-
pub fn get_range<R: RangeBounds<usize>>(&self, range: R) -> Option<&Slice<K, V>> {
51-
let entries = self.as_entries();
52-
let range = try_simplify_range(range, entries.len())?;
53-
entries.get(range).map(Slice::from_slice)
54-
}
55-
56-
/// Returns a mutable slice of entries in the given range of indices.
57-
///
58-
/// Valid indices are *0 <= index < self.len()*
59-
pub fn get_range_mut<R: RangeBounds<usize>>(&mut self, range: R) -> Option<&mut Slice<K, V>> {
60-
let entries = self.as_entries_mut();
61-
let range = try_simplify_range(range, entries.len())?;
62-
entries.get_mut(range).map(Slice::from_mut_slice)
63-
}
64-
}
65-
66-
impl<'a, K, V> Iter<'a, K, V> {
67-
/// Returns a slice of the remaining entries in the iterator.
68-
pub fn as_slice(&self) -> &'a Slice<K, V> {
69-
Slice::from_slice(self.iter.as_slice())
70-
}
71-
}
72-
73-
impl<'a, K, V> IterMut<'a, K, V> {
74-
/// Returns a slice of the remaining entries in the iterator.
75-
///
76-
/// To avoid creating `&mut` references that alias, this is forced to consume the iterator.
77-
pub fn into_slice(self) -> &'a mut Slice<K, V> {
78-
Slice::from_mut_slice(self.iter.into_slice())
79-
}
80-
}
81-
8236
impl<K, V> Slice<K, V> {
8337
/// Return the number of key-value pairs in the map slice.
8438
#[inline]

src/set.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ pub use crate::rayon::set as rayon;
1010
#[cfg(feature = "std")]
1111
use std::collections::hash_map::RandomState;
1212

13+
use crate::util::try_simplify_range;
1314
use crate::vec::{self, Vec};
1415
use core::cmp::Ordering;
1516
use core::fmt;
@@ -654,6 +655,13 @@ where
654655
}
655656

656657
impl<T, S> IndexSet<T, S> {
658+
/// Returns a slice of all the values in the set.
659+
///
660+
/// Computes in **O(1)** time.
661+
pub fn as_slice(&self) -> &Slice<T> {
662+
Slice::from_slice(self.as_entries())
663+
}
664+
657665
/// Get a value by index
658666
///
659667
/// Valid indices are *0 <= index < self.len()*
@@ -663,6 +671,17 @@ impl<T, S> IndexSet<T, S> {
663671
self.as_entries().get(index).map(Bucket::key_ref)
664672
}
665673

674+
/// Returns a slice of values in the given range of indices.
675+
///
676+
/// Valid indices are *0 <= index < self.len()*
677+
///
678+
/// Computes in **O(1)** time.
679+
pub fn get_range<R: RangeBounds<usize>>(&self, range: R) -> Option<&Slice<T>> {
680+
let entries = self.as_entries();
681+
let range = try_simplify_range(range, entries.len())?;
682+
entries.get(range).map(Slice::from_slice)
683+
}
684+
666685
/// Get the first value
667686
///
668687
/// Computes in **O(1)** time.
@@ -798,6 +817,13 @@ pub struct Iter<'a, T> {
798817
iter: SliceIter<'a, Bucket<T>>,
799818
}
800819

820+
impl<'a, T> Iter<'a, T> {
821+
/// Returns a slice of the remaining entries in the iterator.
822+
pub fn as_slice(&self) -> &'a Slice<T> {
823+
Slice::from_slice(self.iter.as_slice())
824+
}
825+
}
826+
801827
impl<'a, T> Iterator for Iter<'a, T> {
802828
type Item = &'a T;
803829

src/set/slice.rs

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,36 +20,13 @@ pub struct Slice<T> {
2020

2121
#[allow(unsafe_code)]
2222
impl<T> Slice<T> {
23-
fn from_slice(entries: &[Bucket<T>]) -> &Self {
23+
pub(super) fn from_slice(entries: &[Bucket<T>]) -> &Self {
2424
// SAFETY: `Slice<T>` is a transparent wrapper around `[Bucket<T>]`,
2525
// and the lifetimes are bound together by this function's signature.
2626
unsafe { &*(entries as *const [Bucket<T>] as *const Self) }
2727
}
2828
}
2929

30-
impl<T, S> IndexSet<T, S> {
31-
/// Returns a slice of all the values in the set.
32-
pub fn as_slice(&self) -> &Slice<T> {
33-
Slice::from_slice(self.as_entries())
34-
}
35-
36-
/// Returns a slice of values in the given range of indices.
37-
///
38-
/// Valid indices are *0 <= index < self.len()*
39-
pub fn get_range<R: RangeBounds<usize>>(&self, range: R) -> Option<&Slice<T>> {
40-
let entries = self.as_entries();
41-
let range = try_simplify_range(range, entries.len())?;
42-
entries.get(range).map(Slice::from_slice)
43-
}
44-
}
45-
46-
impl<'a, T> Iter<'a, T> {
47-
/// Returns a slice of the remaining entries in the iterator.
48-
pub fn as_slice(&self) -> &'a Slice<T> {
49-
Slice::from_slice(self.iter.as_slice())
50-
}
51-
}
52-
5330
impl<T> Slice<T> {
5431
/// Return the number of elements in the set slice.
5532
pub fn len(&self) -> usize {

0 commit comments

Comments
 (0)