@@ -270,40 +270,38 @@ impl<Item, Gap, const CAP: usize> LinkedChunk<Item, Gap, CAP> {
270
270
271
271
debug_assert ! ( chunk. is_first_chunk( ) . not( ) , "A gap cannot be the first chunk" ) ;
272
272
273
- let ( previous , number_of_items) = match & mut chunk. content {
273
+ let ( maybe_last_chunk_ptr , number_of_items) = match & mut chunk. content {
274
274
ChunkContent :: Gap ( ..) => {
275
275
let items = items. into_iter ( ) ;
276
276
let number_of_items = items. len ( ) ;
277
277
278
- // Find the previous chunk…
279
- //
280
- // SAFETY: `unwrap` is safe because we are ensured `chunk` is not the first
281
- // chunk, so a previous chunk always exists.
282
- let previous = chunk. previous_mut ( ) . unwrap ( ) ;
278
+ let last_inserted_chunk = chunk
279
+ // Insert a new items chunk…
280
+ . insert_next ( Chunk :: new_items_leaked (
281
+ chunk_identifier_generator. generate_next ( ) . unwrap ( ) ,
282
+ ) )
283
+ // … and insert the items.
284
+ . push_items ( items, & chunk_identifier_generator) ;
283
285
284
- // … and insert the items on it.
285
- ( previous. push_items ( items, & chunk_identifier_generator) , number_of_items)
286
+ (
287
+ last_inserted_chunk. is_last_chunk ( ) . then ( || last_inserted_chunk. as_ptr ( ) ) ,
288
+ number_of_items,
289
+ )
286
290
}
287
291
ChunkContent :: Items ( ..) => {
288
292
return Err ( LinkedChunkError :: ChunkIsItems { identifier : chunk_identifier } )
289
293
}
290
294
} ;
291
295
292
- // Get the pointer to `chunk` via `previous`.
293
- //
294
- // SAFETY: `unwrap` is safe because we are ensured the next of the previous
295
- // chunk is `chunk` itself.
296
- chunk_ptr = previous. next . unwrap ( ) ;
297
-
298
- // Get the pointer to the `previous` via `chunk`.
299
- let previous_ptr = chunk. previous ;
300
-
301
296
// Now that new items have been pushed, we can unlink the gap chunk.
302
297
chunk. unlink ( ) ;
303
298
299
+ // Get the pointer to `chunk`.
300
+ chunk_ptr = chunk. as_ptr ( ) ;
301
+
304
302
// Update `self.last` if the gap chunk was the last chunk.
305
- if chunk . is_last_chunk ( ) {
306
- self . last = previous_ptr ;
303
+ if let Some ( last_chunk_ptr ) = maybe_last_chunk_ptr {
304
+ self . last = Some ( last_chunk_ptr ) ;
307
305
}
308
306
309
307
self . length += number_of_items;
@@ -1432,10 +1430,10 @@ mod tests {
1432
1430
#[ test]
1433
1431
fn test_replace_gap_at ( ) -> Result < ( ) , LinkedChunkError > {
1434
1432
let mut linked_chunk = LinkedChunk :: < char , ( ) , 3 > :: new ( ) ;
1435
- linked_chunk. push_items_back ( [ 'a' , 'b' , 'c' ] ) ;
1433
+ linked_chunk. push_items_back ( [ 'a' , 'b' ] ) ;
1436
1434
linked_chunk. push_gap_back ( ( ) ) ;
1437
- linked_chunk. push_items_back ( [ 'l' , 'm' , 'n' ] ) ;
1438
- assert_items_eq ! ( linked_chunk, [ 'a' , 'b' , 'c' ] [ -] [ 'l' , 'm' , 'n '] ) ;
1435
+ linked_chunk. push_items_back ( [ 'l' , 'm' ] ) ;
1436
+ assert_items_eq ! ( linked_chunk, [ 'a' , 'b' ] [ -] [ 'l' , 'm' ] ) ;
1439
1437
1440
1438
// Replace a gap in the middle of the linked chunk.
1441
1439
{
@@ -1445,7 +1443,7 @@ mod tests {
1445
1443
linked_chunk. replace_gap_at ( [ 'd' , 'e' , 'f' , 'g' , 'h' ] , gap_identifier) ?;
1446
1444
assert_items_eq ! (
1447
1445
linked_chunk,
1448
- [ 'a' , 'b' , 'c' ] [ 'd' , 'e' , 'f' ] [ 'g' , 'h' ] [ 'l' , 'm' , 'n ']
1446
+ [ 'a' , 'b' ] [ 'd' , 'e' , 'f' ] [ 'g' , 'h' ] [ 'l' , 'm' ]
1449
1447
) ;
1450
1448
}
1451
1449
@@ -1454,7 +1452,7 @@ mod tests {
1454
1452
linked_chunk. push_gap_back ( ( ) ) ;
1455
1453
assert_items_eq ! (
1456
1454
linked_chunk,
1457
- [ 'a' , 'b' , 'c' ] [ 'd' , 'e' , 'f' ] [ 'g' , 'h' ] [ 'l' , 'm' , 'n '] [ -]
1455
+ [ 'a' , 'b' ] [ 'd' , 'e' , 'f' ] [ 'g' , 'h' ] [ 'l' , 'm' ] [ -]
1458
1456
) ;
1459
1457
1460
1458
let gap_identifier = linked_chunk. chunk_identifier ( Chunk :: is_gap) . unwrap ( ) ;
@@ -1463,11 +1461,11 @@ mod tests {
1463
1461
linked_chunk. replace_gap_at ( [ 'w' , 'x' , 'y' , 'z' ] , gap_identifier) ?;
1464
1462
assert_items_eq ! (
1465
1463
linked_chunk,
1466
- [ 'a' , 'b' , 'c' ] [ 'd' , 'e' , 'f' ] [ 'g' , 'h' ] [ 'l' , 'm' , 'n '] [ 'w' , 'x' , 'y' ] [ 'z' ]
1464
+ [ 'a' , 'b' ] [ 'd' , 'e' , 'f' ] [ 'g' , 'h' ] [ 'l' , 'm' ] [ 'w' , 'x' , 'y' ] [ 'z' ]
1467
1465
) ;
1468
1466
}
1469
1467
1470
- assert_eq ! ( linked_chunk. len( ) , 15 ) ;
1468
+ assert_eq ! ( linked_chunk. len( ) , 13 ) ;
1471
1469
1472
1470
Ok ( ( ) )
1473
1471
}
0 commit comments