If you’ve ever struggled with error E0509 “Cannot move out of type
T, which implements the
Drop trait”, then you know how annoying that error can be to resolve. It often involves
ptr::read(), or other tricky unsafe code. This crate encapsulates that unsafe code so you don’t have to write it.
How to use it:
Put this in your main.rs or lib.rs
#[macro_use] extern crate derive_destructure;
Then mark a struct with
#[derive(destructure)]. When you want to move values out of that struct, you can simply write
let (field_1, field_2, ...) = my_struct.destructure();
This turns the struct into a tuple of its fields without running the struct’s drop() method. You can then happily move elements out of this tuple.
An alternative API that turns the struct into a different struct that doesn’t implement
Drop (instead of turning it into a tuple) is also available. Check the readme on Github for more info on that.