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
use crate::{
    bindings::{metacall_destroy, metacall_initialize},
    types::MetacallInitError,
};
use std::ffi::c_int;

pub fn destroy_manually() -> c_int {
    unsafe { metacall_destroy() }
}
pub fn initialize_manually() -> c_int {
    unsafe { metacall_initialize() }
}

pub struct MetacallAutoDestroy(pub fn() -> c_int);
impl Drop for MetacallAutoDestroy {
    fn drop(&mut self) {
        self.0();
    }
}

/// Initializes Metacall. Always remember to store the output in a variable to avoid instant drop.
/// For example: ...
/// ```
/// // Initialize metacall at the top of your main function before loading your codes or
/// // calling any function.
/// let _metacall = metacall::initialize().unwrap();
///
///
/// ```
pub fn initialize() -> Result<MetacallAutoDestroy, MetacallInitError> {
    if initialize_manually() != 0 {
        return Err(MetacallInitError::new());
    }

    Ok(MetacallAutoDestroy(destroy_manually))
}