hyperlight_common/func/
ret_type.rs1use alloc::string::String;
18use alloc::vec::Vec;
19
20use super::error::Error;
21use crate::flatbuffer_wrappers::function_types::{ReturnType, ReturnValue};
22
23pub trait SupportedReturnType: Sized + Clone + Send + Sync + 'static {
25 const TYPE: ReturnType;
27
28 fn into_value(self) -> ReturnValue;
30
31 fn from_value(value: ReturnValue) -> Result<Self, Error>;
33}
34
35#[macro_export]
36#[doc(hidden)]
37macro_rules! for_each_return_type {
38 ($macro:ident) => {
39 $macro!((), Void);
40 $macro!(String, String);
41 $macro!(i32, Int);
42 $macro!(u32, UInt);
43 $macro!(i64, Long);
44 $macro!(u64, ULong);
45 $macro!(f32, Float);
46 $macro!(f64, Double);
47 $macro!(bool, Bool);
48 $macro!(Vec<u8>, VecBytes);
49 };
50}
51
52macro_rules! impl_supported_return_type {
53 ($type:ty, $enum:ident) => {
54 impl SupportedReturnType for $type {
55 const TYPE: ReturnType = ReturnType::$enum;
56
57 fn into_value(self) -> ReturnValue {
58 ReturnValue::$enum(self)
59 }
60
61 fn from_value(value: ReturnValue) -> Result<Self, Error> {
62 match value {
63 ReturnValue::$enum(i) => Ok(i),
64 other => Err(Error::ReturnValueConversionFailure(
65 other.clone(),
66 stringify!($type),
67 )),
68 }
69 }
70 }
71 };
72}
73
74pub trait ResultType<E: core::fmt::Debug> {
76 type ReturnType: SupportedReturnType;
78
79 fn into_result(self) -> Result<Self::ReturnType, E>;
81}
82
83impl<T, E> ResultType<E> for T
84where
85 T: SupportedReturnType,
86 E: core::fmt::Debug,
87{
88 type ReturnType = T;
89
90 fn into_result(self) -> Result<Self::ReturnType, E> {
91 Ok(self)
92 }
93}
94
95impl<T, E> ResultType<E> for Result<T, E>
96where
97 T: SupportedReturnType,
98 E: core::fmt::Debug,
99{
100 type ReturnType = T;
101
102 fn into_result(self) -> Result<Self::ReturnType, E> {
103 self
104 }
105}
106
107for_each_return_type!(impl_supported_return_type);