Expand description
Macro for ensuring critical code execution on function return or panics in Rust, making it easy to include essential code for reliable operation.
§Short example:
ⓘ
use drop_code::drop_code;
#[allow(unreachable_code)]
fn main() {
drop_code! {
println!("Code that must be executed in any situation."); // 3
}
println!("your code"); // 1
panic!("panic info"); // 2
}
§Full syntax example:
use drop_code::drop_code;
struct AlwaysDropLogic {}
impl AlwaysDropLogic {
fn drop_logic(&mut self) {
println!("#[] drop_logic!");
}
fn valid_logic(&mut self) {
println!("#[] valid_logic!");
}
}
fn main() {
let mut adl = AlwaysDropLogic {};
drop_code!(#[inline(always)]: (mut adl: AlwaysDropLogic) {
adl.drop_logic();
});
adl.valid_logic();
// out:
// #[] valid_logic!
// #[] drop_logic!
}
§Technical concept:
The drop_code macro generates the drop function code for the Drop trait in Rust, creating a hidden structure that encapsulates user-supplied arguments from the macro and ensuring their transfer. This mechanism guarantees the execution of critical operations when the object is destroyed, facilitating reliable handling of essential code within Rust applications, with the order of code execution dictated by Rust’s rules and conventions.
Macros§
- drop_
code - Macro for ensuring critical code execution on function return or panics in Rust, making it easy to include essential code for reliable operation.