hyperlight_common/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 alloc::string::String;
18use alloc::vec::Vec;
19
20use super::error::Error;
21use crate::flatbuffer_wrappers::function_types::{ReturnType, ReturnValue};
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, 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
74/// A trait to handle either a [`SupportedReturnType`] or a [`Result<impl SupportedReturnType>`]
75pub trait ResultType<E: core::fmt::Debug> {
76    /// The return type of the supported return value
77    type ReturnType: SupportedReturnType;
78
79    /// Convert the return type into a `Result<impl SupportedReturnType>`
80    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);