Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions library/core/src/alloc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,12 @@ impl fmt::Display for AllocError {
///
/// Memory blocks that are [*currently allocated*] by an allocator,
/// must point to valid memory, and retain their validity until either:
/// - the memory block is deallocated, or
/// - the allocator is dropped.
/// - the memory block is deallocated,
/// - the allocator is mutated through public API taking `&mut` access (notably,
/// running the allocator's destructor is such a mutation), or
Comment on lines +95 to +96

@Amanieu Amanieu Jun 4, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this needs to be in the Allocator docs: the Allocator trait itself doesn't have any methods that take &mut self, and if you're calling allocator-specific methods then it's up to them to document the effect on allocated objections.

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The point of this is: Box and other collections aren't allowed to have a safe allocator_mut method that gives out a &mut A. In exchange, allocator implementations are allowed to have a safe &mut self method that invalidates allocations.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a separate paragraph in the safety section:

Safe methods taking &self on types that implement Allocator must not cause the lifetime of an allocation to end early.

And maybe another sentence explaining that this is because collections can expose immutable access to their allocator.

/// - the allocator's type becomes invalid.
/// (For example, the type `&'a T` becomes invalid when `'a` expires.
/// More generally, a type becomes invalid when any of its lifetime parameters has expired.)
Comment on lines +97 to +99

@Amanieu Amanieu Jun 4, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't believe "type becomes invalid" is a concept that we've previously used elsewhere. Instead this should just explicitly mirror the wording from the docs of the allocate method which lists the 2 conditions which can cause an allocation to become invalid.

View changes since the review

///
/// Copying, cloning, or moving the allocator must not invalidate memory blocks returned from it.
/// A copied or cloned allocator must behave like the original allocator.
Expand Down
Loading