You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I don't know if this suggestion was raised before in this crate, but it looks like Vec::from_array can be implemented in const context.
Along with some other methods.
Yes, PRs would be welcome for more constness ! Last I remember not many things could be made const due to compiler limitations, but I might be wrong and would love to be proven so by a PR :)
PRs introducing a breaking change would be delayed for a long time, so if you must make some breaking changes to make some functions const, please make that a separate PR from other non-breaking const improvements, that will speed-up merging the ones that can be released now.
I have managed to implement const from_array without structural changes.
But implementing const set_len() , capacity() requires their "specialized" Vec-specific versions.
This can be achieved either through adding separate set_len_const()capacity_const(), etc...
Either through making Vec a separate type with VecInner inside - this way we can forward most
methods to VecInner implementation, and then one-by-one move member functions const.
If we go with the second option - we could make macro that forward trait implementations to VecInner - in case, if you have more types with fixed capacity that could go this way.
(I'm not familiar enough with heapless - maybe strings, or something like that - could be refactored this way later).
Just a follow up to a "second option". I see that you sometimes rely on coercion to a VecView. This can be replaced with trait IntoVecView. Then, in all places where you expect VecView arg, you work with impl IntoVecView - in analogy to IntoIterator. This way all functions will still work with both Vec and VecView.
Of course, these all are breaking changes...
UPDATE. On a second thought - we could just Deref to an inner VecView and redefine all const functions with the same names in Vec. This way self. will always lead to Vec implementations, and arg: &VecView - to VecView implementations. Don't know how "beautiful" it will look though...
Activity
sgued commentedon Jan 28, 2026
Yes, PRs would be welcome for more constness ! Last I remember not many things could be made const due to compiler limitations, but I might be wrong and would love to be proven so by a PR :)
PRs introducing a breaking change would be delayed for a long time, so if you must make some breaking changes to make some functions const, please make that a separate PR from other non-breaking const improvements, that will speed-up merging the ones that can be released now.
tower120 commentedon Jan 28, 2026
I have managed to implement
const from_arraywithout structural changes.But implementing const
set_len(),capacity()requires their "specialized"Vec-specific versions.This can be achieved either through adding separate
set_len_const()capacity_const(), etc...Either through making
Veca separate type withVecInnerinside - this way we can forward mostmethods to
VecInnerimplementation, and then one-by-one move member functionsconst.If we go with the second option - we could make macro that forward trait implementations to
VecInner- in case, if you have more types with fixed capacity that could go this way.(I'm not familiar enough with
heapless- maybe strings, or something like that - could be refactored this way later).tower120 commentedon Jan 29, 2026
Just a follow up to a "second option". I see that you sometimes rely on coercion to a
VecView. This can be replaced withtrait IntoVecView. Then, in all places where you expectVecViewarg, you work withimpl IntoVecView- in analogy toIntoIterator. This way all functions will still work with bothVecandVecView.Of course, these all are breaking changes...
UPDATE. On a second thought - we could just
Derefto an innerVecViewand redefine all const functions with the same names inVec. This wayself.will always lead toVecimplementations, andarg: &VecView- toVecViewimplementations. Don't know how "beautiful" it will look though...