autd3capi_driver/ptr/
link.rs

1use autd3::core::link::Link;
2
3use crate::{ConstPtr, impl_result};
4
5#[repr(C)]
6pub struct LinkPtr(pub *const libc::c_void);
7
8impl<L: Link + 'static> From<L> for LinkPtr {
9    fn from(v: L) -> Self {
10        let v: Box<dyn Link> = Box::new(v);
11        Self(Box::into_raw(Box::new(v)) as _)
12    }
13}
14
15impl LinkPtr {
16    pub const NULL: Self = Self(std::ptr::null());
17
18    pub fn cast<T: Link>(&self) -> &T {
19        unsafe {
20            (self.0 as *const Box<dyn Link> as *const Box<T>)
21                .as_ref()
22                .unwrap()
23        }
24    }
25
26    pub fn cast_mut<T: Link>(&mut self) -> &mut T {
27        unsafe {
28            (self.0 as *mut Box<dyn Link> as *mut Box<T>)
29                .as_mut()
30                .unwrap()
31        }
32    }
33}
34
35#[repr(C)]
36pub struct ResultLink {
37    pub result: LinkPtr,
38    pub err_len: u32,
39    pub err: ConstPtr,
40}
41
42impl_result!(ResultLink, LinkPtr);