@@ -2414,7 +2414,18 @@ impl<T, A: Allocator> Vec<T, A> {
24142414 #[ rustc_confusables( "push_back" , "put" , "append" ) ]
24152415 #[ track_caller]
24162416 pub fn push ( & mut self , value : T ) {
2417- let _ = self . push_mut ( value) ;
2417+ // Inform codegen that the length does not change across grow_one().
2418+ let len = self . len ;
2419+ // This will panic or abort if we would allocate > isize::MAX bytes
2420+ // or if the length increment would overflow for zero-sized types.
2421+ if len == self . buf . capacity ( ) {
2422+ self . buf . grow_one ( ) ;
2423+ }
2424+ unsafe {
2425+ let end = self . as_mut_ptr ( ) . add ( len) ;
2426+ ptr:: write ( end, value) ;
2427+ self . len = len + 1 ;
2428+ }
24182429 }
24192430
24202431 /// Appends an element if there is sufficient spare capacity, otherwise an error is returned
@@ -2455,7 +2466,16 @@ impl<T, A: Allocator> Vec<T, A> {
24552466 #[ inline]
24562467 #[ unstable( feature = "vec_push_within_capacity" , issue = "100486" ) ]
24572468 pub fn push_within_capacity ( & mut self , value : T ) -> Result < ( ) , T > {
2458- self . push_mut_within_capacity ( value) . map ( |_| ( ) )
2469+ if self . len == self . buf . capacity ( ) {
2470+ return Err ( value) ;
2471+ }
2472+ unsafe {
2473+ let end = self . as_mut_ptr ( ) . add ( self . len ) ;
2474+ ptr:: write ( end, value) ;
2475+ self . len += 1 ;
2476+ // SAFETY: We just wrote a value to the pointer that will live the lifetime of the reference.
2477+ Ok ( ( ) )
2478+ }
24592479 }
24602480
24612481 /// Appends an element to the back of a collection, returning a reference to it.
0 commit comments