Crates.io | async_destruction |
lib.rs | async_destruction |
version | 0.1.1 |
source | src |
created_at | 2021-03-12 11:11:37.949284 |
updated_at | 2021-03-12 11:28:15.498934 |
description | A smart pointer which executes drop asynchronously in tokio. |
homepage | https://github.com/liangyongrui/async_destruction |
repository | https://github.com/liangyongrui/async_destruction |
max_upload_size | |
id | 367676 |
size | 19,755 |
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);
}
Licensed under either of
at your option.
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