hyperlight_host/func/
ret_type.rs

1/*
2Copyright 2025  The Hyperlight Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8    http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17use hyperlight_common::flatbuffer_wrappers::function_types::{ReturnType, ReturnValue};
18use tracing::{Span, instrument};
19
20use crate::HyperlightError::ReturnValueConversionFailure;
21use crate::{Result, log_then_return};
22
23/// This is a marker trait that is used to indicate that a type is a valid Hyperlight return type.
24pub trait SupportedReturnType: Sized + Clone + Send + Sync + 'static {
25    /// The return type of the supported return value
26    const TYPE: ReturnType;
27
28    /// Gets the value of the supported return value
29    fn into_value(self) -> ReturnValue;
30
31    /// Gets the inner value of the supported return type
32    fn from_value(value: ReturnValue) -> Result<Self>;
33}
34
35/// A trait to handle either a [`SupportedReturnType`] or a [`Result<impl SupportedReturnType>`]
36pub trait ResultType {
37    /// The return type of the supported return value
38    type ReturnType: SupportedReturnType;
39
40    /// Convert the return type into a `Result<impl SupportedReturnType>`
41    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);