Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use subslice patterns in slice methods #69706

Merged
merged 1 commit into from
Mar 7, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 8 additions & 22 deletions src/libcore/slice/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ impl<T> [T] {
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn first(&self) -> Option<&T> {
self.get(0)
if let [first, ..] = self { Some(first) } else { None }
}

/// Returns a mutable pointer to the first element of the slice, or `None` if it is empty.
Expand All @@ -121,7 +121,7 @@ impl<T> [T] {
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn first_mut(&mut self) -> Option<&mut T> {
self.get_mut(0)
if let [first, ..] = self { Some(first) } else { None }
}

/// Returns the first and all the rest of the elements of the slice, or `None` if it is empty.
Expand All @@ -139,7 +139,7 @@ impl<T> [T] {
#[stable(feature = "slice_splits", since = "1.5.0")]
#[inline]
pub fn split_first(&self) -> Option<(&T, &[T])> {
if self.is_empty() { None } else { Some((&self[0], &self[1..])) }
if let [first, tail @ ..] = self { Some((first, tail)) } else { None }
}

/// Returns the first and all the rest of the elements of the slice, or `None` if it is empty.
Expand All @@ -159,12 +159,7 @@ impl<T> [T] {
#[stable(feature = "slice_splits", since = "1.5.0")]
#[inline]
pub fn split_first_mut(&mut self) -> Option<(&mut T, &mut [T])> {
if self.is_empty() {
None
} else {
let split = self.split_at_mut(1);
Some((&mut split.0[0], split.1))
}
if let [first, tail @ ..] = self { Some((first, tail)) } else { None }
}

/// Returns the last and all the rest of the elements of the slice, or `None` if it is empty.
Expand All @@ -182,8 +177,7 @@ impl<T> [T] {
#[stable(feature = "slice_splits", since = "1.5.0")]
#[inline]
pub fn split_last(&self) -> Option<(&T, &[T])> {
let len = self.len();
if len == 0 { None } else { Some((&self[len - 1], &self[..(len - 1)])) }
if let [init @ .., last] = self { Some((last, init)) } else { None }
}

/// Returns the last and all the rest of the elements of the slice, or `None` if it is empty.
Expand All @@ -203,13 +197,7 @@ impl<T> [T] {
#[stable(feature = "slice_splits", since = "1.5.0")]
#[inline]
pub fn split_last_mut(&mut self) -> Option<(&mut T, &mut [T])> {
let len = self.len();
if len == 0 {
None
} else {
let split = self.split_at_mut(len - 1);
Some((&mut split.1[0], split.0))
}
if let [init @ .., last] = self { Some((last, init)) } else { None }
}

/// Returns the last element of the slice, or `None` if it is empty.
Expand All @@ -226,8 +214,7 @@ impl<T> [T] {
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn last(&self) -> Option<&T> {
let last_idx = self.len().checked_sub(1)?;
self.get(last_idx)
if let [.., last] = self { Some(last) } else { None }
}

/// Returns a mutable pointer to the last item in the slice.
Expand All @@ -245,8 +232,7 @@ impl<T> [T] {
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
pub fn last_mut(&mut self) -> Option<&mut T> {
let last_idx = self.len().checked_sub(1)?;
self.get_mut(last_idx)
if let [.., last] = self { Some(last) } else { None }
}

/// Returns a reference to an element or subslice depending on the type of
Expand Down