@@ -1023,11 +1023,9 @@ impl<T, A: Allocator> Vec<T, A> {
10231023 #[ cfg( not( no_global_oom_handling) ) ]
10241024 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
10251025 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 ( ) ,
10311029 }
10321030 }
10331031
@@ -1110,13 +1108,10 @@ impl<T, A: Allocator> Vec<T, A> {
11101108 /// ```
11111109 #[ cfg( not( no_global_oom_handling) ) ]
11121110 #[ 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 ( ) ,
11201115 }
11211116 }
11221117
@@ -1844,15 +1839,9 @@ impl<T, A: Allocator> Vec<T, A> {
18441839 #[ inline]
18451840 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
18461841 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 ( ) ,
18561845 }
18571846 }
18581847
@@ -1931,11 +1920,12 @@ impl<T, A: Allocator> Vec<T, A> {
19311920 #[ cfg( not( no_global_oom_handling) ) ]
19321921 #[ inline]
19331922 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+ }
19391929 }
19401930
19411931 /// Tries to append elements to `Self` from other buffer.
@@ -2515,31 +2505,10 @@ impl<T, F: FnMut() -> T> ExtendWith<T> for ExtendFunc<F> {
25152505impl < T , A : Allocator > Vec < T , A > {
25162506 #[ cfg( not( no_global_oom_handling) ) ]
25172507 /// 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 ( ) ,
25432512 }
25442513 }
25452514
@@ -2886,27 +2855,10 @@ impl<T, A: Allocator> Vec<T, A> {
28862855 // leaf method to which various SpecFrom/SpecExtend implementations delegate when
28872856 // they have no further optimizations to apply
28882857 #[ 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 ( ) ,
29102862 }
29112863 }
29122864
0 commit comments