Amanieu / intrusive-rs

Intrusive collections for Rust
Apache License 2.0
400 stars 47 forks source link

DynLinkedList #91

Open conradludgate opened 4 months ago

conradludgate commented 4 months ago

I wanted to have a linked list of Arc<dyn DynTask> for a toy async runtime I am working on. It took a bit of effort but I did eventually manage to define an adaptor that works. I thought I would share it here for lack of any other suitable discussion place.

The basis of this impl is the FatLink, which is a linked list link + a type-erased get_value function that the Adapter impl uses.

pub(crate) struct FatLink<D: ?Sized> {
    link: XorLinkedListAtomicLink,
    get_value: unsafe fn(link: NonNull<FatLink<D>>) -> *const D,
}

Unfortunately, I needed to add this get_link method to the DynTask trait, which requires feature(arbitrary_self_types). I could not figure out a way to avoid the nightly dep here.

    unsafe fn get_link(self: *const Self) -> NonNull<FatLink<dyn DynTask>> {
        <Self as FatLinked<dyn DynTask>>::get_link(self)
    }

https://github.com/conradludgate/arc-discharge/blob/dbd9b0aeb0d1389262a203eeff3bc2e9b0c91a37/src/linked_list.rs