|
22 | 22 | //! pointers and cannot implement `IStable`. These simple `#[repr(C)]` types |
23 | 23 | //! provide the same FFI-safe semantics without that constraint. |
24 | 24 |
|
| 25 | +use stabby::string::String as SString; |
| 26 | + |
25 | 27 | /// An FFI-safe option type. |
26 | 28 | #[repr(C, u8)] |
27 | 29 | #[derive(Debug, Clone)] |
@@ -68,65 +70,65 @@ impl<T> FFI_Option<T> { |
68 | 70 | } |
69 | 71 | } |
70 | 72 |
|
71 | | -/// An FFI-safe result type. |
| 73 | +/// An FFI-safe result type with SString as the error type. |
72 | 74 | #[repr(C, u8)] |
73 | 75 | #[derive(Debug, Clone)] |
74 | | -pub enum FFI_Result<T, E> { |
| 76 | +pub enum FFIResult<T> { |
75 | 77 | Ok(T), |
76 | | - Err(E), |
77 | | -} |
78 | | - |
79 | | -impl<T, E> From<Result<T, E>> for FFI_Result<T, E> { |
80 | | - fn from(res: Result<T, E>) -> Self { |
81 | | - match res { |
82 | | - Ok(v) => FFI_Result::Ok(v), |
83 | | - Err(e) => FFI_Result::Err(e), |
84 | | - } |
85 | | - } |
86 | | -} |
87 | | - |
88 | | -impl<T, E> From<FFI_Result<T, E>> for Result<T, E> { |
89 | | - fn from(res: FFI_Result<T, E>) -> Self { |
90 | | - match res { |
91 | | - FFI_Result::Ok(v) => Ok(v), |
92 | | - FFI_Result::Err(e) => Err(e), |
93 | | - } |
94 | | - } |
| 78 | + Err(SString), |
95 | 79 | } |
96 | 80 |
|
97 | | -impl<T, E> FFI_Result<T, E> { |
| 81 | +impl<T> FFIResult<T> { |
98 | 82 | pub fn is_ok(&self) -> bool { |
99 | | - matches!(self, FFI_Result::Ok(_)) |
| 83 | + matches!(self, FFIResult::Ok(_)) |
100 | 84 | } |
101 | 85 |
|
102 | 86 | pub fn is_err(&self) -> bool { |
103 | | - matches!(self, FFI_Result::Err(_)) |
| 87 | + matches!(self, FFIResult::Err(_)) |
104 | 88 | } |
105 | 89 |
|
106 | | - pub fn unwrap_err(self) -> E { |
| 90 | + pub fn unwrap_err(self) -> SString { |
107 | 91 | match self { |
108 | | - FFI_Result::Err(e) => e, |
109 | | - FFI_Result::Ok(_) => panic!("called unwrap_err on Ok"), |
| 92 | + FFIResult::Err(e) => e, |
| 93 | + FFIResult::Ok(_) => panic!("called unwrap_err on Ok"), |
110 | 94 | } |
111 | 95 | } |
112 | 96 |
|
113 | | - pub fn map<U, F: FnOnce(T) -> U>(self, f: F) -> FFI_Result<U, E> { |
| 97 | + pub fn map<U, F: FnOnce(T) -> U>(self, f: F) -> FFIResult<U> { |
114 | 98 | match self { |
115 | | - FFI_Result::Ok(v) => FFI_Result::Ok(f(v)), |
116 | | - FFI_Result::Err(e) => FFI_Result::Err(e), |
| 99 | + FFIResult::Ok(v) => FFIResult::Ok(f(v)), |
| 100 | + FFIResult::Err(e) => FFIResult::Err(e), |
117 | 101 | } |
118 | 102 | } |
119 | 103 |
|
120 | | - pub fn into_result(self) -> Result<T, E> { |
| 104 | + pub fn into_result(self) -> Result<T, SString> { |
121 | 105 | self.into() |
122 | 106 | } |
123 | 107 | } |
124 | 108 |
|
125 | | -impl<T: PartialEq, E: PartialEq> PartialEq for FFI_Result<T, E> { |
| 109 | +impl<T> From<FFIResult<T>> for Result<T, SString> { |
| 110 | + fn from(res: FFIResult<T>) -> Self { |
| 111 | + match res { |
| 112 | + FFIResult::Ok(v) => Ok(v), |
| 113 | + FFIResult::Err(e) => Err(e), |
| 114 | + } |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +impl<T, E: ToString> From<Result<T, E>> for FFIResult<T> { |
| 119 | + fn from(res: Result<T, E>) -> Self { |
| 120 | + match res { |
| 121 | + Ok(v) => FFIResult::Ok(v), |
| 122 | + Err(e) => FFIResult::Err(SString::from(e.to_string().as_str())), |
| 123 | + } |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +impl<T: PartialEq> PartialEq for FFIResult<T> { |
126 | 128 | fn eq(&self, other: &Self) -> bool { |
127 | 129 | match (self, other) { |
128 | | - (FFI_Result::Ok(a), FFI_Result::Ok(b)) => a == b, |
129 | | - (FFI_Result::Err(a), FFI_Result::Err(b)) => a == b, |
| 130 | + (FFIResult::Ok(a), FFIResult::Ok(b)) => a == b, |
| 131 | + (FFIResult::Err(a), FFIResult::Err(b)) => a == b, |
130 | 132 | _ => false, |
131 | 133 | } |
132 | 134 | } |
|
0 commit comments