@@ -1023,11 +1023,9 @@ impl<T, A: Allocator> Vec<T, A> {
1023
1023
#[ cfg( not( no_global_oom_handling) ) ]
1024
1024
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1025
1025
pub fn shrink_to_fit ( & mut self ) {
1026
- // The capacity is never less than the length, and there's nothing to do when
1027
- // they are equal, so we can avoid the panic case in `RawVec::shrink_to_fit`
1028
- // by only calling it with a greater capacity.
1029
- if self . capacity ( ) > self . len {
1030
- self . buf . shrink_to_fit ( self . len ) ;
1026
+ match self . try_shrink_to_fit ( ) {
1027
+ Ok ( r) => r,
1028
+ Err ( e) => e. handle ( ) ,
1031
1029
}
1032
1030
}
1033
1031
@@ -1110,13 +1108,10 @@ impl<T, A: Allocator> Vec<T, A> {
1110
1108
/// ```
1111
1109
#[ cfg( not( no_global_oom_handling) ) ]
1112
1110
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1113
- pub fn into_boxed_slice ( mut self ) -> Box < [ T ] , A > {
1114
- unsafe {
1115
- self . shrink_to_fit ( ) ;
1116
- let me = ManuallyDrop :: new ( self ) ;
1117
- let buf = ptr:: read ( & me. buf ) ;
1118
- let len = me. len ( ) ;
1119
- buf. into_box ( len) . assume_init ( )
1111
+ pub fn into_boxed_slice ( self ) -> Box < [ T ] , A > {
1112
+ match self . try_into_boxed_slice ( ) {
1113
+ Ok ( r) => r,
1114
+ Err ( e) => e. handle ( ) ,
1120
1115
}
1121
1116
}
1122
1117
@@ -1844,15 +1839,9 @@ impl<T, A: Allocator> Vec<T, A> {
1844
1839
#[ inline]
1845
1840
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1846
1841
pub fn push ( & mut self , value : T ) {
1847
- // This will panic or abort if we would allocate > isize::MAX bytes
1848
- // or if the length increment would overflow for zero-sized types.
1849
- if self . len == self . buf . capacity ( ) {
1850
- self . reserve ( 1 ) ;
1851
- }
1852
- unsafe {
1853
- let end = self . as_mut_ptr ( ) . add ( self . len ) ;
1854
- ptr:: write ( end, value) ;
1855
- self . len += 1 ;
1842
+ match self . try_push ( value) {
1843
+ Ok ( r) => r,
1844
+ Err ( e) => e. handle ( ) ,
1856
1845
}
1857
1846
}
1858
1847
@@ -1931,11 +1920,12 @@ impl<T, A: Allocator> Vec<T, A> {
1931
1920
#[ cfg( not( no_global_oom_handling) ) ]
1932
1921
#[ inline]
1933
1922
unsafe fn append_elements ( & mut self , other : * const [ T ] ) {
1934
- let count = unsafe { ( * other) . len ( ) } ;
1935
- self . reserve ( count) ;
1936
- let len = self . len ( ) ;
1937
- unsafe { ptr:: copy_nonoverlapping ( other as * const T , self . as_mut_ptr ( ) . add ( len) , count) } ;
1938
- self . len += count;
1923
+ unsafe {
1924
+ match self . try_append_elements ( other) {
1925
+ Ok ( r) => r,
1926
+ Err ( e) => e. handle ( ) ,
1927
+ }
1928
+ }
1939
1929
}
1940
1930
1941
1931
/// Tries to append elements to `Self` from other buffer.
@@ -2515,31 +2505,10 @@ impl<T, F: FnMut() -> T> ExtendWith<T> for ExtendFunc<F> {
2515
2505
impl < T , A : Allocator > Vec < T , A > {
2516
2506
#[ cfg( not( no_global_oom_handling) ) ]
2517
2507
/// Extend the vector by `n` values, using the given generator.
2518
- fn extend_with < E : ExtendWith < T > > ( & mut self , n : usize , mut value : E ) {
2519
- self . reserve ( n) ;
2520
-
2521
- unsafe {
2522
- let mut ptr = self . as_mut_ptr ( ) . add ( self . len ( ) ) ;
2523
- // Use SetLenOnDrop to work around bug where compiler
2524
- // might not realize the store through `ptr` through self.set_len()
2525
- // don't alias.
2526
- let mut local_len = SetLenOnDrop :: new ( & mut self . len ) ;
2527
-
2528
- // Write all elements except the last one
2529
- for _ in 1 ..n {
2530
- ptr:: write ( ptr, value. next ( ) ) ;
2531
- ptr = ptr. offset ( 1 ) ;
2532
- // Increment the length in every step in case next() panics
2533
- local_len. increment_len ( 1 ) ;
2534
- }
2535
-
2536
- if n > 0 {
2537
- // We can write the last element directly without cloning needlessly
2538
- ptr:: write ( ptr, value. last ( ) ) ;
2539
- local_len. increment_len ( 1 ) ;
2540
- }
2541
-
2542
- // len set by scope guard
2508
+ fn extend_with < E : ExtendWith < T > > ( & mut self , n : usize , value : E ) {
2509
+ match self . try_extend_with ( n, value) {
2510
+ Ok ( r) => r,
2511
+ Err ( e) => e. handle ( ) ,
2543
2512
}
2544
2513
}
2545
2514
@@ -2886,27 +2855,10 @@ impl<T, A: Allocator> Vec<T, A> {
2886
2855
// leaf method to which various SpecFrom/SpecExtend implementations delegate when
2887
2856
// they have no further optimizations to apply
2888
2857
#[ cfg( not( no_global_oom_handling) ) ]
2889
- fn extend_desugared < I : Iterator < Item = T > > ( & mut self , mut iterator : I ) {
2890
- // This is the case for a general iterator.
2891
- //
2892
- // This function should be the moral equivalent of:
2893
- //
2894
- // for item in iterator {
2895
- // self.push(item);
2896
- // }
2897
- while let Some ( element) = iterator. next ( ) {
2898
- let len = self . len ( ) ;
2899
- if len == self . capacity ( ) {
2900
- let ( lower, _) = iterator. size_hint ( ) ;
2901
- self . reserve ( lower. saturating_add ( 1 ) ) ;
2902
- }
2903
- unsafe {
2904
- ptr:: write ( self . as_mut_ptr ( ) . add ( len) , element) ;
2905
- // Since next() executes user code which can panic we have to bump the length
2906
- // after each step.
2907
- // NB can't overflow since we would have had to alloc the address space
2908
- self . set_len ( len + 1 ) ;
2909
- }
2858
+ fn extend_desugared < I : Iterator < Item = T > > ( & mut self , iterator : I ) {
2859
+ match self . try_extend_desugared ( iterator) {
2860
+ Ok ( r) => r,
2861
+ Err ( e) => e. handle ( ) ,
2910
2862
}
2911
2863
}
2912
2864
0 commit comments