Skip to content

Commit 05f3a3d

Browse files
authored
Rollup merge of #148534 - WaffleLapkin:push_within_capacity_now_with_50_percent_more_mut, r=Amanieu
Merge `Vec::push{,_mut}_within_capacity` Implements rust-lang/libs-team#689. cc #135974, #100486 r? libs-api
2 parents 72cef11 + 32c93cc commit 05f3a3d

File tree

1 file changed

+16
-34
lines changed

1 file changed

+16
-34
lines changed

library/alloc/src/vec/mod.rs

Lines changed: 16 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2564,8 +2564,8 @@ impl<T, A: Allocator> Vec<T, A> {
25642564
let _ = self.push_mut(value);
25652565
}
25662566

2567-
/// Appends an element if there is sufficient spare capacity, otherwise an error is returned
2568-
/// with the element.
2567+
/// Appends an element and returns a reference to it if there is sufficient spare capacity,
2568+
/// otherwise an error is returned with the element.
25692569
///
25702570
/// Unlike [`push`] this method will not reallocate when there's insufficient capacity.
25712571
/// The caller should use [`reserve`] or [`try_reserve`] to ensure that there is enough capacity.
@@ -2601,8 +2601,20 @@ impl<T, A: Allocator> Vec<T, A> {
26012601
/// Takes *O*(1) time.
26022602
#[inline]
26032603
#[unstable(feature = "vec_push_within_capacity", issue = "100486")]
2604-
pub fn push_within_capacity(&mut self, value: T) -> Result<(), T> {
2605-
self.push_mut_within_capacity(value).map(|_| ())
2604+
// #[unstable(feature = "push_mut", issue = "135974")]
2605+
pub fn push_within_capacity(&mut self, value: T) -> Result<&mut T, T> {
2606+
if self.len == self.buf.capacity() {
2607+
return Err(value);
2608+
}
2609+
2610+
unsafe {
2611+
let end = self.as_mut_ptr().add(self.len);
2612+
ptr::write(end, value);
2613+
self.len += 1;
2614+
2615+
// SAFETY: We just wrote a value to the pointer that will live the lifetime of the reference.
2616+
Ok(&mut *end)
2617+
}
26062618
}
26072619

26082620
/// Appends an element to the back of a collection, returning a reference to it.
@@ -2654,36 +2666,6 @@ impl<T, A: Allocator> Vec<T, A> {
26542666
}
26552667
}
26562668

2657-
/// Appends an element and returns a reference to it if there is sufficient spare capacity,
2658-
/// otherwise an error is returned with the element.
2659-
///
2660-
/// Unlike [`push_mut`] this method will not reallocate when there's insufficient capacity.
2661-
/// The caller should use [`reserve`] or [`try_reserve`] to ensure that there is enough capacity.
2662-
///
2663-
/// [`push_mut`]: Vec::push_mut
2664-
/// [`reserve`]: Vec::reserve
2665-
/// [`try_reserve`]: Vec::try_reserve
2666-
///
2667-
/// # Time complexity
2668-
///
2669-
/// Takes *O*(1) time.
2670-
#[unstable(feature = "push_mut", issue = "135974")]
2671-
// #[unstable(feature = "vec_push_within_capacity", issue = "100486")]
2672-
#[inline]
2673-
#[must_use = "if you don't need a reference to the value, use `Vec::push_within_capacity` instead"]
2674-
pub fn push_mut_within_capacity(&mut self, value: T) -> Result<&mut T, T> {
2675-
if self.len == self.buf.capacity() {
2676-
return Err(value);
2677-
}
2678-
unsafe {
2679-
let end = self.as_mut_ptr().add(self.len);
2680-
ptr::write(end, value);
2681-
self.len += 1;
2682-
// SAFETY: We just wrote a value to the pointer that will live the lifetime of the reference.
2683-
Ok(&mut *end)
2684-
}
2685-
}
2686-
26872669
/// Removes the last element from a vector and returns it, or [`None`] if it
26882670
/// is empty.
26892671
///

0 commit comments

Comments
 (0)