#drop #destructure #derive #e0509 #remove-trait-impls

macro no-std derive_destructure2

Destructure structs that implement Drop

6 releases

0.1.3 Mar 17, 2024
0.1.2 Aug 12, 2023
0.1.1 Oct 31, 2022
0.1.0 Feb 21, 2022
0.0.2 Dec 27, 2021

#317 in Rust patterns

Download history 10149/week @ 2024-06-16 15599/week @ 2024-06-23 7645/week @ 2024-06-30 12546/week @ 2024-07-07 13460/week @ 2024-07-14 14721/week @ 2024-07-21 13816/week @ 2024-07-28 15957/week @ 2024-08-04 18026/week @ 2024-08-11 20861/week @ 2024-08-18 19905/week @ 2024-08-25 18613/week @ 2024-09-01 20917/week @ 2024-09-08 19101/week @ 2024-09-15 22763/week @ 2024-09-22 21828/week @ 2024-09-29

85,662 downloads per month
Used in 20 crates (5 directly)

MIT/Apache

21KB
264 lines

derive_destructure2 (no-std)

Rust

crate.io downloads

crate.io version

docs

examples

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::{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 (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.

Note: in Rust, a tuple of 1 element is denoted as (x,), not (x).

destructure is implemented as a private 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 private associated function.

Example:

use derive_destructure2::*;

#[derive(destructure, remove_trait_impls)]
struct ImplementsDrop {
    some_str: String,
    some_int: i32
}

impl Drop for ImplementsDrop {
    fn drop(&mut self) {
        panic!("We don't want to drop this");
    }
}

fn main() {
    // Using destructure():
    let x = ImplementsDrop {
        some_str: "foo".to_owned(),
        some_int: 4
    };
    let (some_str, some_int) = x.destructure();
    // x's drop() method never gets called

    // Using remove_trait_impls():
    let x = ImplementsDrop {
        some_str: "foo".to_owned(),
        some_int: 4
    };
    let x = x.remove_trait_impls();
    // this x doesn't implement drop,
    // so we can move fields out of it
    drop(x.some_str);
    println!("{}", x.some_int);
}

License

Licensed under either of

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.

Dependencies

~215–640KB
~15K SLoC