@@ -136,51 +136,39 @@ where
136
136
impl < T , const N : usize > IterExt < T > for std:: array:: IntoIter < T , N > {
137
137
#[ inline]
138
138
fn alloc_from_iter ( self , arena : & TypedArena < T > ) -> & mut [ T ] {
139
- let len = self . len ( ) ;
140
- if len == 0 {
141
- return & mut [ ] ;
142
- }
143
- // Move the content to the arena by copying and then forgetting it
144
- unsafe {
145
- let start_ptr = arena. alloc_raw_slice ( len) ;
146
- self . as_slice ( ) . as_ptr ( ) . copy_to_nonoverlapping ( start_ptr, len) ;
147
- mem:: forget ( self ) ;
148
- slice:: from_raw_parts_mut ( start_ptr, len)
149
- }
139
+ alloc_from_iter ( arena, self . len ( ) , self . as_slice ( ) . as_ptr ( ) , || mem:: forget ( self ) )
150
140
}
151
141
}
152
142
153
143
impl < T > IterExt < T > for Vec < T > {
154
144
#[ inline]
155
145
fn alloc_from_iter ( mut self , arena : & TypedArena < T > ) -> & mut [ T ] {
156
- let len = self . len ( ) ;
157
- if len == 0 {
158
- return & mut [ ] ;
159
- }
160
- // Move the content to the arena by copying and then forgetting it
161
- unsafe {
162
- let start_ptr = arena. alloc_raw_slice ( len) ;
163
- self . as_ptr ( ) . copy_to_nonoverlapping ( start_ptr, len) ;
164
- self . set_len ( 0 ) ;
165
- slice:: from_raw_parts_mut ( start_ptr, len)
166
- }
146
+ alloc_from_iter ( arena, self . len ( ) , self . as_ptr ( ) , || unsafe { self . set_len ( 0 ) } )
167
147
}
168
148
}
169
149
170
150
impl < A : smallvec:: Array > IterExt < A :: Item > for SmallVec < A > {
171
151
#[ inline]
172
152
fn alloc_from_iter ( mut self , arena : & TypedArena < A :: Item > ) -> & mut [ A :: Item ] {
173
- let len = self . len ( ) ;
174
- if len == 0 {
175
- return & mut [ ] ;
176
- }
177
- // Move the content to the arena by copying and then forgetting it
178
- unsafe {
179
- let start_ptr = arena. alloc_raw_slice ( len) ;
180
- self . as_ptr ( ) . copy_to_nonoverlapping ( start_ptr, len) ;
181
- self . set_len ( 0 ) ;
182
- slice:: from_raw_parts_mut ( start_ptr, len)
183
- }
153
+ alloc_from_iter ( arena, self . len ( ) , self . as_ptr ( ) , || unsafe { self . set_len ( 0 ) } )
154
+ }
155
+ }
156
+
157
+ /// Reduce duplication for multiple specializations of alloc_from_iter.
158
+ #[ inline]
159
+ fn alloc_from_iter < T , F > ( arena : & TypedArena < T > , len : usize , ptr : * const T , clean : F ) -> & mut [ T ]
160
+ where
161
+ F : FnOnce ( ) ,
162
+ {
163
+ if len == 0 {
164
+ return & mut [ ] ;
165
+ }
166
+ // Move the content to the arena by copying and then forgetting it
167
+ unsafe {
168
+ let start_ptr = arena. alloc_raw_slice ( len) ;
169
+ ptr. copy_to_nonoverlapping ( start_ptr, len) ;
170
+ clean ( ) ;
171
+ slice:: from_raw_parts_mut ( start_ptr, len)
184
172
}
185
173
}
186
174
0 commit comments