hyperlight_host/func/
ret_type.rs1use hyperlight_common::flatbuffer_wrappers::function_types::{ReturnType, ReturnValue};
18use tracing::{instrument, Span};
19
20use crate::HyperlightError::ReturnValueConversionFailure;
21use crate::{log_then_return, Result};
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!(bool, Bool);
53 $macro!(Vec<u8>, VecBytes);
54 };
55}
56
57macro_rules! impl_supported_return_type {
58 ($type:ty, $enum:ident) => {
59 impl SupportedReturnType for $type {
60 const TYPE: ReturnType = ReturnType::$enum;
61
62 #[instrument(skip_all, parent = Span::current(), level= "Trace")]
63 fn into_value(self) -> ReturnValue {
64 ReturnValue::$enum(self)
65 }
66
67 #[instrument(err(Debug), skip_all, parent = Span::current(), level= "Trace")]
68 fn from_value(value: ReturnValue) -> Result<Self> {
69 match value {
70 ReturnValue::$enum(i) => Ok(i),
71 other => {
72 log_then_return!(ReturnValueConversionFailure(
73 other.clone(),
74 stringify!($type)
75 ));
76 }
77 }
78 }
79 }
80
81 impl ResultType for $type {
82 type ReturnType = $type;
83
84 #[instrument(err(Debug), skip_all, parent = Span::current(), level= "Trace")]
85 fn into_result(self) -> Result<Self::ReturnType> {
86 Ok(self)
87 }
88 }
89
90 impl ResultType for Result<$type> {
91 type ReturnType = $type;
92
93 #[instrument(err(Debug), skip_all, parent = Span::current(), level= "Trace")]
94 fn into_result(self) -> Result<Self::ReturnType> {
95 self
96 }
97 }
98 };
99}
100
101for_each_return_type!(impl_supported_return_type);