Skip to content

Conversation

@antonilol
Copy link
Contributor

@antonilol antonilol commented Sep 21, 2025

ACP: rust-lang/libs-team#658
Tracking issue: #146975

Text below was written before opening the ACP

Feature was requested in #69939, I recently also needed it so decided to implement it as my first contribution to the Rust standard library. I plan on doing more but wanted to start with a small change.

Some questions I had (both on implementation and design) with answers:

  • Q: extend allows iterators that yield &T where T is Clone, should extend_front do too?
    A: No, users can use copied and/or cloned.
  • Q: Does this need a whole new trait like Extend or only a method on VecDeque?
    A: No, see ACP.
  • Q: How do I deal with all the code duplication? Most code is similar to that of extend, maybe there is a nice way to factor out the code around push_unchecked/push_front_unchecked.
    Will come back to this later.
  • Q: Why are certain things behind feature gates, cfg(not(test)) like vec::IntoIter here and cfg(not(no_global_oom_handling)) like Vec::extend_from_within? (I am also looking at implementing VecDeque::extend_from_within)
    A: See add extend_front to VecDeque with specialization like extend #146861 (review)
  • Q: Should extend_front act like repeated pushes to the front of the queue? This reverses the order of the elements. Doing it different might incur an extra move if the iterator length is not known up front (where do you start placing elements in the buffer?).
    A: extend_front acts like repeated pushes, prepend preserves the element order, see ACP or tracking issue.

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Sep 21, 2025
@workingjubilee workingjubilee added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. needs-acp This change is blocked on the author creating an ACP. labels Sep 21, 2025
@workingjubilee
Copy link
Member

workingjubilee commented Sep 21, 2025

cc @the8472 a bunch of questions re: specialization that I believe you may be able to address

@rust-log-analyzer

This comment has been minimized.

Copy link
Member

@the8472 the8472 left a comment

Choose a reason for hiding this comment

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

Why are certain things behind feature gates, cfg(not(test))

This is due to tests living in the alloctest crate and some of the tests need access to implementation details so they're done with #[path] module imports there which means the originals need to be disabled.
See #136642

cfg(not(no_global_oom_handling)) like

This one is an unstable feature previously used by the linux kernel. IIRC a few other projects are using it too. It's supposed to remove all methods that abort on oom rather than returning an error from alloc's API surface.

Feature was requested in #69939

It's recommended to file an API change proposal to get some preliminary review of the proposed API from the team.

Considering that there are open questions how the API should look like I think it'd be best to have that discussion on an ACP thread so we can focus on the implementation details there.

View changes since this review

}

