-
Notifications
You must be signed in to change notification settings - Fork 19
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
Make VecDeque::new
const, add guarantee for O(1) Vec<T> -> VecDeque<T>
conversion
#138
Comments
I forgot to mention it, but obviously this would also include |
As a bonus if the O(1) conversion happens, we could delegate (Could even spell it |
I've actually tried this before (in my original deque implementation, not in stdlib code), and it was noticably slower than the manual implementation. I'm not exactly sure why that is, maybe I just did something wrong or forgot to mark it |
The relevant PR got merged, so I don't think there's any more blockers for this ACP now. |
I'd love to have this. It'd be amazing to change the answer for "how do I remove a bunch of things from the front of a |
Seconded |
An additional fun thing this will let us do: collect a @Sp00ph with the second here, I think you're clear to open a PR with these changes -- the |
@scottmcm I probably need to add |
Oh, right. Probably the official way is you first make a doesn't-need-FCP change to make it unstably const, for which you'd open a tracking issue using the template in https://github.com/rust-lang/rust/issues/new/choose. And yes, you just make up the tracking issue name. Then the FCP-needing PR would change it from (I'd be tempted to just go straight to stable, since it's not some complicated new compile-time functionality, but I don't know if just having an ACP is enough for that. Up to libs-api how strict they'd want to be.) |
So what's the best approach then in your opinion? Should I wait until I get confirmation from the libs-api team about whether I can go straight to stable? Or should I just open a PR going straight to stable and then fall back to the full process in case that PR gets rejected? |
My default answer is do whatever's easier, and then if libs-api says to do something else do that 🙃 |
Let's pick up the discussion in the 3 PRs. Thank you @Sp00ph! |
Proposal
Problem statement
Unlike almost all std collections,
VecDeque<T>
does not have aconst fn new()
. Also, there is currently no way to convert aVec<T>
into aVecDeque<T>
without potentially reallocating.Motivation, use-cases
The Rust std collections generally allocate lazily, allowing the constructor to be basically free and making almost all of the constructors
const fn
s. Specifically,{Vec, LinkedList, BinaryHeap}::new
are alreadyconst fn
on stable, and{BTreeMap, BTreeSet}::new
will becomeconst fn
with Rust 1.66. The fact thatVecDeque::new
isn't markedconst
seems like an unfortunate implementation detail, which will hopefully be able to be changed if this PR gets merged.Since the current
VecDeque<T>
implementation is constrained to power-of-2 sizes only, converting aVec<T>
into aVecDeque<T>
may have to reallocate the entire buffer. By guaranteeing that this conversion be O(1), it allows users to freely convert betweenVec<T>
andVecDeque<T>
without ever reallocating and makes it trivial to e.g. turn an existingVec<T>
into a FIFO queue.Solution sketches
If the
VecDeque
rewrite gets merged, all that would remain would be to markVecDeque::new
as aconst fn
, and to add a doc comment to theimpl From<Vec<T>> for VecDeque<T> {...}
stating that the conversion is guaranteed to be O(1) and to not reallocate. If the PR doesn't get merged, this ACP would probably need to be dropped until some otherVecDeque<T>
rewrite makes the changes possible.Links and related work
rust-lang/rust#102991 (
VecDeque
rewrite PR)What happens now?
This issue is part of the libs-api team API change proposal process. Once this issue is filed the libs-api team will review open proposals in its weekly meeting. You should receive feedback within a week or two.
The text was updated successfully, but these errors were encountered: