destruct_drop/lib.rs
1#![warn(clippy::pedantic, clippy::cargo, unsafe_op_in_unsafe_fn)]
2#![allow(clippy::needless_doctest_main)]
3
4/*!
5Macro for dropping the fields of a struct or enum without dropping the container.
6
7## Usage
8Add `#[derive(DestructDrop)]` to your `struct` or `enum` definition.
9
10```
11use destruct_drop::DestructDrop;
12
13#[derive(DestructDrop)]
14struct Container {
15 inner: Inner
16}
17
18struct Inner;
19
20impl Drop for Container {
21 fn drop(&mut self) {
22 println!("dropped Container");
23 }
24}
25
26impl Drop for Inner {
27 fn drop(&mut self) {
28 println!("dropped Inner");
29 }
30}
31
32fn main() {
33 // prints "dropped Inner" and then "dropped Container"
34 drop(Container { inner: Inner });
35
36 // prints only "dropped Inner"
37 Container { inner: Inner }.destruct_drop();
38}
39```
40!*/
41
42pub use destruct_drop_derive::*;
43
44/// Trait for consuming a struct or enum without dropping it while dropping all of its contents normally.
45pub trait DestructDrop {
46 /// Consume self without dropping it while dropping all of its contents normally.
47 fn destruct_drop(self);
48}