pub(crate) trait IsElement<T> {
// Required methods
fn entry_of(_: &T) -> &Entry;
unsafe fn element_of(_: &Entry) -> &T;
unsafe fn finalize(_: &Entry, _: &Guard);
}Expand description
Implementing this trait asserts that the type T can be used as an element in the intrusive
linked list defined in this module. T has to contain (or otherwise be linked to) an instance
of Entry.
§Example
ⓘ
struct A {
entry: Entry,
data: usize,
}
impl IsElement<A> for A {
fn entry_of(a: &A) -> &Entry {
let entry_ptr = ((a as usize) + offset_of!(A, entry)) as *const Entry;
unsafe { &*entry_ptr }
}
unsafe fn element_of(entry: &Entry) -> &T {
let elem_ptr = ((entry as usize) - offset_of!(A, entry)) as *const T;
&*elem_ptr
}
unsafe fn finalize(entry: &Entry, guard: &Guard) {
guard.defer_destroy(Shared::from(Self::element_of(entry) as *const _));
}
}This trait is implemented on a type separate from T (although it can be just T), because
one type might be placeable into multiple lists, in which case it would require multiple
implementations of IsElement. In such cases, each struct implementing IsElement<T>
represents a distinct Entry in T.
For example, we can insert the following struct into two lists using entry1 for one
and entry2 for the other:
ⓘ
struct B {
entry1: Entry,
entry2: Entry,
data: usize,
}Required Methods§
Sourceunsafe fn element_of(_: &Entry) -> &T
unsafe fn element_of(_: &Entry) -> &T
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.