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
#![doc = include_str!("../README.md")]

use std::fmt::{Debug, Formatter};

pub use crate::handle::TokioEnterGuard;

mod handle;
fn get_data() -> u64 {
    get_features as *const () as u64
}

fn get_features() -> u16 {
    0
}

#[derive(Clone)]
#[repr(C)]
pub struct SharedTokioHandle {
    data: u64,
    features: u16,
    handle: ::tokio::runtime::Handle,
}
impl Debug for SharedTokioHandle {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        let mut dbg = f.debug_struct("SharedTokioHandle");

        dbg.field("data", &format_args!("{:p}", self.data as *const ()))
            .field("features", &self.features);
        dbg.finish()
    }
}
impl SharedTokioHandle {
    pub fn new() -> Self {
        SharedTokioHandle {
            data: get_data(),
            features: get_features(),
            handle: tokio::runtime::Handle::current(),
        }
    }

    #[inline(never)]
    pub fn enter(&self) -> TokioEnterGuard {
        TokioEnterGuard::new(self.handle.enter())
    }
}

#[no_mangle]
#[inline(never)]
pub fn setup_shared_tokio_ref(handle: &SharedTokioHandle) -> TokioEnterGuard {
    handle.enter()
}