Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VecDeque::with_capacity contradicts std::collections module documentation #59931

Closed
gluyas opened this issue Apr 13, 2019 · 0 comments · Fixed by #59984
Closed

VecDeque::with_capacity contradicts std::collections module documentation #59931

gluyas opened this issue Apr 13, 2019 · 0 comments · Fixed by #59984
Labels
A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools C-bug Category: This is a bug.

Comments

@gluyas
Copy link
Contributor

gluyas commented Apr 13, 2019

From std::collections module-level doc (emphasis added):

Any with_capacity constructor will instruct the collection to allocate enough space for the specified number of elements. Ideally this will be for exactly that many elements, but some implementation details may prevent this. Vec and VecDeque can be relied on to allocate exactly the requested amount, though.

However, the implementation actually requires that the capacity is a power of 2, taking advantage of bitmask modulo tricks. It also needs to keep a single empty slot in the buffer.

playground example

There are some use cases that would want this feature: rotate would be an O(1) operation when len == cap, which could be useful in sliding-window type applications. But it doesn't seem like something general deque usage would really care about. So probably shouldn't reimplement the whole data structure and just change the doc to not mention VecDeque here.

@jonas-schievink jonas-schievink added C-bug Category: This is a bug. A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools labels Apr 13, 2019
Centril added a commit to Centril/rust that referenced this issue Apr 17, 2019
…c-fix, r=rkruppe

Remove collection-specific `with_capacity` documentation from `std::collections`

Fixes rust-lang#59931

The style of `std::collections` module doc is very much a beginner friendly guide, and documenting niche, collection-specific behaviour feels out of place, if not brittle.

The note about `VecDeque` is outdated (see issue), and while `Vec` probably won't change its guarantees any time soon, the users who are interested in its allocation properties will find that in its own documentation.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools C-bug Category: This is a bug.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants