@@ -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