@@ -2388,27 +2388,41 @@ impl<T> SpecExtend<T, IntoIter<T>> for Vec<T> {
2388
2388
// Avoid reallocation if we can use iterator's storage instead. This requires 1 memcpy and 0-1 memmove
2389
2389
// while reallocation would require 1 alloc, 1-2 memcpy, 1-2 free.
2390
2390
//
2391
+ // # illustration of some extend scenarios (not exhaustive)
2392
+ //
2393
+ // == step == == memory == == self == == iter / v ==
2394
+ // 0123456789abcdef0123456789abcdef
2395
+ // 0---------------1---------------
2396
+ //
2391
2397
// ## non-empty self, partially consumed iterator
2392
2398
//
2393
- // == step == == memory == == self == == iter / v ==
2394
- // 0123456789abcdef0123456789abcdef
2395
- // 0---------------1---------------
2399
+ // [initial] AAAA_-----__BBB___-------------- Vec(0x00, 4, 5) IntoIter(0x0a, 0x0c, 0x0f, 8)
2400
+ // ³ into_vec AAAA_-----____BBB_-------------- Vec(0x00, 4, 5) Vec(0x0a, 7, 8)
2401
+ // ² prepend _____-----AAAABBB_-------------- Vec(0x00, 0, 5) Vec(0x0a, 7, 8)
2402
+ // ⁴ *self = v ----------AAAABBB_-------------- Vec(0x0a, 7, 8)
2403
+ //
2404
+ // ## empty self, partially consumed iterator
2405
+ //
2406
+ // [initial] ____------__BBBB__-------------- Vec(0x00, 0, 4) IntoIter(0x0a, 0x0c, 0x10, 8)
2407
+ // ³ into_vec ____------BBBB____-------------- Vec(0x00, 0, 4) Vec(0x0a, 4, 8)
2408
+ // ⁴ *self = v ----------BBBB____-------------- Vec(0x0a, 4, 8)
2396
2409
//
2397
- // [initial] AAAA_-----__BBB___-------------- Vec(0x00, 4, 5) IntoIter(0x0a, 0x0c, 0x0f, 8)
2398
- // into_vec AAAA_-----____BBB_-------------- Vec(0x00, 4, 5) Vec(0x0a, 7, 8)
2399
- // prepend _____-----AAAABBB_-------------- Vec(0x00, 0, 5) Vec(0x0a, 7, 8)
2400
- // *self = v ----------AAAABBB_-------------- Vec(0x0a, 7, 8)
2410
+ // ## empty self, pristine iterator
2401
2411
//
2402
- // ## empty self, partially consumed iterator
2412
+ // [initial] ----------BBBB____-------------- Vec(0x00, 0, 0) IntoIter(0x0a, 0x0a, 0x0e, 8)
2413
+ // *self = v ----------BBBB____-------------- Vec(0x0a, 4, 8)
2403
2414
//
2404
- // [initial] ____------__BBBB__-------------- Vec(0x00, 0, 4) IntoIter(0x0a, 0x0c, 0x10, 8)
2405
- // into_vec ____------BBBB____-------------- Vec(0x00, 0, 4) Vec(0x0a, 4, 8)
2406
- // *self = v ----------BBBB____-------------- Vec(0x0a, 4, 8)
2415
+ // ## insufficient capacity
2407
2416
//
2408
- // ## empty self, pristine iterator
2417
+ // [initial] AAAAA-----BBBBBB__-------------- Vec(0x00, 5, 5) IntoIter(0x0a, 0x0a, 0x0f, 8)
2418
+ // ¹² reserve(6) ----------BBBBBB__--AAAAA______- Vec(0x14, 5, 11) IntoIter(0x0a, 0x0a, 0x0f, 8)
2419
+ // ² ptr:copy_n ----------________--AAAAABBBBBB- Vec(0x14, 11, 11) IntoIter(0x0a, 0x0f, 0x0f, 8)
2420
+ // ⁴ drop --------------------AAAAABBBBBB- Vec(0x14, 11, 11)
2409
2421
//
2410
- // [initial] ----------BBBB____-------------- Vec(0x00, 0, 0) IntoIter(0x0a, 0x0a, 0x0e, 8)
2411
- // *self = v ----------BBBB____-------------- Vec(0x0a, 4, 8)
2422
+ // ¹ malloc
2423
+ // ² memcpy
2424
+ // ³ memmove
2425
+ // ⁴ free
2412
2426
//
2413
2427
if mem:: size_of :: < T > ( ) > 0
2414
2428
&& self . capacity ( ) - self . len ( ) < iterator. len ( )
0 commit comments