tesseract_swift_utils/
response.rs

1use std::mem::ManuallyDrop;
2use std::result::Result;
3use super::error::CError;
4
5#[repr(C)]
6#[derive(Copy, Clone, Debug)]
7pub enum COptionResponseResult {
8    Error = 0,
9    None,
10    Some,
11}
12
13pub trait CVoidResponse {
14    fn response(self, error: &mut ManuallyDrop<CError>) -> bool;
15}
16
17pub trait CCopyResponse<T: Copy, R> {
18    fn response(self, value: &mut T, error: &mut ManuallyDrop<CError>) -> R;
19}
20
21pub trait CMoveResponse<T, R> {
22    fn response(self, value: &mut ManuallyDrop<T>, error: &mut ManuallyDrop<CError>) -> R;
23}
24
25impl CVoidResponse for Result<(), CError> {
26    fn response(self, error: &mut ManuallyDrop<CError>) -> bool {
27        match self {
28            Err(err) => {
29                *error = ManuallyDrop::new(err);
30                false
31            }
32            Ok(_) => true
33        }
34    }
35}
36
37impl<T: Copy> CCopyResponse<T, bool> for Result<T, CError> {
38    fn response(self, value: &mut T, error: &mut ManuallyDrop<CError>) -> bool {
39        match self {
40            Err(err) => {
41                *error = ManuallyDrop::new(err);
42                false
43            }
44            Ok(val) => {
45                *value = val;
46                true
47            }
48        }
49    }
50}
51
52impl<T, IT> CMoveResponse<T, bool> for Result<IT, CError> where IT: Into<T> {
53    fn response(self, value: &mut ManuallyDrop<T>, error: &mut ManuallyDrop<CError>) -> bool {
54        match self {
55            Err(err) => {
56                *error = ManuallyDrop::new(err);
57                false
58            }
59            Ok(val) => {
60                *value = ManuallyDrop::new(val.into());
61                true
62            }
63        }
64    }
65}
66
67impl<T: Copy> CCopyResponse<T, COptionResponseResult> for Result<Option<T>, CError> {
68    fn response(self, value: &mut T, error: &mut ManuallyDrop<CError>) -> COptionResponseResult {
69        match self {
70            Err(err) => {
71                *error = ManuallyDrop::new(err);
72                COptionResponseResult::Error
73            }
74            Ok(opt) => match opt {
75                None => COptionResponseResult::None,
76                Some(val) => {
77                    *value = val;
78                    COptionResponseResult::Some
79                }
80            },
81        }
82    }
83}
84
85impl<T, IT> CMoveResponse<T, COptionResponseResult> for Result<Option<IT>, CError>
86where
87   IT: Into<T>,
88{
89    fn response(
90        self,
91        value: &mut ManuallyDrop<T>,
92        error: &mut ManuallyDrop<CError>,
93    ) -> COptionResponseResult {
94        match self {
95            Err(err) => {
96                *error = ManuallyDrop::new(err);
97                COptionResponseResult::Error
98            }
99            Ok(opt) => match opt {
100                None => COptionResponseResult::None,
101                Some(val) => {
102                    *value = ManuallyDrop::new(val.into());
103                    COptionResponseResult::Some
104                }
105            },
106        }
107    }
108}