Skip to content

Commit 5e6abf1

Browse files
committed
add ascii illustration
1 parent cb477d4 commit 5e6abf1

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

library/alloc/src/vec.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2386,7 +2386,26 @@ where
23862386
impl<T> SpecExtend<T, IntoIter<T>> for Vec<T> {
23872387
fn spec_extend(&mut self, iterator: IntoIter<T>) {
23882388
// Avoid reallocation if we can use iterator's storage instead. This requires 1 memcpy and 0-1 memmove
2389-
// while reallocation would require 1 alloc, 1-2 memcpy, 1-2 free
2389+
// while reallocation would require 1 alloc, 1-2 memcpy, 1-2 free.
2390+
//
2391+
// A) non-empty self, partially consumed iterator
2392+
//
2393+
// self iterator
2394+
// |AAAA | | BBB |
2395+
// |AAAA | | BBB | into_vec_with_uninit_prefix
2396+
// | | |AAAABBB | prepend
2397+
// |AAAABBB | -- *self = v
2398+
//
2399+
// B) empty self, partially consumed iterator
2400+
//
2401+
// | | | BBBB |
2402+
// | | |BBBB | into_vec_with_uninit_prefix
2403+
// |BBBB | -- *self = v
2404+
//
2405+
// C) empty self, pristine iterator
2406+
//
2407+
// | | |BBBB |
2408+
// |BBBB | -- *self = v
23902409
if mem::size_of::<T>() > 0
23912410
&& self.capacity() - self.len() < iterator.len()
23922411
&& iterator.cap - iterator.len() >= self.len()

0 commit comments

Comments
 (0)