-
Notifications
You must be signed in to change notification settings - Fork 6
/
derive_tokio.rs
53 lines (44 loc) · 1.67 KB
/
derive_tokio.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
use std::result::Result;
use std::time::Duration;
use async_dropper::{AsyncDrop, AsyncDropError};
use async_trait::async_trait;
// NOTE: this example is rooted in crates/async-dropper
/// This object will be async-dropped
///
/// Objects that are dropped *must* implement [Default] and [PartialEq]
/// (so make members optional, hide them behind Rc/Arc as necessary)
#[derive(Debug, Default, PartialEq, Eq, AsyncDrop)]
struct AsyncThing {
value: String,
}
// async_dropper also works with tuple structs!
//
// #[derive(Debug, Default, PartialEq, Eq, AsyncDrop)]
// struct AsyncTupleThing(String);
/// Implementation of [AsyncDrop] that specifies the actual behavior
#[async_trait]
impl AsyncDrop for AsyncThing {
async fn async_drop(&mut self) -> Result<(), AsyncDropError> {
// Wait 2 seconds then "succeed"
eprintln!("async dropping [{:?}]!", self);
tokio::time::sleep(Duration::from_secs(2)).await;
eprintln!("dropped [{:?}]!", self);
Ok(())
}
fn drop_timeout(&self) -> Duration {
Duration::from_secs(5) // extended from default 3 seconds, as an example
}
// NOTE: the method below is automatically derived for you, but you can override it
// make sure that the object is equal to T::default() by the end, otherwise it will panic!
// fn reset(&mut self) {
// self.value = String::default();
// }
// NOTE: below was not implemented since we want the default of DropFailAction::Continue
// fn drop_fail_action(&self) -> DropFailAction;
}
#[tokio::main]
#[allow(dead_code)]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
drop(AsyncThing { value: String::from("test")});
Ok(())
}