Skip to content

Commit e6b677b

Browse files
authored
Merge pull request #430 from cuviper/split_at_checked
Add `Slice::split_at_checked` and `split_at_mut_checked`
2 parents 8b8d350 + 61c9d53 commit e6b677b

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

src/map/slice.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ impl<K, V> Slice<K, V> {
124124
/// Divides one slice into two at an index.
125125
///
126126
/// ***Panics*** if `index > len`.
127+
/// For a non-panicking alternative see [`split_at_checked`][Self::split_at_checked].
127128
#[track_caller]
128129
pub fn split_at(&self, index: usize) -> (&Self, &Self) {
129130
let (first, second) = self.entries.split_at(index);
@@ -133,12 +134,29 @@ impl<K, V> Slice<K, V> {
133134
/// Divides one mutable slice into two at an index.
134135
///
135136
/// ***Panics*** if `index > len`.
137+
/// For a non-panicking alternative see [`split_at_mut_checked`][Self::split_at_mut_checked].
136138
#[track_caller]
137139
pub fn split_at_mut(&mut self, index: usize) -> (&mut Self, &mut Self) {
138140
let (first, second) = self.entries.split_at_mut(index);
139141
(Self::from_mut_slice(first), Self::from_mut_slice(second))
140142
}
141143

144+
/// Divides one slice into two at an index.
145+
///
146+
/// Returns `None` if `index > len`.
147+
pub fn split_at_checked(&self, index: usize) -> Option<(&Self, &Self)> {
148+
let (first, second) = self.entries.split_at_checked(index)?;
149+
Some((Self::from_slice(first), Self::from_slice(second)))
150+
}
151+
152+
/// Divides one mutable slice into two at an index.
153+
///
154+
/// Returns `None` if `index > len`.
155+
pub fn split_at_mut_checked(&mut self, index: usize) -> Option<(&mut Self, &mut Self)> {
156+
let (first, second) = self.entries.split_at_mut_checked(index)?;
157+
Some((Self::from_mut_slice(first), Self::from_mut_slice(second)))
158+
}
159+
142160
/// Returns the first key-value pair and the rest of the slice,
143161
/// or `None` if it is empty.
144162
pub fn split_first(&self) -> Option<((&K, &V), &Self)> {

src/set/slice.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,21 @@ impl<T> Slice<T> {
8585
/// Divides one slice into two at an index.
8686
///
8787
/// ***Panics*** if `index > len`.
88+
/// For a non-panicking alternative see [`split_at_checked`][Self::split_at_checked].
8889
#[track_caller]
8990
pub fn split_at(&self, index: usize) -> (&Self, &Self) {
9091
let (first, second) = self.entries.split_at(index);
9192
(Self::from_slice(first), Self::from_slice(second))
9293
}
9394

95+
/// Divides one slice into two at an index.
96+
///
97+
/// Returns `None` if `index > len`.
98+
pub fn split_at_checked(&self, index: usize) -> Option<(&Self, &Self)> {
99+
let (first, second) = self.entries.split_at_checked(index)?;
100+
Some((Self::from_slice(first), Self::from_slice(second)))
101+
}
102+
94103
/// Returns the first value and the rest of the slice,
95104
/// or `None` if it is empty.
96105
pub fn split_first(&self) -> Option<(&T, &Self)> {

0 commit comments

Comments
 (0)