hyperlight_host/func/
param_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::{ParameterType, ParameterValue};
18use tracing::{instrument, Span};
19
20use crate::HyperlightError::ParameterValueConversionFailure;
21use crate::{log_then_return, Result};
22
23/// This is a marker trait that is used to indicate that a type is a
24/// valid Hyperlight parameter type.
25///
26/// For each parameter type Hyperlight supports in host functions, we
27/// provide an implementation for `SupportedParameterType<SupportedType>`
28pub trait SupportedParameterType<T> {
29    /// Get the underlying Hyperlight parameter type representing this
30    /// `SupportedParameterType`
31    fn get_hyperlight_type() -> ParameterType;
32    /// Get the underling Hyperlight parameter value representing this
33    /// `SupportedParameterType`
34    fn get_hyperlight_value(&self) -> ParameterValue;
35    /// Get the actual inner value of this `SupportedParameterType`
36    fn get_inner(a: ParameterValue) -> Result<T>;
37}
38
39// We can then implement these traits for each type that Hyperlight supports as a parameter or return type
40impl SupportedParameterType<String> for String {
41    #[instrument(skip_all, parent = Span::current(), level= "Trace")]
42    fn get_hyperlight_type() -> ParameterType {
43        ParameterType::String
44    }
45
46    #[instrument(skip_all, parent = Span::current(), level= "Trace")]
47    fn get_hyperlight_value(&self) -> ParameterValue {
48        ParameterValue::String(self.clone())
49    }
50
51    #[instrument(err(Debug), skip_all, parent = Span::current(), level= "Trace")]
52    fn get_inner(a: ParameterValue) -> Result<String> {
53        match a {
54            ParameterValue::String(i) => Ok(i),
55            other => {
56                log_then_return!(ParameterValueConversionFailure(other.clone(), "String"));
57            }
58        }
59    }
60}
61
62impl SupportedParameterType<i32> for i32 {
63    #[instrument(skip_all, parent = Span::current(), level= "Trace")]
64    fn get_hyperlight_type() -> ParameterType {
65        ParameterType::Int
66    }
67
68    #[instrument(skip_all, parent = Span::current(), level= "Trace")]
69    fn get_hyperlight_value(&self) -> ParameterValue {
70        ParameterValue::Int(*self)
71    }
72
73    #[instrument(err(Debug), skip_all, parent = Span::current(), level= "Trace")]
74    fn get_inner(a: ParameterValue) -> Result<i32> {
75        match a {
76            ParameterValue::Int(i) => Ok(i),
77            other => {
78                log_then_return!(ParameterValueConversionFailure(other.clone(), "i32"));
79            }
80        }
81    }
82}
83
84impl SupportedParameterType<u32> for u32 {
85    #[instrument(skip_all, parent = Span::current(), level= "Trace")]
86    fn get_hyperlight_type() -> ParameterType {
87        ParameterType::UInt
88    }
89
90    #[instrument(skip_all, parent = Span::current(), level= "Trace")]
91    fn get_hyperlight_value(&self) -> ParameterValue {
92        ParameterValue::UInt(*self)
93    }
94
95    #[instrument(err(Debug), skip_all, parent = Span::current(), level= "Trace")]
96    fn get_inner(a: ParameterValue) -> Result<u32> {
97        match a {
98            ParameterValue::UInt(ui) => Ok(ui),
99            other => {
100                log_then_return!(ParameterValueConversionFailure(other.clone(), "u32"));
101            }
102        }
103    }
104}
105
106impl SupportedParameterType<i64> for i64 {
107    #[instrument(skip_all, parent = Span::current(), level= "Trace")]
108    fn get_hyperlight_type() -> ParameterType {
109        ParameterType::Long
110    }
111
112    #[instrument(skip_all, parent = Span::current(), level= "Trace")]
113    fn get_hyperlight_value(&self) -> ParameterValue {
114        ParameterValue::Long(*self)
115    }
116
117    #[instrument(err(Debug), skip_all, parent = Span::current(), level= "Trace")]
118    fn get_inner(a: ParameterValue) -> Result<i64> {
119        match a {
120            ParameterValue::Long(l) => Ok(l),
121            other => {
122                log_then_return!(ParameterValueConversionFailure(other.clone(), "i64"));
123            }
124        }
125    }
126}
127
128impl SupportedParameterType<u64> for u64 {
129    #[instrument(skip_all, parent = Span::current(), level= "Trace")]
130    fn get_hyperlight_type() -> ParameterType {
131        ParameterType::ULong
132    }
133
134    #[instrument(skip_all, parent = Span::current(), level= "Trace")]
135    fn get_hyperlight_value(&self) -> ParameterValue {
136        ParameterValue::ULong(*self)
137    }
138
139    #[instrument(err(Debug), skip_all, parent = Span::current(), level= "Trace")]
140    fn get_inner(a: ParameterValue) -> Result<u64> {
141        match a {
142            ParameterValue::ULong(ul) => Ok(ul),
143            other => {
144                log_then_return!(ParameterValueConversionFailure(other.clone(), "u64"));
145            }
146        }
147    }
148}
149
150impl SupportedParameterType<bool> for bool {
151    #[instrument(skip_all, parent = Span::current(), level= "Trace")]
152    fn get_hyperlight_type() -> ParameterType {
153        ParameterType::Bool
154    }
155
156    #[instrument(skip_all, parent = Span::current(), level= "Trace")]
157    fn get_hyperlight_value(&self) -> ParameterValue {
158        ParameterValue::Bool(*self)
159    }
160
161    #[instrument(err(Debug), skip_all, parent = Span::current(), level= "Trace")]
162    fn get_inner(a: ParameterValue) -> Result<bool> {
163        match a {
164            ParameterValue::Bool(i) => Ok(i),
165            other => {
166                log_then_return!(ParameterValueConversionFailure(other.clone(), "bool"));
167            }
168        }
169    }
170}
171
172impl SupportedParameterType<Vec<u8>> for Vec<u8> {
173    #[instrument(skip_all, parent = Span::current(), level= "Trace")]
174    fn get_hyperlight_type() -> ParameterType {
175        ParameterType::VecBytes
176    }
177
178    #[instrument(skip_all, parent = Span::current(), level= "Trace")]
179    fn get_hyperlight_value(&self) -> ParameterValue {
180        ParameterValue::VecBytes(self.clone())
181    }
182
183    #[instrument(err(Debug), skip_all, parent = Span::current(), level= "Trace")]
184    fn get_inner(a: ParameterValue) -> Result<Vec<u8>> {
185        match a {
186            ParameterValue::VecBytes(i) => Ok(i),
187            other => {
188                log_then_return!(ParameterValueConversionFailure(other.clone(), "Vec<u8>"));
189            }
190        }
191    }
192}