SlotMap attach/reattach, but without moving?
⚓ Rust 📅 2026-07-02 👤 surdeus 👁️ 1A SlotMap has detach/reattach methods, to help with certain access patterns. With the SecondaryMapyou can just use remove/insert for a similar effect.
// &mut self
let x = self.things.remove(id).unwrap();
x.layout(self); // Now I can pass a &mut to things because x is not borrowed
self.things.insert(id, x);
If value removed is small, it doesn't matter, but if the value is bigger it might be nice to have a way to use the same idea, but without moving the object in memory. Would it be possible?
let x = self.things.detach_ref(id); // x is &Thing, or &mut Thing
x.do_stuff(self);
self.things.revive(id);
The slotmap would have to know that one of its values was off limits. Maybe something like this already exists. Maybe that is basically re-inventing RefCell....
11 posts - 6 participants
🏷️ Rust_feed