async_destruction

Crates.ioasync_destruction
lib.rsasync_destruction
version0.1.1
sourcesrc
created_at2021-03-12 11:11:37.949284
updated_at2021-03-12 11:28:15.498934
descriptionA smart pointer which executes drop asynchronously in tokio.
homepagehttps://github.com/liangyongrui/async_destruction
repositoryhttps://github.com/liangyongrui/async_destruction
max_upload_size
id367676
size19,755
Liang Yongrui (liangyongrui)

documentation

README

Async Destruction

A smart pointer which executes drop asynchronously in tokio.

Crates.io version Download docs.rs docs ci

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

Commit count: 6

cargo fmt