Crates.io | tokio-async-drop |
lib.rs | tokio-async-drop |
version | 0.1.0 |
source | src |
created_at | 2023-07-02 13:47:56.645282 |
updated_at | 2023-07-02 13:47:56.645282 |
description | macro to enable async drop in a tokio multithreaded runtime |
homepage | |
repository | https://github.com/nappa85/tokio-async-drop |
max_upload_size | |
id | 906040 |
size | 4,157 |
This is a quick and dirty "hack" (not really an hack, since it's the desired behaviour of used components) to allow async drop in a tokio multithreaded runtime.
The same approach could potentially be used to allow async code in many other situations, e.g. inside a once_cell::sync::Lazy
static variable.
struct Foo<'a> {
inner: &'a mut u8,
}
impl<'a> Foo<'a> {
async fn bar(&mut self) {
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
*self.inner = 1;
}
}
impl<'a> Drop for Foo<'a> {
fn drop(&mut self) {
tokio_async_drop!({
self.bar().await;
});
}
}
#[tokio::main]
async fn main() {
let mut semaphore = 0;
let f = Foo {
inner: &mut semaphore,
};
drop(f);
assert_eq!(semaphore, 1);
}