-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Implement push_mut
#135975
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
Open
balt-dev
wants to merge
2
commits into
rust-lang:master
Choose a base branch
from
balt-dev:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+250
−31
Open
Implement push_mut
#135975
Changes from all commits
Commits
Show all changes
2 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 |
---|---|---|
|
@@ -182,11 +182,16 @@ impl<T, A: Allocator> VecDeque<T, A> { | |
unsafe { ptr::read(self.ptr().add(off)) } | ||
} | ||
|
||
/// Writes an element into the buffer, moving it. | ||
/// Writes an element into the buffer, moving it and returning a pointer to it. | ||
balt-dev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// # Safety | ||
/// | ||
/// May only be called if `off < self.capacity()`. | ||
#[inline] | ||
unsafe fn buffer_write(&mut self, off: usize, value: T) { | ||
unsafe fn buffer_write(&mut self, off: usize, value: T) -> &mut T { | ||
unsafe { | ||
ptr::write(self.ptr().add(off), value); | ||
let ptr = self.ptr().add(off); | ||
ptr::write(ptr, value); | ||
&mut *ptr | ||
} | ||
} | ||
|
||
|
@@ -1888,16 +1893,34 @@ impl<T, A: Allocator> VecDeque<T, A> { | |
#[stable(feature = "rust1", since = "1.0.0")] | ||
#[track_caller] | ||
pub fn push_front(&mut self, value: T) { | ||
let _ = self.push_front_mut(value); | ||
} | ||
|
||
/// Prepends an element to the deque, returning a reference to it. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ``` | ||
/// #![feature(push_mut)] | ||
/// use std::collections::VecDeque; | ||
/// | ||
/// let mut d = VecDeque::from([1, 2, 3]); | ||
/// let x = d.push_front_mut(8); | ||
/// *x -= 1; | ||
/// assert_eq!(d.front(), Some(&7)); | ||
/// ``` | ||
#[unstable(feature = "push_mut", issue = "135974")] | ||
#[track_caller] | ||
#[must_use = "if you don't need a reference to the value, use VecDeque::push_front instead"] | ||
pub fn push_front_mut(&mut self, value: T) -> &mut T { | ||
if self.is_full() { | ||
self.grow(); | ||
} | ||
|
||
self.head = self.wrap_sub(self.head, 1); | ||
self.len += 1; | ||
|
||
unsafe { | ||
self.buffer_write(self.head, value); | ||
} | ||
// SAFETY: We know that self.head is within range of the deque. | ||
unsafe { self.buffer_write(self.head, value) } | ||
} | ||
|
||
/// Appends an element to the back of the deque. | ||
|
@@ -1916,12 +1939,33 @@ impl<T, A: Allocator> VecDeque<T, A> { | |
#[rustc_confusables("push", "put", "append")] | ||
#[track_caller] | ||
pub fn push_back(&mut self, value: T) { | ||
let _ = self.push_back_mut(value); | ||
} | ||
|
||
/// Appends an element to the back of the deque, returning a reference to it. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ``` | ||
/// #![feature(push_mut)] | ||
/// use std::collections::VecDeque; | ||
/// | ||
/// let mut d = VecDeque::from([1, 2, 3]); | ||
/// let x = d.push_back_mut(9); | ||
/// *x += 1; | ||
/// assert_eq!(d.back(), Some(&10)); | ||
/// ``` | ||
#[unstable(feature = "push_mut", issue = "135974")] | ||
#[track_caller] | ||
#[must_use = "if you don't need a reference to the value, use VecDeque::push_back instead"] | ||
pub fn push_back_mut(&mut self, value: T) -> &mut T { | ||
if self.is_full() { | ||
self.grow(); | ||
} | ||
|
||
unsafe { self.buffer_write(self.to_physical_idx(self.len), value) } | ||
let len = self.len; | ||
self.len += 1; | ||
unsafe { self.buffer_write(self.to_physical_idx(len), value) } | ||
} | ||
|
||
#[inline] | ||
|
@@ -2007,7 +2051,7 @@ impl<T, A: Allocator> VecDeque<T, A> { | |
/// | ||
/// # Panics | ||
/// | ||
/// Panics if `index` is strictly greater than deque's length | ||
/// Panics if `index` is strictly greater than the deque's length. | ||
/// | ||
/// # Examples | ||
/// | ||
|
@@ -2029,7 +2073,38 @@ impl<T, A: Allocator> VecDeque<T, A> { | |
#[stable(feature = "deque_extras_15", since = "1.5.0")] | ||
#[track_caller] | ||
pub fn insert(&mut self, index: usize, value: T) { | ||
assert!(index <= self.len(), "index out of bounds"); | ||
let _ = self.insert_mut(index, value); | ||
} | ||
|
||
/// Inserts an element at `index` within the deque, shifting all elements | ||
/// with indices greater than or equal to `index` towards the back, | ||
/// and returning a reference to it. | ||
Comment on lines
+2079
to
+2081
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: looks like this needs a rewrap, library docs are (usually) 100 chars |
||
/// | ||
/// Element at index 0 is the front of the queue. | ||
/// | ||
/// # Panics | ||
/// | ||
/// Panics if `index` is strictly greater than the deque's length. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ``` | ||
/// #![feature(push_mut)] | ||
/// use std::collections::VecDeque; | ||
/// | ||
/// let mut vec_deque = VecDeque::from([1, 2, 3]); | ||
/// | ||
/// let x = vec_deque.insert_mut(1, 5); | ||
/// *x += 7; | ||
/// assert_eq!(vec_deque, &[1, 12, 2, 3]); | ||
/// ``` | ||
#[unstable(feature = "push_mut", issue = "135974")] | ||
#[track_caller] | ||
#[must_use = "if you don't need a reference to the value, use VecDeque::insert instead"] | ||
pub fn insert_mut(&mut self, index: usize, value: T) -> &mut T { | ||
if core::intrinsics::unlikely(index > self.len) { | ||
panic!("insertion index (is {index}) should be <= len (is {})", self.len()) | ||
}; | ||
if self.is_full() { | ||
self.grow(); | ||
} | ||
|
@@ -2042,16 +2117,16 @@ impl<T, A: Allocator> VecDeque<T, A> { | |
unsafe { | ||
// see `remove()` for explanation why this wrap_copy() call is safe. | ||
self.wrap_copy(self.to_physical_idx(index), self.to_physical_idx(index + 1), k); | ||
self.buffer_write(self.to_physical_idx(index), value); | ||
self.len += 1; | ||
self.buffer_write(self.to_physical_idx(index), value) | ||
} | ||
} else { | ||
let old_head = self.head; | ||
self.head = self.wrap_sub(self.head, 1); | ||
unsafe { | ||
self.wrap_copy(old_head, self.head, index); | ||
self.buffer_write(self.to_physical_idx(index), value); | ||
self.len += 1; | ||
self.buffer_write(self.to_physical_idx(index), value) | ||
} | ||
} | ||
} | ||
|
Oops, something went wrong.
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.