Skip to content

Update safety requirements of Vec::from_raw_parts? #94950

Closed

Description

@EFanZh

The “Safety” section of the document of Vec::from_raw_parts says:

  • length needs to be less than or equal to capacity.
  • capacity needs to be the capacity that the pointer was allocated with.

I think the first requirement should also add “The first length elements starting from ptr are initialized”. And the second requirement could be relaxed into “capacity needs to be less than or equal to the capacity that the pointer was allocated with”, this is because Allocator::deallocate does not require specifying exact size that the pointer was allocated with.

Activity

  1. CryZe commented on Mar 15, 2022

    @CryZe
    Contributor

    Two opinions:

    1. The Allocator API is unstable, I don't think it would make sense to adjust documentation to guarantee unstable behavior.
    2. It's actually even more tricky than your wording, because it also needs to be at least the size of the Layout that was used when allocating the memory block, which you may or may not even know about depending on whether the Vec itself was using the Allocator or you yourself are constructing a Vec from a memory block you manually allocated. So given that Vec probably doesn't want to guarantee any growing behavior, you can't know the Layout that the Vec used, and thus it wouldn't be safe (in general) to use any capacity that is less than what you got from the Vec.
  2. TheDestroyer19 commented on Apr 21, 2022

    @TheDestroyer19

    I just noticed that from_raw_parts and from_raw_parts_in doesn't list a requirement for the first length elements of ptrmust be initialized. A possible wording would be

    • The first length must be initialized.
  3. added
    A-allocatorsArea: Custom and system allocators
    A-docsArea: Documentation for any part of the project, including the compiler, standard library, and tools
    C-discussionCategory: Discussion or questions that doesn't represent real issues.
    T-opsemRelevant to the opsem team
    T-libs-api[DEPRECATED; DO NOT USE]
    and removed on Jan 25, 2024
  4. hkBst commented on Jan 24, 2025

    @hkBst
    Member

    Documentation currently includes this line: "The first length values must be properly initialized values of type T."
    The suggestion about less strict capacity does not seem to be the current reality as Layout for allocation and drop must be the same size and alignment. So there is nothing more to do here.

    Can be closed.

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

    A-allocatorsArea: Custom and system allocatorsA-docsArea: Documentation for any part of the project, including the compiler, standard library, and toolsC-bugCategory: This is a bug.C-discussionCategory: Discussion or questions that doesn't represent real issues.T-libs-api[DEPRECATED; DO NOT USE]T-opsemRelevant to the opsem team

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions