Not sure if this has been proposed before, but it would be nice to have a Take trait as defined below:
trait Take {
fn take(&mut self) -> Self;
}
This is inspired by the Option.take method, but to make it even more useful there could be an implementation for types which implement Default:
impl<D: Default> Take for D {
fn take(&mut self) -> Self {
mem::replace(self, Default::default())
}
}
My motivation was wanting a way to check and clear a boolean flag in one step, then I thought the idea was generic enough it could apply to any Default type.
A similar method also exists as Cell::take. Looking at the impls of Default in std, this looks like it will work pretty much everywhere and be quite convenient.
Downside is if default is expensive and you’re just trying to move out quickly. Then you’re wasting time computing defaults when you don’t actually need them. Optionand Cell don’t have this problem because the defaults are cheap.
EDIT: Actually, I don’t think &mut Cell is sensible to its purpose, so Cell::take has a different definition.