vapoursynth4_rs/plugin/
plugin_function.rs

1use std::{ffi::CStr, ptr::NonNull};
2
3use crate::{api::Api, ffi};
4
5use super::Plugin;
6
7#[derive(PartialEq, Eq, Hash, Debug)]
8pub struct PluginFunction {
9    handle: NonNull<ffi::VSPluginFunction>,
10    api: Api,
11}
12
13impl PluginFunction {
14    pub(crate) fn from_ptr(ptr: NonNull<ffi::VSPluginFunction>, api: Api) -> Self {
15        Self { handle: ptr, api }
16    }
17
18    #[must_use]
19    pub fn new(handle: NonNull<ffi::VSPluginFunction>, api: Api) -> Self {
20        Self { handle, api }
21    }
22
23    #[must_use]
24    pub fn as_ptr(&self) -> *const ffi::VSPluginFunction {
25        self.handle.as_ptr()
26    }
27
28    #[must_use]
29    pub fn name(&self) -> &CStr {
30        unsafe {
31            let ptr = (self.api.getPluginFunctionName)(self.as_ptr().cast_mut());
32            CStr::from_ptr(ptr)
33        }
34    }
35
36    #[must_use]
37    pub fn arguments(&self) -> &CStr {
38        unsafe {
39            let ptr = (self.api.getPluginFunctionArguments)(self.as_ptr().cast_mut());
40            CStr::from_ptr(ptr)
41        }
42    }
43
44    #[must_use]
45    pub fn return_type(&self) -> &CStr {
46        unsafe {
47            let ptr = (self.api.getPluginFunctionReturnType)(self.as_ptr().cast_mut());
48            CStr::from_ptr(ptr)
49        }
50    }
51}
52
53#[derive(Clone, PartialEq, Eq, Hash, Debug)]
54pub struct Functions<'p> {
55    cursor: *mut ffi::VSPluginFunction,
56    plugin: &'p Plugin,
57}
58
59impl<'p> Functions<'p> {
60    pub(crate) fn new(plugin: &'p Plugin) -> Functions<'p> {
61        Self {
62            cursor: std::ptr::null_mut(),
63            plugin,
64        }
65    }
66}
67
68impl Iterator for Functions<'_> {
69    type Item = PluginFunction;
70
71    fn next(&mut self) -> Option<Self::Item> {
72        unsafe {
73            let ptr = (self.plugin.api.getNextPluginFunction)(
74                self.cursor,
75                self.plugin.as_ptr().cast_mut(),
76            );
77            NonNull::new(ptr).map(|p| {
78                self.cursor = ptr;
79                PluginFunction::new(p, self.plugin.api)
80            })
81        }
82    }
83}