metacall/
init.rs

1use crate::{
2    bindings::{metacall_destroy, metacall_initialize, metacall_is_initialized},
3    types::MetaCallInitError,
4};
5use std::ptr;
6
7pub struct MetaCallDestroy(unsafe extern "C" fn());
8
9impl Drop for MetaCallDestroy {
10    fn drop(&mut self) {
11        unsafe { self.0() }
12    }
13}
14
15/// Initializes MetaCall. Always remember to store the output in a variable to avoid instant drop.
16/// For example: ...
17/// ```
18/// // Initialize metacall at the top of your main function before loading your codes or
19/// // calling any function.
20/// let _metacall = metacall::initialize().unwrap();
21///
22///
23/// ```
24pub fn initialize() -> Result<MetaCallDestroy, MetaCallInitError> {
25    let code = unsafe { metacall_initialize() };
26
27    if code != 0 {
28        return Err(MetaCallInitError::new(code));
29    }
30
31    Ok(MetaCallDestroy(metacall_destroy))
32}
33
34pub fn is_initialized() -> bool {
35    let initialized = unsafe { metacall_is_initialized(ptr::null_mut()) };
36
37    if initialized == 0 {
38        return true;
39    }
40
41    false
42}