Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 0187bd8

Browse files
committedDec 12, 2023
Clarify the lifetimes of allocations returned by the Allocator trait
The previous definition (accidentally) disallowed the implementation of stack-based allocators whose memory would become invalid once the lifetime of the allocator type ended. This also ensures the validity of the following blanket implementation: ```rust impl<A: Allocator> Allocator for &'_ A {} ```
1 parent 4f3da90 commit 0187bd8

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed
 

‎library/core/src/alloc/mod.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,10 @@ impl fmt::Display for AllocError {
9595
/// # Safety
9696
///
9797
/// * Memory blocks returned from an allocator that are [*currently allocated*] must point to
98-
/// valid memory and retain their validity while they are [*currently allocated*] and at
99-
/// least one of the instance and all of its clones has not been dropped.
98+
/// valid memory and retain their validity while they are [*currently allocated*] and the shorter
99+
/// of:
100+
/// - the borrow-checker lifetime of the allocator type itself.
101+
/// - as long as at least one of the instance and all of its clones has not been dropped.
100102
///
101103
/// * copying, cloning, or moving the allocator must not invalidate memory blocks returned from this
102104
/// allocator. A copied or cloned allocator must behave like the same allocator, and

0 commit comments

Comments
 (0)
Please sign in to comment.