Skip to main content

StaticAllocator

Trait StaticAllocator 

Source
pub unsafe trait StaticAllocator: Allocator { }
🔬This is a nightly-only experimental API. (allocator_api #32838)
Expand description

Marks that an allocator and its supertypes will never invalidate currently allocated memory unless explicitly deallocated via a call to a deallocating method, even if dropped or if the allocator’s lifetime expires.

This is a necessity in conjunction with Pin, as only allocators that promise memory is never reused without a destructor running may be used to back a pinned pointer.

§Safety

Implementors must ensure that memory blocks are only, ever invalidated by a call to a de/reallocating method on Allocator, and that this holds true for all possible instances of all subtypes of the implementor as well.

These requirements trivially apply to allocators that always maintain global state, such as System or Global. However, due to subtype coercion, it is not sound to implement for an arbitrary Allocator + 'static due to edge-case interactions with e.g. Pin::clone. Namely, an impl of StaticAllocator for MyAllocator + 'long guarantees that any value of MyAllocator + 'short also fulfills the requirements of StaticAllocator.

The following must thus be guaranteed:

  • the Drop impl of the allocator does not invalidate any allocations;
  • the allocator does not expose a safe API surface that allows invalidating its allocations;
  • the allocator’s lifetime expiring does not invalidate any allocations;
  • the above also hold for all equivalent allocators (see Allocator docs).

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§