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
use std::ptr::NonNull;

use crate::{api, ffi, map::Map};

#[derive(PartialEq, Eq, Hash, Debug)]
#[repr(transparent)]
pub struct Function {
    handle: NonNull<ffi::VSFunction>,
}

impl Function {
    pub(crate) unsafe fn from_ptr(ptr: *mut ffi::VSFunction) -> Self {
        Self {
            handle: NonNull::new_unchecked(ptr),
        }
    }

    #[must_use]
    pub fn as_ptr(&self) -> *mut ffi::VSFunction {
        self.handle.as_ptr()
    }

    pub fn call(&mut self, in_: &Map, out: &mut Map) {
        unsafe {
            (api().callFunction)(self.handle.as_ptr(), in_.as_ptr(), out.as_mut_ptr());
        }
    }
}

impl Drop for Function {
    fn drop(&mut self) {
        unsafe { (api().freeFunction)(self.as_ptr()) }
    }
}

impl Clone for Function {
    fn clone(&self) -> Self {
        unsafe { Self::from_ptr((api().addFunctionRef)(self.as_ptr())) }
    }
}