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 FromIterator and IntoIterator impls for ThinVec #83821

Merged
merged 1 commit into from
Apr 6, 2021

Conversation

camelid
Copy link
Member

@camelid camelid commented Apr 3, 2021

These should make using ThinVec feel much more like using Vec.
They will allow users of Vec to switch to ThinVec while continuing
to use collect(), for loops, and other parts of the iterator API.

I don't know if there were use cases before for using the iterator API
with ThinVec, but I would like to start using ThinVec in rustdoc,
and having it conform to the iterator API would make the transition
a lot easier.

I added a FromIterator impl, an IntoIterator impl that yields owned
elements, and IntoIterator impls that yield immutable or mutable
references to elements. I also added some unit tests for ThinVec.

@camelid camelid added C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 3, 2021
@rust-highfive
Copy link
Collaborator

r? @petrochenkov

(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 Apr 3, 2021
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@petrochenkov petrochenkov added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 3, 2021
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@camelid camelid added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Apr 3, 2021
@camelid
Copy link
Member Author

camelid commented Apr 3, 2021

Do you think it's worth adding a len() method too?

@camelid
Copy link
Member Author

camelid commented Apr 3, 2021

Inference was struggling in the FromIterator test with the into(), so I added a to_vec() method that is private to the tests module. Do you want me to add that to the public API?

@rust-log-analyzer

This comment has been minimized.

@petrochenkov
Copy link
Contributor

Do you think it's worth adding a len() method too?

I wouldn't personally add methods that are not used right now, if they can always be added on demand.
If the upcoming rustdoc changes use it, then it's worth adding.

@petrochenkov petrochenkov added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 3, 2021
@camelid
Copy link
Member Author

camelid commented Apr 4, 2021

Let me know before you r+ and I can squash.

@camelid camelid added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Apr 4, 2021
@rust-log-analyzer

This comment has been minimized.

@petrochenkov
Copy link
Contributor

r=me with commits squashed.

@petrochenkov petrochenkov added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 5, 2021
Comment on lines 12 to 23
pub fn new() -> Self {
ThinVec(None)
}

pub fn iter(&self) -> std::slice::Iter<'_, T> {
self.into_iter()
}

pub fn iter_mut(&mut self) -> std::slice::IterMut<'_, T> {
self.into_iter()
}
}
Copy link
Member

Choose a reason for hiding this comment

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

It looks like this is also missing .push and .pop, I can add those separately. Seems weird that this derefs to a slice instead of a vec.

Copy link
Member

Choose a reason for hiding this comment

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

You couldn't DerefMut to a vec in the None case.

These should make using `ThinVec` feel much more like using `Vec`.
They will allow users of `Vec` to switch to `ThinVec` while continuing
to use `collect()`, `for` loops, and other parts of the iterator API.

I don't know if there were use cases before for using the iterator API
with `ThinVec`, but I would like to start using `ThinVec` in rustdoc,
and having it conform to the iterator API would make the transition
*a lot* easier.

I added a `FromIterator` impl, an `IntoIterator` impl that yields owned
elements, and `IntoIterator` impls that yield immutable or mutable
references to elements. I also added some unit tests for `ThinVec`.
@camelid
Copy link
Member Author

camelid commented Apr 6, 2021

@bors r=petrochenkov

@bors
Copy link
Contributor

bors commented Apr 6, 2021

📌 Commit 09ff88b has been approved by petrochenkov

@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-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Apr 6, 2021
@bors
Copy link
Contributor

bors commented Apr 6, 2021

⌛ Testing commit 09ff88b with merge e1d49aa...

@bors
Copy link
Contributor

bors commented Apr 6, 2021

☀️ Test successful - checks-actions
Approved by: petrochenkov
Pushing e1d49aa to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Apr 6, 2021
@bors bors merged commit e1d49aa into rust-lang:master Apr 6, 2021
@rustbot rustbot added this to the 1.53.0 milestone Apr 6, 2021
@camelid camelid deleted the improve-thinvec branch April 6, 2021 16:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Category: An issue proposing an enhancement or a PR with one. merged-by-bors This PR was explicitly merged by bors. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants