1use crate::ffi;
2use core::ffi::c_void;
3use core::ptr;
4
5pub struct Filter {
7 ptr: ffi::BNNSFilter,
8}
9
10unsafe impl Send for Filter {}
11unsafe impl Sync for Filter {}
12
13impl Drop for Filter {
14 fn drop(&mut self) {
15 if !self.ptr.is_null() {
16 unsafe { ffi::BNNSFilterDestroy(self.ptr) };
18 self.ptr = ptr::null_mut();
19 }
20 }
21}
22
23impl Filter {
24 #[must_use]
30 pub unsafe fn from_convolution(
31 layer_params: *const c_void,
32 filter_params: *const c_void,
33 ) -> Option<Self> {
34 let ptr = ffi::BNNSFilterCreateLayerConvolution(layer_params, filter_params);
35 if ptr.is_null() {
36 None
37 } else {
38 Some(Self { ptr })
39 }
40 }
41
42 #[must_use]
48 pub unsafe fn from_fully_connected(
49 layer_params: *const c_void,
50 filter_params: *const c_void,
51 ) -> Option<Self> {
52 let ptr = ffi::BNNSFilterCreateLayerFullyConnected(layer_params, filter_params);
53 if ptr.is_null() {
54 None
55 } else {
56 Some(Self { ptr })
57 }
58 }
59
60 #[must_use]
61 pub const fn as_ptr(&self) -> *mut c_void {
62 self.ptr
63 }
64
65 pub unsafe fn apply(&self, input: *const c_void, output: *mut c_void) -> i32 {
71 ffi::BNNSFilterApply(self.ptr, input, output)
72 }
73}