pub unsafe trait Linked<L> {
type Handle;
fn into_ptr(r: Self::Handle) -> NonNull<Self>;
unsafe fn from_ptr(ptr: NonNull<Self>) -> Self::Handle;
unsafe fn links(ptr: NonNull<Self>) -> NonNull<L>;
}
Expand description
Trait implemented by types which can be members of an intrusive collection.
In order to be part of an intrusive collection, a type must contain a
Links
type that stores the pointers to other nodes in that collection. For
example, to be part of a doubly-linked list, a type must contain the
list::Links
struct, or to be part of a [MPSC queue], a type must contain
the mpsc_queue::Links
struct.
Safety
This is unsafe to implement because it’s the implementation’s responsibility to ensure that types implementing this trait are valid intrusive collection nodes. In particular:
- Implementations must ensure that implementorss are pinned in memory while they
are in an intrusive collection. While a given
Linked
type is in an intrusive data structure, it may not be deallocated or moved to a different memory location. - The type implementing this trait must not implement
Unpin
. - Additional safety requirements for individual methods on this trait are documented on those methods.
Failure to uphold these invariants will result in corruption of the intrusive data structure, including dangling pointers.
Required Associated Types
Required Methods
Convert a Self::Handle
to a raw pointer to Self
, taking ownership
of it in the process.
Convert a raw pointer to Self
into an owning Self::Handle
.
Safety
This function is safe to call when:
- It is valid to construct a
Self::Handle
from a`raw pointer - The pointer points to a valid instance of
Self
(e.g. it does not dangle).