rust-lang / rust-lang/rust

`Allocator::grow` documentation

Open Beginner friendly
#156,733 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-docs T-libs
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Location (URL)

https://doc.rust-lang.org/core/alloc/trait.Allocator.html#method.grow

Summary

The current documentation for Allocator::grow has parts which seem a little ambiguous, as discussed on Zulip here.

In particular, the current phrasing talks about memory ownership being "transferred to this allocator," which a user could misinterpret as referring to memory transfer between two allocators, instead of from the caller back to the original allocator.

Additionally, it seems like "memory/memory block" are used to refer to both an allocation with full temporal provenance (when talking about ownership transfer back to the allocator) and the memory contents without temporal provenance attached (when talking about how the old memory contents are accessible through the newly returned pointer, which comes with new temporal provenance).

Putting these together, I propose to change the documentation to look like this, with changes in bold. Here I replaced "returned" with "transferred" and wrote "memory block" instead of "allocation," to clarify the two points above. I also added another clarifying sentence, as per a suggestion in the Zulip conversation.

If this returns Ok, then ownership of the allocation referenced by ptr has been returned to this allocator. Any access to the old ptr is Undefined Behavior, even if the allocation was grown in-place. In particular, it is UB to offset the old ptr, except if the offset is 0, since it is now derived from a freed allocation. The newly returned pointer is the only valid pointer for accessing this memory now.

If this method returns Err, then ownership of the allocation has not been returned to this allocator, and the contents of the memory block are unaltered.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the Allocator::grow documentation in the linked core::alloc::Allocator API entry. Compare the current ownership and memory-block wording with the proposed replacement and the linked Zulip discussion. Done means the documentation clearly distinguishes returning allocation ownership, invalidating the old pointer, and preserving contents when the method returns Err.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
documentation
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
78/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.