Skip to content

A smart pointer which executes drop asynchronously in tokio.

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT
Notifications You must be signed in to change notification settings

liangyongrui/async_destruction

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

A smart pointer which executes drop asynchronously in tokio.

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages