1 | use std::mem; |
2 | |
3 | use crate::{sys, Status}; |
4 | |
5 | /// Notice |
6 | /// The hook will be removed if `AsyncCleanupHook` was `dropped`. |
7 | /// If you want keep the hook until node process exited, call the `AsyncCleanupHook::forget`. |
8 | #[repr (transparent)] |
9 | pub struct AsyncCleanupHook(pub(crate) sys::napi_async_cleanup_hook_handle); |
10 | |
11 | impl AsyncCleanupHook { |
12 | /// Safe to forget it. |
13 | /// Things will be cleanup before process exited. |
14 | pub fn forget(self) { |
15 | mem::forget(self); |
16 | } |
17 | } |
18 | |
19 | impl Drop for AsyncCleanupHook { |
20 | fn drop(&mut self) { |
21 | let status: i32 = unsafe { sys::napi_remove_async_cleanup_hook(self.0) }; |
22 | assert!( |
23 | status == sys::Status::napi_ok, |
24 | "Delete async cleanup hook failed: {}" , |
25 | Status::from(status) |
26 | ); |
27 | } |
28 | } |
29 | |