Amanieu / intrusive-rs

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

splice with mutable reference #43

Closed jynnantonix closed 4 years ago

jynnantonix commented 4 years ago

It would be nice to have a way to splice a LinkedList into another LinkedList via mutable reference. The use case is that I have 2 structs, each of which contain a LinkedList, and I would like to transfer all the elements from one LinkedList into the other.

Unfortunately, since both splice_before and splice_after take the other list by value and not by mutable reference, I have to work around it with mem::replace:

struct Foo {
    list: LinkedList<Adapter>,
    other_field: u64,
}

fn transfer(from: &mut Foo, to: &mut Foo) {
    to.list.cursor_mut().splice_before(mem::replace(
        &mut from.list,
        LinkedList::new(Adapter::new()),
    ));
}

This feels a bit hacky and it would be nice if CursorMut provided splice_* methods that took the other list by mutable reference.

Amanieu commented 4 years ago

You can use the take method on LinkedList:

fn transfer(from: &mut Foo, to: &mut Foo) {
    to.list.cursor_mut().splice_before(from.list.take());
}