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 cannot be freed except via a call to Allocator::deallocate, and that subtype coercion preserves this invariant.

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 Pin::clone. Namely, an impl of StaticAllocator for MyAllocator + 'long guarantees that an impl of StaticAllocator for MyAllocator + 'short would be sound to write.

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§