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§

source

fn entry_of(_: &T) -> &Entry

Returns a reference to this element’s Entry.

source

unsafe fn element_of(_: &Entry) -> &T

Given a reference to an element’s entry, returns that element.

let elem = ListElement::new();
assert_eq!(elem.entry_of(),
           unsafe { ListElement::element_of(elem.entry_of()) } );
Safety

The caller has to guarantee that the Entry is called with was retrieved from an instance of the element type (T).

source

unsafe fn finalize(_: &Entry, _: &Guard)

The function that is called when an entry is unlinked from list.

Safety

The caller has to guarantee that the Entry is called with was retrieved from an instance of the element type (T).

Implementors§