Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 29e29ce

Browse files
committedMar 23, 2022
fix some links, clarify documentation based on review feedback
1 parent 7549cfa commit 29e29ce

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed
 

‎library/alloc/src/vec/in_place_collect.rs

+19-12
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
//! Inplace iterate-and-collect specialization for `Vec`
22
//!
3+
//! Note: This documents Vec internals, some of the following sections explain implementation
4+
//! details and are best read together with the source of this module.
5+
//!
36
//! The specialization in this module applies to iterators in the shape of
47
//! `source.adapter().adapter().adapter().collect::<Vec<U>>()`
5-
//! where `source` is an owning iterator obtained from [`Vec<T>`], [`Box<[T]>`] (by conversion to `Vec`)
8+
//! where `source` is an owning iterator obtained from [`Vec<T>`], [`Box<[T]>`][box] (by conversion to `Vec`)
69
//! or [`BinaryHeap<T>`], the adapters each consume one or more items per step
710
//! (represented by [`InPlaceIterable`]), provide transitive access to `source` (via [`SourceIter`])
811
//! and thus the underlying allocation. And finally the layouts of `T` and `U` must
9-
//! have the same size and alignment, this is currently ensured via const eval instead of trait
10-
//! bounds.
12+
//! have the same size and alignment, this is currently ensured via const eval instead of trait bounds
13+
//! in the specialized [`SpecFromIter`] implementation.
1114
//!
1215
//! [`BinaryHeap<T>`]: crate::collections::BinaryHeap
13-
//! [`Box<[T]>`]: crate::boxed::Box
16+
//! [box]: crate::boxed::Box
1417
//!
15-
//! By extension some other collections which use `collect::Vec<_>()` internally in their
18+
//! By extension some other collections which use `collect::<Vec<_>>()` internally in their
1619
//! `FromIterator` implementation benefit from this too.
1720
//!
1821
//! Access to the underlying source goes through a further layer of indirection via the private
@@ -27,10 +30,10 @@
2730
//! # Reading from and writing to the same allocation
2831
//!
2932
//! By its nature collecting in place means that the reader and writer side of the iterator
30-
//! use the same allocation. Since `fold()` and co. take a reference to the iterator for the
31-
//! duration of the iteration that means we can't interleave the step of reading a value
32-
//! and getting a reference to write to. Instead raw pointers must be used on the reader
33-
//! and writer side.
33+
//! use the same allocation. Since `try_fold()` (used in [`SpecInPlaceCollect`]) takes a
34+
//! reference to the iterator for the duration of the iteration that means we can't interleave
35+
//! the step of reading a value and getting a reference to write to. Instead raw pointers must be
36+
//! used on the reader and writer side.
3437
//!
3538
//! That writes never clobber a yet-to-be-read item is ensured by the [`InPlaceIterable`] requirements.
3639
//!
@@ -49,25 +52,29 @@
4952
//! All those drops in turn can panic which then must either leak the allocation or abort to avoid
5053
//! double-drops.
5154
//!
52-
//! These tasks are handled by [`InPlaceDrop`] and [`vec::IntoIter::forget_allocation_drop_remaining()`]
55+
//! This is handled by the [`InPlaceDrop`] guard for sink items (`U`) and by
56+
//! [`vec::IntoIter::forget_allocation_drop_remaining()`] for remaining source items (`T`).
5357
//!
5458
//! [`vec::IntoIter::forget_allocation_drop_remaining()`]: super::IntoIter::forget_allocation_drop_remaining()
5559
//!
5660
//! # O(1) collect
5761
//!
5862
//! The main iteration itself is further specialized when the iterator implements
5963
//! [`TrustedRandomAccessNoCoerce`] to let the optimizer see that it is a counted loop with a single
60-
//! induction variable. This can turn some iterators into a noop, i.e. it reduces them from O(n) to
64+
//! [induction variable]. This can turn some iterators into a noop, i.e. it reduces them from O(n) to
6165
//! O(1). This particular optimization is quite fickle and doesn't always work, see [#79308]
6266
//!
6367
//! [#79308]: https://github.com/rust-lang/rust/issues/79308
68+
//! [induction variable]: https://en.wikipedia.org/wiki/Induction_variable
6469
//!
6570
//! Since unchecked accesses through that trait do not advance the read pointer of `IntoIter`
6671
//! this would interact unsoundly with the requirements about dropping the tail described above.
6772
//! But since the normal `Drop` implementation of `IntoIter` would suffer from the same problem it
6873
//! is only correct for `TrustedRandomAccessNoCoerce` to be implemented when the items don't
6974
//! have a destructor. Thus that implicit requirement also makes the specialization safe to use for
7075
//! in-place collection.
76+
//! Note that this safety concern is about the correctness of `impl Drop for IntoIter`,
77+
//! not the guarantees of `InPlaceIterable`.
7178
//!
7279
//! # Adapter implementations
7380
//!
@@ -76,7 +83,7 @@
7683
//! For example `InPlaceIterable` would be valid to implement for [`Peekable`], except
7784
//! that it is stateful, cloneable and `IntoIter`'s clone implementation shortens the underlying
7885
//! allocation which means if the iterator has been peeked and then gets cloned there no longer is
79-
//! enough room, thus breaking an invariant (#85322).
86+
//! enough room, thus breaking an invariant ([#85322]).
8087
//!
8188
//! [#85322]: https://github.com/rust-lang/rust/issues/85322
8289
//! [`Peekable`]: core::iter::Peekable

0 commit comments

Comments
 (0)
Failed to load comments.