1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
//! # FFI Destruct
//! Generates destructors for structures that contain raw pointers in the FFI.
//!
//! ## Example
//! Provides a structure with several raw pointers that need to be dropped manually.
//! ```
//! use ffi_destruct::{extern_c_destructor, Destruct};
//! use std::ffi::*;
//!
//! #[derive(Destruct)]
//! pub struct MyStruct {
//! field: *mut std::ffi::c_char,
//! }
//!
//! pub struct AnyOther(u32, u32);
//!
//! // Struct definition here, with deriving Destruct and nullable attributes.
//! #[derive(Destruct)]
//! pub struct Structure {
//! // Default is non-null.
//! c_string: *const c_char,
//! #[nullable]
//! c_string_nullable: *mut c_char,
//!
//! other: *mut MyStruct,
//! #[nullable]
//! other_nullable: *mut MyStruct,
//!
//! // Do not drop this field.
//! #[no_drop]
//! not_dropped: *const AnyOther,
//!
//! // Raw pointer for any other things
//! any: *mut AnyOther,
//!
//! // Non-pointer types are still available, and will not be added to drop().
//! pub normal_int: u32,
//! pub normal_string: String,
//! }
//!
//! // (Optional) The macro here generates the destructor: destruct_structure()
//! extern_c_destructor!(Structure);
//!
//! fn test() {
//! // Some resources manually managed
//! let tmp = AnyOther(1, 1);
//! let tmp_ptr = Box::into_raw(Box::new(tmp));
//!
//! let my_struct = Structure {
//! c_string: CString::new("Hello").unwrap().into_raw(),
//! c_string_nullable: std::ptr::null_mut(),
//! other: Box::into_raw(Box::new(MyStruct {
//! field: CString::new("Hello").unwrap().into_raw(),
//! })),
//! other_nullable: std::ptr::null_mut(),
//! not_dropped: tmp_ptr,
//! any: Box::into_raw(Box::new(AnyOther(1, 1))),
//! normal_int: 114514,
//! normal_string: "Hello".to_string(),
//! };
//!
//! let my_struct_ptr = Box::into_raw(Box::new(my_struct));
//! // FFI calling
//! unsafe {
//! destruct_structure(my_struct_ptr);
//! }
//!
//! // Drop the manually managed resources
//! unsafe {
//! let _ = Box::from_raw(tmp_ptr);
//! }
//! }
//! ```
//!
//! After expanding the macros:
//! ```ignore
//! #[macro_use]
//! #![feature(prelude_import)]
//! #![allow(dead_code)]
//! #[prelude_import]
//! use std::prelude::rust_2021::*;
//! #[macro_use]
//! extern crate std;
//! use ffi_destruct::{extern_c_destructor, Destruct};
//! use std::ffi::*;
//! pub struct MyStruct {
//! field: *mut std::ffi::c_char,
//! }
//! impl ::std::ops::Drop for MyStruct {
//! fn drop(&mut self) {
//! unsafe {
//! let _ = ::std::ffi::CString::from_raw(self.field as *mut ::std::ffi::c_char);
//! }
//! }
//! }
//! pub struct AnyOther(u32, u32);
//! pub struct Structure {
//! c_string: *const c_char,
//! #[nullable]
//! c_string_nullable: *mut c_char,
//! other: *mut MyStruct,
//! #[nullable]
//! other_nullable: *mut MyStruct,
//! #[no_drop]
//! not_dropped: *const AnyOther,
//! any: *mut AnyOther,
//! pub normal_int: u32,
//! pub normal_string: String,
//! }
//! impl ::std::ops::Drop for Structure {
//! fn drop(&mut self) {
//! unsafe {
//! let _ = ::std::ffi::CString::from_raw(
//! self.c_string as *mut ::std::ffi::c_char,
//! );
//! if !self.c_string_nullable.is_null() {
//! let _ = ::std::ffi::CString::from_raw(
//! self.c_string_nullable as *mut ::std::ffi::c_char,
//! );
//! }
//! let _ = ::std::boxed::Box::from_raw(self.other as *mut MyStruct);
//! if !self.other_nullable.is_null() {
//! let _ = ::std::boxed::Box::from_raw(
//! self.other_nullable as *mut MyStruct,
//! );
//! }
//! let _ = ::std::boxed::Box::from_raw(self.any as *mut AnyOther);
//! }
//! }
//! }
//! #[no_mangle]
//! pub unsafe extern "C" fn destruct_structure(ptr: *mut Structure) {
//! if ptr.is_null() {
//! return;
//! }
//! let _ = ::std::boxed::Box::from_raw(ptr);
//! }
//! fn test() {
//! let tmp = AnyOther(1, 1);
//! let tmp_ptr = Box::into_raw(Box::new(tmp));
//! let my_struct = Structure {
//! c_string: CString::new("Hello").unwrap().into_raw(),
//! c_string_nullable: std::ptr::null_mut(),
//! other: Box::into_raw(
//! Box::new(MyStruct {
//! field: CString::new("Hello").unwrap().into_raw(),
//! }),
//! ),
//! other_nullable: std::ptr::null_mut(),
//! not_dropped: tmp_ptr,
//! any: Box::into_raw(Box::new(AnyOther(1, 1))),
//! normal_int: 114514,
//! normal_string: "Hello".to_string(),
//! };
//! let my_struct_ptr = Box::into_raw(Box::new(my_struct));
//! unsafe {
//! destruct_structure(my_struct_ptr);
//! }
//! unsafe {
//! let _ = Box::from_raw(tmp_ptr);
//! }
//! }
//! ```
use ;
use ;
use ;
use ;
/// The [`Destruct`] derive macro.
///
/// Generate a destructor for the structure.
///
/// ## Field Attributes
/// - `#[nullable]` - The field is nullable, the destructor will check if the pointer is null before
/// - `#[no_drop]` - The field will not be added to the destructor
/// Generate extern "C" destructor for provide type
///
/// Provide the function name: "destruct_" + snake_case name of the type.
///
/// ## Usage
///
/// ```
/// // Definition of struct here
/// # use ffi_destruct::{Destruct, extern_c_destructor};
/// #[derive(Destruct)]
/// pub struct MyStruct {
/// field: *mut std::ffi::c_char,
/// }
/// // destructor macro here
/// extern_c_destructor!(MyStruct);
/// ```
/// The macro will be expanded to:
/// ```
/// # use ffi_destruct::Destruct;
/// # #[derive(Destruct)]
/// # pub struct MyStruct {
/// # field: *mut std::ffi::c_char,
/// # }
/// #[no_mangle]
/// pub unsafe extern "C" fn destruct_my_struct(ptr: *mut MyStruct) {
/// if ptr.is_null() {
/// return;
/// }
/// let _ = ::std::boxed::Box::from_raw(ptr);
/// }
/// ```