derive_destructure2 (no-std)
This crate allows you to destructure structs that implement Drop.
If you've ever struggled with error E0509
"cannot move out of type T, which implements the Drop trait"
then this crate may be for you.
To use this crate, put this in your lib.rs or main.rs for rust < 1.30:
#[macro_use]
extern crate derive_destructure2;
For rust >= 1.30, just import it as a regular item:
use derive_destructure2::{derive_destructure, remove_trait_impls};
Then you have 2 ways to use this crate:
Option 1: #[derive(destructure)]
If you mark a struct with #[derive(destructure)], then you can destructure it using
from your crate
let = 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.
Note: in Rust, a tuple of 1 element is denoted as (x,), not (x).
destructure is implemented as a pub(crate) associated function.
Option 2: #[derive(remove_trait_impls)]
If you mark your struct with #[derive(remove_trait_impls)], then you can do
from your crate
let my_struct = my_struct.remove_trait_impls;
The result is a struct with the same fields, but it implements no traits
(except automatically-implemented traits like Sync and Send).
In particular, it doesn't implement Drop, so you can move fields out of it.
The name of the resulting struct is the original name plus the suffix
WithoutTraitImpls.
For example, Foo becomes FooWithoutTraitImpls. But you usually don't need to write
out this name.
#[derive(remove_trait_impls)] works on enums too.
remove_trait_impls is a pub(crate) associated function.
Example:
use *;
License
Licensed under either of
- Apache License, Version 2.0, (LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or https://opensource.org/licenses/MIT)
at your option.
Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.