Skip to content

Vec const-ness #643

Open
Open

Description

@tower120

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.

I think I can implement that, if you accept PR's.

Activity

  1. sgued commented on Jan 28, 2026

    @sgued
    Contributor

    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.

  2. tower120 commented on Jan 28, 2026

    @tower120
    ContributorAuthor

    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).

  3. tower120 commented on Jan 29, 2026

    @tower120
    ContributorAuthor

    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...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions