autd3capi_driver/
result.rs1use autd3::driver::error::AUTDDriverError;
2
3use crate::{
4 CapiResult, ConstPtr, Duration, SamplingConfigTag, SamplingConfigValue, SamplingConfigWrap,
5};
6
7#[macro_export]
8macro_rules! impl_result {
9 ($type:ty, $inner:ident) => {
10 impl<T, E> From<Result<T, E>> for $type
11 where
12 $inner: From<T>,
13 E: std::error::Error,
14 {
15 fn from(value: Result<T, E>) -> Self {
16 match value {
17 Ok(value) => Self {
18 result: value.into(),
19 err_len: 0,
20 err: ConstPtr(std::ptr::null_mut()),
21 },
22 Err(e) => {
23 let err = e.to_string();
24 Self {
25 result: $inner::NULL,
26 err_len: err.as_bytes().len() as u32 + 1,
27 err: ConstPtr(Box::into_raw(Box::new(err)) as _),
28 }
29 }
30 }
31 }
32 }
33 };
34}
35
36#[macro_export]
37macro_rules! validate_cstr {
38 ($path:expr, $type:tt, $retty:tt) => {
39 match unsafe { std::ffi::CStr::from_ptr($path).to_str() } {
40 Ok(v) => v,
41 Err(e) => {
42 let err = e.to_string();
43 return $retty {
44 result: $type::NULL,
45 err_len: err.as_bytes().len() as u32 + 1,
46 err: ConstPtr(Box::into_raw(Box::new(err)) as _),
47 };
48 }
49 }
50 };
51}
52
53#[derive(Clone, Copy, PartialEq, Eq, Debug)]
54#[repr(u8)]
55pub enum AUTDStatus {
56 AUTDTrue = 0,
57 AUTDFalse = 1,
58 AUTDErr = 2,
59}
60
61impl AUTDStatus {
62 pub const NULL: Self = Self::AUTDErr;
63}
64
65#[repr(C)]
66pub struct ResultStatus {
67 pub result: AUTDStatus,
68 pub err_len: u32,
69 pub err: ConstPtr,
70}
71
72impl From<()> for AUTDStatus {
73 fn from(_: ()) -> Self {
74 Self::AUTDTrue
75 }
76}
77
78impl From<AUTDDriverError> for AUTDStatus {
79 fn from(_: AUTDDriverError) -> Self {
80 Self::AUTDErr
81 }
82}
83
84impl_result!(ResultStatus, AUTDStatus);
85
86#[repr(C)]
87pub struct ResultSamplingConfig {
88 pub result: SamplingConfigWrap,
89 pub err_len: u32,
90 pub err: ConstPtr,
91}
92
93impl CapiResult for SamplingConfigWrap {
94 const NULL: Self = SamplingConfigWrap {
95 tag: SamplingConfigTag::Divide,
96 value: SamplingConfigValue { divide: 0 },
97 };
98}
99
100impl_result!(ResultSamplingConfig, SamplingConfigWrap);
101
102#[repr(C)]
103pub struct ResultU8 {
104 pub result: u8,
105 pub err_len: u32,
106 pub err: ConstPtr,
107}
108
109impl CapiResult for u8 {
110 const NULL: Self = 0;
111}
112
113impl_result!(ResultU8, u8);
114
115#[repr(C)]
116pub struct ResultU16 {
117 pub result: u16,
118 pub err_len: u32,
119 pub err: ConstPtr,
120}
121
122impl CapiResult for u16 {
123 const NULL: Self = 0;
124}
125
126impl_result!(ResultU16, u16);
127
128#[repr(C)]
129pub struct ResultF32 {
130 pub result: f32,
131 pub err_len: u32,
132 pub err: ConstPtr,
133}
134
135impl CapiResult for f32 {
136 const NULL: Self = 0.;
137}
138
139impl_result!(ResultF32, f32);
140
141#[repr(C)]
142pub struct ResultDuration {
143 pub result: Duration,
144 pub err_len: u32,
145 pub err: ConstPtr,
146}
147
148impl CapiResult for Duration {
149 const NULL: Self = Duration { nanos: 0 };
150}
151
152impl_result!(ResultDuration, Duration);