Amanieu / intrusive-rs

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

Getting a mutable reference from CursorMut in LinkedList #89

Closed brianshih1 closed 1 year ago

brianshih1 commented 1 year ago

Hello I am trying to get a mutable reference to the last element in the linked list. Since there is no get_mut() on a CursorMut, I currently have to remove the final element of the LinkedList, modify it, then push it back onto the Linked list:

let mut last_fragment = self.frags.back_mut();
let mut last_fragment = last_fragment.remove().unwrap();
last_fragment.as_mut().append(src, len);
self.frags.push_back(last_fragment);

Is there a better way to do this? Thanks!

Amanieu commented 1 year ago

You can put your data in a Cell, RefCell or UnsafeCell so that you can modify it with only get.

brianshih1 commented 1 year ago

got it, makes sense!