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