@@ -121,6 +121,10 @@ use self::spec_from_iter_nested::SpecFromIterNested;
121
121
122
122
mod spec_from_iter_nested;
123
123
124
+ use self :: spec_from_iter:: SpecFromIter ;
125
+
126
+ mod spec_from_iter;
127
+
124
128
/// A contiguous growable array type, written `Vec<T>` but pronounced 'vector'.
125
129
///
126
130
/// # Examples
@@ -2156,98 +2160,6 @@ impl<T, A: Allocator> Extend<T> for Vec<T, A> {
2156
2160
}
2157
2161
}
2158
2162
2159
- /// Specialization trait used for Vec::from_iter
2160
- ///
2161
- /// ## The delegation graph:
2162
- ///
2163
- /// ```text
2164
- /// +-------------+
2165
- /// |FromIterator |
2166
- /// +-+-----------+
2167
- /// |
2168
- /// v
2169
- /// +-+-------------------------------+ +---------------------+
2170
- /// |SpecFromIter +---->+SpecFromIterNested |
2171
- /// |where I: | | |where I: |
2172
- /// | Iterator (default)----------+ | | Iterator (default) |
2173
- /// | vec::IntoIter | | | TrustedLen |
2174
- /// | SourceIterMarker---fallback-+ | | |
2175
- /// | slice::Iter | | |
2176
- /// | Iterator<Item = &Clone> | +---------------------+
2177
- /// +---------------------------------+
2178
- /// ```
2179
- trait SpecFromIter < T , I > {
2180
- fn from_iter ( iter : I ) -> Self ;
2181
- }
2182
-
2183
- impl < T , I > SpecFromIter < T , I > for Vec < T >
2184
- where
2185
- I : Iterator < Item = T > ,
2186
- {
2187
- default fn from_iter ( iterator : I ) -> Self {
2188
- SpecFromIterNested :: from_iter ( iterator)
2189
- }
2190
- }
2191
-
2192
- impl < T > SpecFromIter < T , IntoIter < T > > for Vec < T > {
2193
- fn from_iter ( iterator : IntoIter < T > ) -> Self {
2194
- // A common case is passing a vector into a function which immediately
2195
- // re-collects into a vector. We can short circuit this if the IntoIter
2196
- // has not been advanced at all.
2197
- // When it has been advanced We can also reuse the memory and move the data to the front.
2198
- // But we only do so when the resulting Vec wouldn't have more unused capacity
2199
- // than creating it through the generic FromIterator implementation would. That limitation
2200
- // is not strictly necessary as Vec's allocation behavior is intentionally unspecified.
2201
- // But it is a conservative choice.
2202
- let has_advanced = iterator. buf . as_ptr ( ) as * const _ != iterator. ptr ;
2203
- if !has_advanced || iterator. len ( ) >= iterator. cap / 2 {
2204
- unsafe {
2205
- let it = ManuallyDrop :: new ( iterator) ;
2206
- if has_advanced {
2207
- ptr:: copy ( it. ptr , it. buf . as_ptr ( ) , it. len ( ) ) ;
2208
- }
2209
- return Vec :: from_raw_parts ( it. buf . as_ptr ( ) , it. len ( ) , it. cap ) ;
2210
- }
2211
- }
2212
-
2213
- let mut vec = Vec :: new ( ) ;
2214
- // must delegate to spec_extend() since extend() itself delegates
2215
- // to spec_from for empty Vecs
2216
- vec. spec_extend ( iterator) ;
2217
- vec
2218
- }
2219
- }
2220
-
2221
- impl < ' a , T : ' a , I > SpecFromIter < & ' a T , I > for Vec < T >
2222
- where
2223
- I : Iterator < Item = & ' a T > ,
2224
- T : Clone ,
2225
- {
2226
- default fn from_iter ( iterator : I ) -> Self {
2227
- SpecFromIter :: from_iter ( iterator. cloned ( ) )
2228
- }
2229
- }
2230
-
2231
- // This utilizes `iterator.as_slice().to_vec()` since spec_extend
2232
- // must take more steps to reason about the final capacity + length
2233
- // and thus do more work. `to_vec()` directly allocates the correct amount
2234
- // and fills it exactly.
2235
- impl < ' a , T : ' a + Clone > SpecFromIter < & ' a T , slice:: Iter < ' a , T > > for Vec < T > {
2236
- #[ cfg( not( test) ) ]
2237
- fn from_iter ( iterator : slice:: Iter < ' a , T > ) -> Self {
2238
- iterator. as_slice ( ) . to_vec ( )
2239
- }
2240
-
2241
- // HACK(japaric): with cfg(test) the inherent `[T]::to_vec` method, which is
2242
- // required for this method definition, is not available. Instead use the
2243
- // `slice::to_vec` function which is only available with cfg(test)
2244
- // NB see the slice::hack module in slice.rs for more information
2245
- #[ cfg( test) ]
2246
- fn from_iter ( iterator : slice:: Iter < ' a , T > ) -> Self {
2247
- crate :: slice:: to_vec ( iterator. as_slice ( ) , Global )
2248
- }
2249
- }
2250
-
2251
2163
// Specialization trait used for Vec::extend
2252
2164
trait SpecExtend < T , I > {
2253
2165
fn spec_extend ( & mut self , iter : I ) ;
0 commit comments