@@ -2414,7 +2414,18 @@ impl<T, A: Allocator> Vec<T, A> {
2414
2414
#[ rustc_confusables( "push_back" , "put" , "append" ) ]
2415
2415
#[ track_caller]
2416
2416
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
+ }
2418
2429
}
2419
2430
2420
2431
/// 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> {
2455
2466
#[ inline]
2456
2467
#[ unstable( feature = "vec_push_within_capacity" , issue = "100486" ) ]
2457
2468
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
+ }
2459
2479
}
2460
2480
2461
2481
/// Appends an element to the back of a collection, returning a reference to it.
0 commit comments