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

Add some Vec <-> VecDeque documentation #61447

Merged
merged 6 commits into from
Jun 16, 2019
Merged

Conversation

scottmcm
Copy link
Member

@scottmcm scottmcm commented Jun 2, 2019

These are more than just .into_iter().collect(), so talk about some of their nuances.

For VecDeque -> Vec I'm trying to intentionally not write a guarantee for people making their own Vecs, since the rules are more complicated than I think we want to commit to forever.

The "Vec -> VecDeque doesn't reallocate" guarantee seems reasonable, though. (And I'm intentionally ambiguous about when it's O(1) instead of O(n).)

@rust-highfive
Copy link
Collaborator

r? @sfackler

(rust_highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jun 2, 2019
Copy link
Contributor

@Centril Centril left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some proof-reading.

src/liballoc/collections/vec_deque.rs Outdated Show resolved Hide resolved
src/liballoc/collections/vec_deque.rs Outdated Show resolved Hide resolved
src/liballoc/collections/vec_deque.rs Outdated Show resolved Hide resolved
src/liballoc/collections/vec_deque.rs Outdated Show resolved Hide resolved
src/liballoc/collections/vec_deque.rs Outdated Show resolved Hide resolved
src/liballoc/collections/vec_deque.rs Outdated Show resolved Hide resolved
src/liballoc/collections/vec_deque.rs Outdated Show resolved Hide resolved
src/liballoc/collections/vec_deque.rs Outdated Show resolved Hide resolved
src/liballoc/collections/vec_deque.rs Outdated Show resolved Hide resolved
src/liballoc/collections/vec_deque.rs Outdated Show resolved Hide resolved
@czipperz
Copy link
Contributor

czipperz commented Jun 2, 2019

What's the precedent for documenting trait impl blocks vs trait functions? My understanding is the former is easier to see on the docs webpage because functions are minimized.

@scottmcm
Copy link
Member Author

scottmcm commented Jun 2, 2019

@czipperz Well, @Mark-Simulacrum said on discord,

I'd go with method since it's somewhat clearer imo what you're specifically documenting, though for From it doesn't matter since there's just the one method

(I'm happy to do whatever here.)

@czipperz
Copy link
Contributor

czipperz commented Jun 2, 2019

Looking through other parts of std, it seems like documenting the method is standard. Also, when you override the documentation it automatically expands in rustdoc: try https://doc.rust-lang.org/nightly/std/net/enum.SocketAddr.html for From<(I, u16)>

@Mark-Simulacrum
Copy link
Member

In rust-lang/rust a loose grep appears to suggest 96 cases of documentation on trait impls (among ~20,000 trait impls) so I would say the clear preference is definitely for the method.

/// // Start with a `VecDeque<i32>`.
/// let deque: VecDeque<_> = (1..5).collect();
///
/// // Turn it into a `Vec<i32>` with no allocation needed.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the motivation for spending so much time worrying about reallocation here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there is a few open issues where people want to know about our allocation strategy for various From impls. In other words, people want to know so they are being explicit about it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Basically, the only thing interesting about these two From impls is that they can (sometimes) avoid allocations compared to .into_iter().collect(). So I want to write an example that shows the lack of reallocation, but I also want to write it in such a way that it's a situation where we'd be ok committing to the lack of reallocation. So I want to make an example that doesn't do things like "well, I happen to know that right now it doesn't reallocate if I with_capacity(8) and don't use all the capacity", because I don't want to write text elaborating why those conditions work.

I agree this example is far more complicated than most, though, so would be happy to change it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if an example is really helpful at all here, if we don't actually want to guarantee anything.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sfackler Ok, I removed the example from here.

@czipperz
Copy link
Contributor

czipperz commented Jun 6, 2019

Should we be including links to VecDeque from Vec and Vec from VecDeque? I think this makes sense because it increases the navigability of the source code. But the docs will already link to them for us as part of the signature. I would still go on the side of links.

@rust-highfive

This comment has been minimized.

scottmcm and others added 5 commits June 8, 2019 22:38
These are more than just `.into_iter().collect()`, so talk about some of their nuances.
Co-Authored-By: Mazdak Farrokhzad <twingoow@gmail.com>
Since simulacrum suggested (on Discord) they're better there.
Co-Authored-By: Joe ST <joe@fbstj.net>
Let's try the auto-linking instead, since the relative ones don't work.
@czipperz
Copy link
Contributor

So converting to VecDeque can reallocate and to Vec can shift elements around

@sfackler
Copy link
Member

@bors r+

@bors
Copy link
Contributor

bors commented Jun 16, 2019

📌 Commit 0150448 has been approved by sfackler

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 16, 2019
Centril added a commit to Centril/rust that referenced this pull request Jun 16, 2019
Add some Vec <-> VecDeque documentation

These are more than just `.into_iter().collect()`, so talk about some of their nuances.

For VecDeque -> Vec I'm trying to intentionally not write a guarantee for people making their own `Vec`s, since the rules are more complicated than I think we want to commit to forever.

The "Vec -> VecDeque doesn't reallocate" guarantee seems reasonable, though.  (And I'm intentionally ambiguous about when it's O(1) instead of O(n).)
bors added a commit that referenced this pull request Jun 16, 2019
Rollup of 6 pull requests

Successful merges:

 - #61447 (Add some Vec <-> VecDeque documentation)
 - #61704 (Pass LLVM linker flags to librustc_llvm build)
 - #61829 (rustbuild: include llvm-libunwind in dist tarball)
 - #61832 (update miri)
 - #61866 (Remove redundant `clone()`s)
 - #61869 (Cleanup some new active feature gates)

Failed merges:

r? @ghost
@bors bors merged commit 0150448 into rust-lang:master Jun 16, 2019
@scottmcm scottmcm deleted the vec-vecdeque branch June 16, 2019 19:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants