Is there already a roadmap for eager drop in Rust? I'd like to share a proof of concept put together with the help of LLM, to demonstrate that it's feasible and can be implemented without breaking changes. All it needs is fine grained application. Here's where the demo lives GitHub - fuji-184/Rust-ASAP-Drop · GitHub
With this demonstration code :
use asap_macro::asap;
#[derive(Clone)]
struct Tracked {
name: &'static str,
}
impl Tracked {
fn new(name: &'static str) -> Self {
println!("CREATE {name}");
Tracked { name }
}
fn use_it(&self) {
println!("USE {}", self.name);
}
}
impl Drop for Tracked {
fn drop(&mut self) {
println!("DROP {}", self.name);
}
}
fn borrow(a: &Tracked) {
println!("BORROW early 2 is borrowed by other function, the new 2nd");
}
fn moved(a: Tracked) {
println!("MOVE early 2 is moved to other function, the new 2nd");
}
fn main() {
let normal = Tracked::new("variable that uses scope based drop");
let early = asap!(Tracked::new("variable that uses asap drop"));
normal.use_it();
early.use_it();
drop(normal);
let early = asap!(Tracked::new("variable that uses asap drop, the new 2nd"));
let cloned = early.clone();
borrow(&cloned);
borrow(&early);
moved(early);
let early = asap!(Tracked::new("variable that uses asap drop, branching"));
if 1 == 12 {
borrow(&early);
} else {
moved(early);
}
println!("after branching");
}
Here is the result of eager drop :
CREATE variable that uses scope based drop
CREATE variable that uses asap drop
USE variable that uses scope based drop
USE variable that uses asap drop
DROP variable that uses asap drop
DROP variable that uses scope based drop
CREATE variable that uses asap drop, the new 2nd
BORROW early 2 is borrowed by other function, the new 2nd
DROP variable that uses asap drop, the new 2nd
BORROW early 2 is borrowed by other function, the new 2nd
MOVE early 2 is moved to other function, the new 2nd
DROP variable that uses asap drop, the new 2nd
CREATE variable that uses asap drop, branching
MOVE early 2 is moved to other function, the new 2nd
DROP variable that uses asap drop, branching
after branching
To see the difference, here is the output if it is scope based mode :
CREATE variable that uses scope based drop
CREATE variable that uses asap drop
USE variable that uses scope based drop
USE variable that uses asap drop
DROP variable that uses scope based drop
CREATE variable that uses asap drop, the new 2nd
BORROW early 2 is borrowed by other function, the new 2nd
BORROW early 2 is borrowed by other function, the new 2nd
MOVE early 2 is moved to other function, the new 2nd
DROP variable that uses asap drop, the new 2nd
CREATE variable that uses asap drop, branching
MOVE early 2 is moved to other function, the new 2nd
DROP variable that uses asap drop, branching
after branching
DROP variable that uses asap drop, the new 2nd
DROP variable that uses asap drop
As shown, there are no stale variables. Everything is cleaned up immediately after it is no longer needed, rather than waiting until the end of the scope for execution to complete. This therefore resolves the temporary memory leak problem, as there is no memory sitting idle