hyperlight_host/func/
ret_type.rs

1/*
2Copyright 2024 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::{instrument, Span};
19
20use crate::HyperlightError::ReturnValueConversionFailure;
21use crate::{log_then_return, Result};
22
23/// This is a marker trait that is used to indicate that a type is a valid Hyperlight return type.
24pub trait SupportedReturnType<T> {
25    /// Gets the return type of the supported return value
26    fn get_hyperlight_type() -> ReturnType;
27
28    /// Gets the value of the supported return value
29    fn get_hyperlight_value(&self) -> ReturnValue;
30
31    /// Gets the inner value of the supported return type
32    fn get_inner(a: ReturnValue) -> Result<T>;
33}
34
35impl SupportedReturnType<()> for () {
36    #[instrument(skip_all, parent = Span::current(), level= "Trace")]
37    fn get_hyperlight_type() -> ReturnType {
38        ReturnType::Void
39    }
40
41    #[instrument(skip_all, parent = Span::current(), level= "Trace")]
42    fn get_hyperlight_value(&self) -> ReturnValue {
43        ReturnValue::Void
44    }
45
46    #[instrument(err(Debug), skip_all, parent = Span::current(), level= "Trace")]
47    fn get_inner(a: ReturnValue) -> Result<()> {
48        match a {
49            ReturnValue::Void => Ok(()),
50            other => {
51                log_then_return!(ReturnValueConversionFailure(other.clone(), "()"));
52            }
53        }
54    }
55}
56
57impl SupportedReturnType<String> for String {
58    #[instrument(skip_all, parent = Span::current(), level= "Trace")]
59    fn get_hyperlight_type() -> ReturnType {
60        ReturnType::String
61    }
62
63    #[instrument(skip_all, parent = Span::current(), level= "Trace")]
64    fn get_hyperlight_value(&self) -> ReturnValue {
65        ReturnValue::String(self.clone())
66    }
67
68    #[instrument(err(Debug), skip_all, parent = Span::current(), level= "Trace")]
69    fn get_inner(a: ReturnValue) -> Result<String> {
70        match a {
71            ReturnValue::String(i) => Ok(i),
72            other => {
73                log_then_return!(ReturnValueConversionFailure(other.clone(), "String"));
74            }
75        }
76    }
77}
78
79impl SupportedReturnType<i32> for i32 {
80    #[instrument(skip_all, parent = Span::current(), level= "Trace")]
81    fn get_hyperlight_type() -> ReturnType {
82        ReturnType::Int
83    }
84
85    #[instrument(skip_all, parent = Span::current(), level= "Trace")]
86    fn get_hyperlight_value(&self) -> ReturnValue {
87        ReturnValue::Int(*self)
88    }
89
90    #[instrument(err(Debug), skip_all, parent = Span::current(), level= "Trace")]
91    fn get_inner(a: ReturnValue) -> Result<i32> {
92        match a {
93            ReturnValue::Int(i) => Ok(i),
94            other => {
95                log_then_return!(ReturnValueConversionFailure(other.clone(), "i32"));
96            }
97        }
98    }
99}
100
101impl SupportedReturnType<u32> for u32 {
102    #[instrument(skip_all, parent = Span::current(), level= "Trace")]
103    fn get_hyperlight_type() -> ReturnType {
104        ReturnType::UInt
105    }
106
107    #[instrument(skip_all, parent = Span::current(), level= "Trace")]
108    fn get_hyperlight_value(&self) -> ReturnValue {
109        ReturnValue::UInt(*self)
110    }
111
112    #[instrument(err(Debug), skip_all, parent = Span::current(), level= "Trace")]
113    fn get_inner(a: ReturnValue) -> Result<u32> {
114        match a {
115            ReturnValue::UInt(u) => Ok(u),
116            other => {
117                log_then_return!(ReturnValueConversionFailure(other.clone(), "u32"));
118            }
119        }
120    }
121}
122
123impl SupportedReturnType<i64> for i64 {
124    #[instrument(skip_all, parent = Span::current(), level= "Trace")]
125    fn get_hyperlight_type() -> ReturnType {
126        ReturnType::Long
127    }
128
129    #[instrument(skip_all, parent = Span::current(), level= "Trace")]
130    fn get_hyperlight_value(&self) -> ReturnValue {
131        ReturnValue::Long(*self)
132    }
133
134    #[instrument(err(Debug), skip_all, parent = Span::current(), level= "Trace")]
135    fn get_inner(a: ReturnValue) -> Result<i64> {
136        match a {
137            ReturnValue::Long(l) => Ok(l),
138            other => {
139                log_then_return!(ReturnValueConversionFailure(other.clone(), "i64"));
140            }
141        }
142    }
143}
144
145impl SupportedReturnType<u64> for u64 {
146    #[instrument(skip_all, parent = Span::current(), level= "Trace")]
147    fn get_hyperlight_type() -> ReturnType {
148        ReturnType::ULong
149    }
150
151    #[instrument(skip_all, parent = Span::current(), level= "Trace")]
152    fn get_hyperlight_value(&self) -> ReturnValue {
153        ReturnValue::ULong(*self)
154    }
155
156    #[instrument(err(Debug), skip_all, parent = Span::current(), level= "Trace")]
157    fn get_inner(a: ReturnValue) -> Result<u64> {
158        match a {
159            ReturnValue::ULong(ul) => Ok(ul),
160            other => {
161                log_then_return!(ReturnValueConversionFailure(other.clone(), "u64"));
162            }
163        }
164    }
165}
166
167impl SupportedReturnType<bool> for bool {
168    #[instrument(skip_all, parent = Span::current(), level= "Trace")]
169    fn get_hyperlight_type() -> ReturnType {
170        ReturnType::Bool
171    }
172
173    #[instrument(skip_all, parent = Span::current(), level= "Trace")]
174    fn get_hyperlight_value(&self) -> ReturnValue {
175        ReturnValue::Bool(*self)
176    }
177
178    #[instrument(err(Debug), skip_all, parent = Span::current(), level= "Trace")]
179    fn get_inner(a: ReturnValue) -> Result<bool> {
180        match a {
181            ReturnValue::Bool(i) => Ok(i),
182            other => {
183                log_then_return!(ReturnValueConversionFailure(other.clone(), "bool"));
184            }
185        }
186    }
187}
188
189impl SupportedReturnType<Vec<u8>> for Vec<u8> {
190    #[instrument(skip_all, parent = Span::current(), level= "Trace")]
191    fn get_hyperlight_type() -> ReturnType {
192        ReturnType::VecBytes
193    }
194
195    #[instrument(skip_all, parent = Span::current(), level= "Trace")]
196    fn get_hyperlight_value(&self) -> ReturnValue {
197        ReturnValue::VecBytes(self.clone())
198    }
199
200    #[instrument(err(Debug), skip_all, parent = Span::current(), level= "Trace")]
201    fn get_inner(a: ReturnValue) -> Result<Vec<u8>> {
202        match a {
203            ReturnValue::VecBytes(i) => Ok(i),
204            other => {
205                log_then_return!(ReturnValueConversionFailure(other.clone(), "Vec<u8>"));
206            }
207        }
208    }
209}