Skip to content

Commit aa4a4ac

Browse files
committed
annotate used primitives in ascii illustration
1 parent 1dbab48 commit aa4a4ac

File tree

1 file changed

+28
-14
lines changed

1 file changed

+28
-14
lines changed

library/alloc/src/vec.rs

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2388,27 +2388,41 @@ impl<T> SpecExtend<T, IntoIter<T>> for Vec<T> {
23882388
// Avoid reallocation if we can use iterator's storage instead. This requires 1 memcpy and 0-1 memmove
23892389
// while reallocation would require 1 alloc, 1-2 memcpy, 1-2 free.
23902390
//
2391+
// # illustration of some extend scenarios (not exhaustive)
2392+
//
2393+
// == step == == memory == == self == == iter / v ==
2394+
// 0123456789abcdef0123456789abcdef
2395+
// 0---------------1---------------
2396+
//
23912397
// ## non-empty self, partially consumed iterator
23922398
//
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)
23962409
//
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
24012411
//
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)
24032414
//
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
24072416
//
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)
24092421
//
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
24122426
//
24132427
if mem::size_of::<T>() > 0
24142428
&& self.capacity() - self.len() < iterator.len()

0 commit comments

Comments
 (0)