hyperlight_host/func/
ret_type.rs1use hyperlight_common::flatbuffer_wrappers::function_types::{ReturnType, ReturnValue};
18use tracing::{Span, instrument};
19
20use crate::HyperlightError::ReturnValueConversionFailure;
21use crate::{Result, log_then_return};
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>;
33}
34
35pub trait ResultType {
37 type ReturnType: SupportedReturnType;
39
40 fn into_result(self) -> Result<Self::ReturnType>;
42}
43
44macro_rules! for_each_return_type {
45 ($macro:ident) => {
46 $macro!((), Void);
47 $macro!(String, String);
48 $macro!(i32, Int);
49 $macro!(u32, UInt);
50 $macro!(i64, Long);
51 $macro!(u64, ULong);
52 $macro!(f32, Float);
53 $macro!(f64, Double);
54 $macro!(bool, Bool);
55 $macro!(Vec<u8>, VecBytes);
56 };
57}
58
59macro_rules! impl_supported_return_type {
60 ($type:ty, $enum:ident) => {
61 impl SupportedReturnType for $type {
62 const TYPE: ReturnType = ReturnType::$enum;
63
64 #[instrument(skip_all, parent = Span::current(), level= "Trace")]
65 fn into_value(self) -> ReturnValue {
66 ReturnValue::$enum(self)
67 }
68
69 #[instrument(err(Debug), skip_all, parent = Span::current(), level= "Trace")]
70 fn from_value(value: ReturnValue) -> Result<Self> {
71 match value {
72 ReturnValue::$enum(i) => Ok(i),
73 other => {
74 log_then_return!(ReturnValueConversionFailure(
75 other.clone(),
76 stringify!($type)
77 ));
78 }
79 }
80 }
81 }
82
83 impl ResultType for $type {
84 type ReturnType = $type;
85
86 #[instrument(err(Debug), skip_all, parent = Span::current(), level= "Trace")]
87 fn into_result(self) -> Result<Self::ReturnType> {
88 Ok(self)
89 }
90 }
91
92 impl ResultType for Result<$type> {
93 type ReturnType = $type;
94
95 #[instrument(err(Debug), skip_all, parent = Span::current(), level= "Trace")]
96 fn into_result(self) -> Result<Self::ReturnType> {
97 self
98 }
99 }
100 };
101}
102
103for_each_return_type!(impl_supported_return_type);