#[cfg(not(test))]
impl<T, A: Allocator> SpecExtendFront<T, Rev<vec::IntoIter<T>>> for VecDeque<T, A> {
Copy link
Member

Choose a reason for hiding this comment

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

This is a curious specialization. Is the expectation that people will frequently use Rev to prepend to a vecdeque?

Anyway, specializations should have dedicated tests since they're not covered by the regular ones.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a curious specialization. Is the expectation that people will frequently use Rev to prepend to a vecdeque?

This depends on the design of extend_front, if it will act like repeated push_front, specializing this can have significant performance improvements. This would be used when prepending elements from a Vec into a VecDeque, in original order, a bit like the requested VecDeque::prepend (#69939 (comment)).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

prepend (see tracking issue) will use this, it calls self.extend_front(other.into_iter().rev())

@the8472
Copy link
Member

the8472 commented Sep 21, 2025

cc @the8472 a bunch of questions re: specialization that I believe you may be able to address

I don't think any of the questions are specific to specialization?

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@antonilol antonilol force-pushed the vec_deque_extend_front branch 3 times, most recently from 5c9b311 to d4ae120 Compare September 30, 2025 20:36
@antonilol antonilol marked this pull request as ready for review September 30, 2025 20:36
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Sep 30, 2025
@rustbot rustbot removed the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Sep 30, 2025
@rustbot
Copy link
Collaborator

rustbot commented Sep 30, 2025

r? @joboet

rustbot has assigned @joboet.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@antonilol antonilol changed the title add extend_front to VecDeque with specialization like extend (unfinished) add extend_front to VecDeque with specialization like extend Oct 12, 2025
@joboet joboet removed the needs-acp This change is blocked on the author creating an ACP. label Oct 21, 2025
Copy link
Member

@joboet joboet left a comment

Choose a reason for hiding this comment

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

I'm not sure who to ping about the specialisation issue. Apart from that, this seems fine!

View changes since this review

@bors
Copy link
Collaborator

bors commented Nov 1, 2025

☔ The latest upstream changes (presumably #148337) made this pull request unmergeable. Please resolve the merge conflicts.

@antonilol antonilol force-pushed the vec_deque_extend_front branch from d4ae120 to e23c155 Compare November 1, 2025 17:30
@rustbot
Copy link
Collaborator

rustbot commented Nov 1, 2025

This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@rust-log-analyzer

This comment has been minimized.

@antonilol antonilol force-pushed the vec_deque_extend_front branch from 4eb54f5 to 5b96677 Compare November 3, 2025 11:36
@antonilol antonilol requested a review from joboet November 3, 2025 11:39
@joboet
Copy link
Member

joboet commented Nov 6, 2025

Thank you!
@bors r+

@bors
Copy link
Collaborator

bors commented Nov 6, 2025

📌 Commit 5b96677 has been approved by joboet

It is now in the queue for this repository.

@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 Nov 6, 2025
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Nov 6, 2025
…r=joboet

add extend_front to VecDeque with specialization like extend

ACP: rust-lang/libs-team#658
Tracking issue: rust-lang#146975

_Text below was written before opening the ACP_

Feature was requested in rust-lang#69939, I recently also needed it so decided to implement it as my first contribution to the Rust standard library. I plan on doing more but wanted to start with a small change.

Some questions I had (both on implementation and design) with answers:
- Q: `extend` allows iterators that yield `&T` where `T` is `Clone`, should extend_front do too?
  A: No, users can use [`copied`](https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.copied) and/or [`cloned`](https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.cloned).
- Q: Does this need a whole new trait like Extend or only a method on `VecDeque`?
  A: No, see ACP.
- Q: How do I deal with all the code duplication? Most code is similar to that of `extend`, maybe there is a nice way to factor out the code around `push_unchecked`/`push_front_unchecked`.
  Will come back to this later.
- Q: Why are certain things behind feature gates, `cfg(not(test))` like `vec::IntoIter` here and `cfg(not(no_global_oom_handling))` like `Vec::extend_from_within`? (I am also looking at implementing `VecDeque::extend_from_within`)
  A: See rust-lang#146861 (review)
- Q: Should `extend_front` act like repeated pushes to the front of the queue? This reverses the order of the elements. Doing it different might incur an extra move if the iterator length is not known up front (where do you start placing elements in the buffer?).
  A: `extend_front` acts like repeated pushes, `prepend` preserves the element order, see ACP or tracking issue.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Nov 6, 2025
…r=joboet

add extend_front to VecDeque with specialization like extend

ACP: rust-lang/libs-team#658
Tracking issue: rust-lang#146975

_Text below was written before opening the ACP_

Feature was requested in rust-lang#69939, I recently also needed it so decided to implement it as my first contribution to the Rust standard library. I plan on doing more but wanted to start with a small change.

Some questions I had (both on implementation and design) with answers:
- Q: `extend` allows iterators that yield `&T` where `T` is `Clone`, should extend_front do too?
  A: No, users can use [`copied`](https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.copied) and/or [`cloned`](https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.cloned).
- Q: Does this need a whole new trait like Extend or only a method on `VecDeque`?
  A: No, see ACP.
- Q: How do I deal with all the code duplication? Most code is similar to that of `extend`, maybe there is a nice way to factor out the code around `push_unchecked`/`push_front_unchecked`.
  Will come back to this later.
- Q: Why are certain things behind feature gates, `cfg(not(test))` like `vec::IntoIter` here and `cfg(not(no_global_oom_handling))` like `Vec::extend_from_within`? (I am also looking at implementing `VecDeque::extend_from_within`)
  A: See rust-lang#146861 (review)
- Q: Should `extend_front` act like repeated pushes to the front of the queue? This reverses the order of the elements. Doing it different might incur an extra move if the iterator length is not known up front (where do you start placing elements in the buffer?).
  A: `extend_front` acts like repeated pushes, `prepend` preserves the element order, see ACP or tracking issue.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Nov 6, 2025
…r=joboet

add extend_front to VecDeque with specialization like extend

ACP: rust-lang/libs-team#658
Tracking issue: rust-lang#146975

_Text below was written before opening the ACP_

Feature was requested in rust-lang#69939, I recently also needed it so decided to implement it as my first contribution to the Rust standard library. I plan on doing more but wanted to start with a small change.

Some questions I had (both on implementation and design) with answers:
- Q: `extend` allows iterators that yield `&T` where `T` is `Clone`, should extend_front do too?
  A: No, users can use [`copied`](https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.copied) and/or [`cloned`](https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.cloned).
- Q: Does this need a whole new trait like Extend or only a method on `VecDeque`?
  A: No, see ACP.
- Q: How do I deal with all the code duplication? Most code is similar to that of `extend`, maybe there is a nice way to factor out the code around `push_unchecked`/`push_front_unchecked`.
  Will come back to this later.
- Q: Why are certain things behind feature gates, `cfg(not(test))` like `vec::IntoIter` here and `cfg(not(no_global_oom_handling))` like `Vec::extend_from_within`? (I am also looking at implementing `VecDeque::extend_from_within`)
  A: See rust-lang#146861 (review)
- Q: Should `extend_front` act like repeated pushes to the front of the queue? This reverses the order of the elements. Doing it different might incur an extra move if the iterator length is not known up front (where do you start placing elements in the buffer?).
  A: `extend_front` acts like repeated pushes, `prepend` preserves the element order, see ACP or tracking issue.
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this pull request Nov 6, 2025
…r=joboet

add extend_front to VecDeque with specialization like extend

ACP: rust-lang/libs-team#658
Tracking issue: rust-lang#146975

_Text below was written before opening the ACP_

Feature was requested in rust-lang#69939, I recently also needed it so decided to implement it as my first contribution to the Rust standard library. I plan on doing more but wanted to start with a small change.

Some questions I had (both on implementation and design) with answers:
- Q: `extend` allows iterators that yield `&T` where `T` is `Clone`, should extend_front do too?
  A: No, users can use [`copied`](https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.copied) and/or [`cloned`](https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.cloned).
- Q: Does this need a whole new trait like Extend or only a method on `VecDeque`?
  A: No, see ACP.
- Q: How do I deal with all the code duplication? Most code is similar to that of `extend`, maybe there is a nice way to factor out the code around `push_unchecked`/`push_front_unchecked`.
  Will come back to this later.
- Q: Why are certain things behind feature gates, `cfg(not(test))` like `vec::IntoIter` here and `cfg(not(no_global_oom_handling))` like `Vec::extend_from_within`? (I am also looking at implementing `VecDeque::extend_from_within`)
  A: See rust-lang#146861 (review)
- Q: Should `extend_front` act like repeated pushes to the front of the queue? This reverses the order of the elements. Doing it different might incur an extra move if the iterator length is not known up front (where do you start placing elements in the buffer?).
  A: `extend_front` acts like repeated pushes, `prepend` preserves the element order, see ACP or tracking issue.
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. T-libs Relevant to the library team, which will review and decide on the PR/issue. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants