Skip to content

Commit a6a98df

Browse files
committed
Move some slice methods for better doc order
1 parent 6004ebd commit a6a98df

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};
@@ -703,6 +703,20 @@ where
703703
}
704704

705705
impl<K, V, S> IndexMap<K, V, S> {
706+
/// Returns a slice of all the key-value pairs in the map.
707+
///
708+
/// Computes in **O(1)** time.
709+
pub fn as_slice(&self) -> &Slice<K, V> {
710+
Slice::from_slice(self.as_entries())
711+
}
712+
713+
/// Returns a mutable slice of all the key-value pairs in the map.
714+
///
715+
/// Computes in **O(1)** time.
716+
pub fn as_mut_slice(&mut self) -> &mut Slice<K, V> {
717+
Slice::from_mut_slice(self.as_entries_mut())
718+
}
719+
706720
/// Get a key-value pair by index
707721
///
708722
/// Valid indices are *0 <= index < self.len()*
@@ -721,6 +735,28 @@ impl<K, V, S> IndexMap<K, V, S> {
721735
self.as_entries_mut().get_mut(index).map(Bucket::muts)
722736
}
723737

738+
/// Returns a slice of key-value pairs in the given range of indices.
739+
///
740+
/// Valid indices are *0 <= index < self.len()*
741+
///
742+
/// Computes in **O(1)** time.
743+
pub fn get_range<R: RangeBounds<usize>>(&self, range: R) -> Option<&Slice<K, V>> {
744+
let entries = self.as_entries();
745+
let range = try_simplify_range(range, entries.len())?;
746+
entries.get(range).map(Slice::from_slice)
747+
}
748+
749+
/// Returns a mutable slice of key-value pairs in the given range of indices.
750+
///
751+
/// Valid indices are *0 <= index < self.len()*
752+
///
753+
/// Computes in **O(1)** time.
754+
pub fn get_range_mut<R: RangeBounds<usize>>(&mut self, range: R) -> Option<&mut Slice<K, V>> {
755+
let entries = self.as_entries_mut();
756+
let range = try_simplify_range(range, entries.len())?;
757+
entries.get_mut(range).map(Slice::from_mut_slice)
758+
}
759+
724760
/// Get the first key-value pair
725761
///
726762
/// Computes in **O(1)** time.
@@ -911,6 +947,13 @@ pub struct Iter<'a, K, V> {
911947
iter: SliceIter<'a, Bucket<K, V>>,
912948
}
913949

950+
impl<'a, K, V> Iter<'a, K, V> {
951+
/// Returns a slice of the remaining entries in the iterator.
952+
pub fn as_slice(&self) -> &'a Slice<K, V> {
953+
Slice::from_slice(self.iter.as_slice())
954+
}
955+
}
956+
914957
impl<'a, K, V> Iterator for Iter<'a, K, V> {
915958
type Item = (&'a K, &'a V);
916959

@@ -955,6 +998,15 @@ pub struct IterMut<'a, K, V> {
955998
iter: SliceIterMut<'a, Bucket<K, V>>,
956999
}
9571000

1001+
impl<'a, K, V> IterMut<'a, K, V> {
1002+
/// Returns a slice of the remaining entries in the iterator.
1003+
///
1004+
/// To avoid creating `&mut` references that alias, this is forced to consume the iterator.
1005+
pub fn into_slice(self) -> &'a mut Slice<K, V> {
1006+
Slice::from_mut_slice(self.iter.into_slice())
1007+
}
1008+
}
1009+
9581010
impl<'a, K, V> Iterator for IterMut<'a, K, V> {
9591011
type Item = (&'a K, &'a mut V);
9601012

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(has_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;
@@ -597,6 +598,13 @@ where
597598
}
598599

599600
impl<T, S> IndexSet<T, S> {
601+
/// Returns a slice of all the values in the set.
602+
///
603+
/// Computes in **O(1)** time.
604+
pub fn as_slice(&self) -> &Slice<T> {
605+
Slice::from_slice(self.as_entries())
606+
}
607+
600608
/// Get a value by index
601609
///
602610
/// Valid indices are *0 <= index < self.len()*
@@ -606,6 +614,17 @@ impl<T, S> IndexSet<T, S> {
606614
self.as_entries().get(index).map(Bucket::key_ref)
607615
}
608616

617+
/// Returns a slice of values in the given range of indices.
618+
///
619+
/// Valid indices are *0 <= index < self.len()*
620+
///
621+
/// Computes in **O(1)** time.
622+
pub fn get_range<R: RangeBounds<usize>>(&self, range: R) -> Option<&Slice<T>> {
623+
let entries = self.as_entries();
624+
let range = try_simplify_range(range, entries.len())?;
625+
entries.get(range).map(Slice::from_slice)
626+
}
627+
609628
/// Get the first value
610629
///
611630
/// Computes in **O(1)** time.
@@ -741,6 +760,13 @@ pub struct Iter<'a, T> {
741760
iter: SliceIter<'a, Bucket<T>>,
742761
}
743762

763+
impl<'a, T> Iter<'a, T> {
764+
/// Returns a slice of the remaining entries in the iterator.
765+
pub fn as_slice(&self) -> &'a Slice<T> {
766+
Slice::from_slice(self.iter.as_slice())
767+
}
768+
}
769+
744770
impl<'a, T> Iterator for Iter<'a, T> {
745771
type Item = &'a T;
746772

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)