#drop #async #smart-pointers #execute #tokio #destruction

async_destruction

在tokio中执行异步释放的智能指针

2个版本

0.1.1 2021年3月12日
0.1.0 2021年3月12日

#49 in #drop

MIT/Apache

8KB
80 代码行数(不含注释)

Async Destruction

在tokio中执行异步释放的智能指针。


基本用法

依赖项

async_destruction = "0.1"
tokio = { version = '1', features = ["full"] }
# Only used in the current example
chrono = "0.4"

示例

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);
}

许可证

根据您的选择,许可协议为

贡献

除非您明确声明,否则您有意提交的任何贡献,按照Apache-2.0许可证的定义,应按照上述协议双授权,不附加任何额外的条款或条件

依赖项

~2–3MB
~46K SLoC