Amanieu / intrusive-rs

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

Suggestion: Alternate design without separate cursors #53

Open Diggsey opened 3 years ago

Diggsey commented 3 years ago

A common reason to use intrusive data-structures is to be able to look up an element via one data-structure, and then perform some operation on the same element in a different data-structure.

AIUI, this cannot be done safely with this crate, as it requires the unsafe cursor_mut_from_ptr method to be called.

I think a safe interface could be made by having this crate own the allocator that produces elements. For example, a basic allocator could store elements in a Vec<T>, and then use Vec indices as the "pointers" used to identify elements. Safety would be ensured by checking indices against the length of the internal Vec.

This would not prevent logic errors (if you mixed elements from different allocators, you'd get garbage results) but it should all be safe.

An arena allocator could work similarly, but using real pointers and range checks instead.

Amanieu commented 3 years ago

I think this is probably outside the scope of intrusive-collections and should be done in a separate crate.