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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
use std::ffi::{CStr, CString, NulError};
use std::marker::PhantomData;
use std::ops::Deref;
use std::ptr::NonNull;
use vapoursynth_sys as ffi;
use api::API;
use map::{Map, OwnedMap};
use plugins::{self, FilterFunction};
#[derive(Debug, Clone, Copy)]
pub struct Plugin<'core> {
handle: NonNull<ffi::VSPlugin>,
_owner: PhantomData<&'core ()>,
}
unsafe impl<'core> Send for Plugin<'core> {}
unsafe impl<'core> Sync for Plugin<'core> {}
impl<'core> Plugin<'core> {
#[inline]
pub(crate) unsafe fn from_ptr(handle: *mut ffi::VSPlugin) -> Self {
Self {
handle: NonNull::new_unchecked(handle),
_owner: PhantomData,
}
}
#[inline]
pub fn functions(&self) -> OwnedMap<'core> {
unsafe { OwnedMap::from_ptr(API::get_cached().get_functions(self.handle.as_ptr())) }
}
#[cfg(feature = "gte-vapoursynth-api-31")]
#[inline]
pub fn path(&self) -> Option<&'core CStr> {
let ptr = unsafe { API::get_cached().get_plugin_path(self.handle.as_ptr()) };
if ptr.is_null() {
None
} else {
Some(unsafe { CStr::from_ptr(ptr) })
}
}
#[inline]
pub fn invoke(&self, name: &str, args: &Map<'core>) -> Result<OwnedMap<'core>, NulError> {
let name = CString::new(name)?;
Ok(unsafe {
OwnedMap::from_ptr(API::get_cached().invoke(
self.handle.as_ptr(),
name.as_ptr(),
args.deref(),
))
})
}
#[inline]
pub fn register_function<F: FilterFunction>(&self, filter_function: F) -> Result<(), NulError> {
let name_cstring = CString::new(filter_function.name())?;
let args_cstring = CString::new(filter_function.args())?;
let data = Box::new(plugins::ffi::FilterFunctionData::<F> {
filter_function,
name: name_cstring,
});
unsafe {
API::get_cached().register_function(
data.name.as_ptr(),
args_cstring.as_ptr(),
plugins::ffi::create::<F>,
Box::into_raw(data) as _,
self.handle.as_ptr(),
);
}
Ok(())
}
}