libuv_sys_lite/
lib.rs

1#![allow(non_upper_case_globals)]
2#![allow(non_camel_case_types)]
3#![allow(non_snake_case)]
4#![allow(ambiguous_glob_reexports)]
5
6// borrowed from https://github.com/neon-bindings/neon/tree/main/crates/neon/src/sys/bindings
7
8#[cfg(feature = "dyn-symbols")]
9macro_rules! generate {
10  (extern "C" {
11    $(fn $name:ident($($param:ident: $ptype:ty$(,)?)*)$( -> $rtype:ty)?;)+
12  }) => {
13    struct LibUv {
14      $(
15        $name: unsafe extern "C" fn(
16          $($param: $ptype,)*
17        )$( -> $rtype)*,
18      )*
19    }
20
21    #[inline(never)]
22    fn panic_load<T>() -> T {
23      panic!("Node-API symbol has not been loaded")
24    }
25
26    static mut LIBUV: LibUv = {
27      $(
28        unsafe extern "C" fn $name($(_: $ptype,)*)$( -> $rtype)* {
29          panic_load()
30        }
31      )*
32
33      LibUv {
34        $(
35          $name,
36        )*
37      }
38    };
39
40    #[allow(clippy::missing_safety_doc)]
41    pub unsafe fn load(
42      host: &libloading::Library,
43    ) -> Result<(), libloading::Error> {
44      LIBUV = LibUv {
45        $(
46          $name: {
47            let symbol: Result<libloading::Symbol<unsafe extern "C" fn ($(_: $ptype,)*)$( -> $rtype)*>, libloading::Error> = host.get(stringify!($name).as_bytes());
48            match symbol {
49              Ok(f) => *f,
50              #[cfg_attr(not(feature = "warn-missing"), allow(unused_variables))]
51              Err(e) => {
52                #[cfg(feature = "warn-missing")] {
53                  eprintln!("Load libuv [{}] from host failed: {}", stringify!($name), e);
54                }
55                LIBUV.$name
56              }
57            }
58          },
59        )*
60      };
61
62      Ok(())
63    }
64
65    $(
66      #[inline]
67      #[allow(clippy::missing_safety_doc)]
68      pub unsafe fn $name($($param: $ptype,)*)$( -> $rtype)* {
69        (LIBUV.$name)($($param,)*)
70      }
71    )*
72  };
73}
74
75#[cfg(not(feature = "dyn-symbols"))]
76macro_rules! generate {
77  (extern "C" {
78    $(fn $name:ident($($param:ident: $ptype:ty$(,)?)*)$( -> $rtype:ty)?;)+
79  }) => {
80    extern "C" {
81      $(
82        pub fn $name($($param: $ptype,)*)$( -> $rtype)*;
83      ) *
84    }
85  };
86}
87
88pub(crate) use generate;
89
90mod functions;
91mod types {
92  include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
93}
94//
95pub use functions::*;
96pub use types::*;
97
98/// Loads libuv symbols from host process.
99/// Must be called at least once before using any functions in bindings or
100/// they will panic.
101#[cfg(feature = "dyn-symbols")]
102#[allow(clippy::missing_safety_doc)]
103pub unsafe fn setup() -> libloading::Library {
104  match load_all() {
105    Err(err) => panic!("{}", err),
106    Ok(l) => l,
107  }
108}