@@ -118,6 +118,8 @@ extern crate std as alloc;
118118#[ cfg( feature = "serde" ) ]
119119mod serde;
120120
121+ mod builder;
122+
121123use alloc:: vec:: { self , Vec } ;
122124use core:: iter:: { self , FromIterator , FusedIterator } ;
123125use core:: { fmt, mem, ops, slice} ;
@@ -1260,51 +1262,12 @@ impl<T> FromIterator<(usize, T)> for Slab<T> {
12601262 I : IntoIterator < Item = ( usize , T ) > ,
12611263 {
12621264 let iterator = iterable. into_iter ( ) ;
1263- let mut slab = Self :: with_capacity ( iterator. size_hint ( ) . 0 ) ;
1265+ let mut builder = builder :: Builder :: with_capacity ( iterator. size_hint ( ) . 0 ) ;
12641266
1265- let mut vacant_list_broken = false ;
1266- let mut first_vacant_index = None ;
12671267 for ( key, value) in iterator {
1268- if key < slab. entries . len ( ) {
1269- // iterator is not sorted, might need to recreate vacant list
1270- if let Entry :: Vacant ( _) = slab. entries [ key] {
1271- vacant_list_broken = true ;
1272- slab. len += 1 ;
1273- }
1274- // if an element with this key already exists, replace it.
1275- // This is consistent with HashMap and BtreeMap
1276- slab. entries [ key] = Entry :: Occupied ( value) ;
1277- } else {
1278- if first_vacant_index. is_none ( ) && slab. entries . len ( ) < key {
1279- first_vacant_index = Some ( slab. entries . len ( ) ) ;
1280- }
1281- // insert holes as necessary
1282- while slab. entries . len ( ) < key {
1283- // add the entry to the start of the vacant list
1284- let next = slab. next ;
1285- slab. next = slab. entries . len ( ) ;
1286- slab. entries . push ( Entry :: Vacant ( next) ) ;
1287- }
1288- slab. entries . push ( Entry :: Occupied ( value) ) ;
1289- slab. len += 1 ;
1290- }
1268+ builder. pair ( key, value)
12911269 }
1292- if slab. len == slab. entries . len ( ) {
1293- // no vacant entries, so next might not have been updated
1294- slab. next = slab. entries . len ( ) ;
1295- } else if vacant_list_broken {
1296- slab. recreate_vacant_list ( ) ;
1297- } else if let Some ( first_vacant_index) = first_vacant_index {
1298- let next = slab. entries . len ( ) ;
1299- match & mut slab. entries [ first_vacant_index] {
1300- Entry :: Vacant ( n) => * n = next,
1301- _ => unreachable ! ( ) ,
1302- }
1303- } else {
1304- unreachable ! ( )
1305- }
1306-
1307- slab
1270+ builder. build ( )
13081271 }
13091272}
13101273
0 commit comments