-
Notifications
You must be signed in to change notification settings - Fork 13.4k
add Vec::peek_mut #142046
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
Merged
Merged
add Vec::peek_mut #142046
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
use core::ops::{Deref, DerefMut}; | ||
|
||
use super::Vec; | ||
use crate::fmt; | ||
|
||
/// Structure wrapping a mutable reference to the last item in a | ||
/// `Vec`. | ||
/// | ||
/// This `struct` is created by the [`peek_mut`] method on [`Vec`]. See | ||
/// its documentation for more. | ||
/// | ||
/// [`peek_mut`]: Vec::peek_mut | ||
#[unstable(feature = "vec_peek_mut", issue = "122742")] | ||
pub struct PeekMut<'a, T> { | ||
vec: &'a mut Vec<T>, | ||
} | ||
|
||
#[unstable(feature = "vec_peek_mut", issue = "122742")] | ||
impl<T: fmt::Debug> fmt::Debug for PeekMut<'_, T> { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
f.debug_tuple("PeekMut").field(self.deref()).finish() | ||
} | ||
} | ||
|
||
impl<'a, T> PeekMut<'a, T> { | ||
pub(crate) fn new(vec: &'a mut Vec<T>) -> Option<Self> { | ||
if vec.is_empty() { None } else { Some(Self { vec }) } | ||
} | ||
|
||
/// Removes the peeked value from the vector and returns it. | ||
#[unstable(feature = "vec_peek_mut", issue = "122742")] | ||
pub fn pop(self) -> T { | ||
// SAFETY: PeekMut is only constructed if the vec is non-empty | ||
unsafe { self.vec.pop().unwrap_unchecked() } | ||
} | ||
} | ||
|
||
#[unstable(feature = "vec_peek_mut", issue = "122742")] | ||
impl<'a, T> Deref for PeekMut<'a, T> { | ||
type Target = T; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
// SAFETY: PeekMut is only constructed if the vec is non-empty | ||
unsafe { self.vec.get_unchecked(self.vec.len() - 1) } | ||
} | ||
} | ||
|
||
#[unstable(feature = "vec_peek_mut", issue = "122742")] | ||
impl<'a, T> DerefMut for PeekMut<'a, T> { | ||
fn deref_mut(&mut self) -> &mut Self::Target { | ||
let idx = self.vec.len() - 1; | ||
// SAFETY: PeekMut is only constructed if the vec is non-empty | ||
unsafe { self.vec.get_unchecked_mut(idx) } | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.