2 releases
0.1.1 | Mar 12, 2021 |
---|---|
0.1.0 | Mar 12, 2021 |
#49 in #drop
8KB
80 lines
Async Destruction
A smart pointer which executes drop asynchronously in tokio.
Basic usage
dependencies
async_destruction = "0.1"
tokio = { version = '1', features = ["full"] }
# Only used in the current example
chrono = "0.4"
demo
use async_destruction::AsyncDestruction;
use chrono::Utc;
use std::{thread::sleep, time::Duration};
#[derive(Clone)]
struct S;
impl Drop for S {
fn drop(&mut self) {
sleep(Duration::from_millis(1));
println!("drop!");
}
}
impl S {
pub fn do_sth(&self) {
println!("do_sth");
}
}
#[test]
fn it_works() {
let a = vec![S; 10];
a[0].do_sth();
let t1 = Utc::now().timestamp_millis();
drop(a);
let t2 = Utc::now().timestamp_millis();
// will print 'drop cost time: 12ms'
println!("drop cost time: {}ms", t2 - t1);
}
#[tokio::test]
async fn async_works() {
let a = AsyncDestruction::new(vec![S; 10]);
// auto deref
a[0].do_sth();
let t1 = Utc::now().timestamp_millis();
drop(a);
let t2 = Utc::now().timestamp_millis();
// will print 'drop cost time: 0ms'
println!("drop cost time: {}ms", t2 - t1);
}
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
~2–3MB
~46K SLoC