Skip to content

Commit c88bb7d

Browse files
authored
Merge pull request #225 from cuviper/slice-patterns
Use first-class patterns for split_first/last
2 parents 5ba716b + ebe2f29 commit c88bb7d

File tree

4 files changed

+12
-11
lines changed

4 files changed

+12
-11
lines changed

src/map/core.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,8 @@ impl<K, V> IndexMapCore<K, V> {
204204

205205
/// Shrink the capacity of the map with a lower bound
206206
pub(crate) fn shrink_to(&mut self, min_capacity: usize) {
207-
self.indices.shrink_to(min_capacity, get_hash(&self.entries));
207+
self.indices
208+
.shrink_to(min_capacity, get_hash(&self.entries));
208209
self.entries.shrink_to(min_capacity);
209210
}
210211

src/map/slice.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ impl<K, V> Slice<K, V> {
130130
/// Returns the first key-value pair and the rest of the slice,
131131
/// or `None` if it is empty.
132132
pub fn split_first(&self) -> Option<((&K, &V), &Self)> {
133-
if let Some((first, rest)) = self.entries.split_first() {
133+
if let [first, rest @ ..] = &self.entries {
134134
Some((first.refs(), Self::from_slice(rest)))
135135
} else {
136136
None
@@ -140,7 +140,7 @@ impl<K, V> Slice<K, V> {
140140
/// Returns the first key-value pair and the rest of the slice,
141141
/// with mutable access to the value, or `None` if it is empty.
142142
pub fn split_first_mut(&mut self) -> Option<((&K, &mut V), &mut Self)> {
143-
if let Some((first, rest)) = self.entries.split_first_mut() {
143+
if let [first, rest @ ..] = &mut self.entries {
144144
Some((first.ref_mut(), Self::from_mut_slice(rest)))
145145
} else {
146146
None
@@ -150,7 +150,7 @@ impl<K, V> Slice<K, V> {
150150
/// Returns the last key-value pair and the rest of the slice,
151151
/// or `None` if it is empty.
152152
pub fn split_last(&self) -> Option<((&K, &V), &Self)> {
153-
if let Some((last, rest)) = self.entries.split_last() {
153+
if let [rest @ .., last] = &self.entries {
154154
Some((last.refs(), Self::from_slice(rest)))
155155
} else {
156156
None
@@ -160,7 +160,7 @@ impl<K, V> Slice<K, V> {
160160
/// Returns the last key-value pair and the rest of the slice,
161161
/// with mutable access to the value, or `None` if it is empty.
162162
pub fn split_last_mut(&mut self) -> Option<((&K, &mut V), &mut Self)> {
163-
if let Some((last, rest)) = self.entries.split_last_mut() {
163+
if let [rest @ .., last] = &mut self.entries {
164164
Some((last.ref_mut(), Self::from_mut_slice(rest)))
165165
} else {
166166
None

src/serde_seq.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,16 @@ use core::fmt::{self, Formatter};
2727
use core::hash::{BuildHasher, Hash};
2828
use core::marker::PhantomData;
2929

30-
use crate::IndexMap;
3130
use crate::map::Slice as MapSlice;
3231
use crate::set::Slice as SetSlice;
32+
use crate::IndexMap;
3333

3434
/// Serializes a `map::Slice` as an ordered sequence.
3535
///
3636
/// This behaves like [`crate::serde_seq`] for `IndexMap`, serializing a sequence
37-
/// of `(key, value)` pairs, rather than as a map that might not preserver order.
37+
/// of `(key, value)` pairs, rather than as a map that might not preserve order.
3838
///
39-
/// Requires crate feature `"serde"` or `"serde-1"`
39+
/// Requires crate feature `"serde"`
4040
impl<K, V> Serialize for MapSlice<K, V>
4141
where
4242
K: Serialize,
@@ -52,7 +52,7 @@ where
5252

5353
/// Serializes a `set::Slice` as an ordered sequence.
5454
///
55-
/// Requires crate feature `"serde"` or `"serde-1"`
55+
/// Requires crate feature `"serde"`
5656
impl<T> Serialize for SetSlice<T>
5757
where
5858
T: Serialize,

src/set/slice.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ impl<T> Slice<T> {
8888
/// Returns the first value and the rest of the slice,
8989
/// or `None` if it is empty.
9090
pub fn split_first(&self) -> Option<(&T, &Self)> {
91-
if let Some((first, rest)) = self.entries.split_first() {
91+
if let [first, rest @ ..] = &self.entries {
9292
Some((&first.key, Self::from_slice(rest)))
9393
} else {
9494
None
@@ -98,7 +98,7 @@ impl<T> Slice<T> {
9898
/// Returns the last value and the rest of the slice,
9999
/// or `None` if it is empty.
100100
pub fn split_last(&self) -> Option<(&T, &Self)> {
101-
if let Some((last, rest)) = self.entries.split_last() {
101+
if let [rest @ .., last] = &self.entries {
102102
Some((&last.key, Self::from_slice(rest)))
103103
} else {
104104
None

0 commit comments

Comments
 (0)