Skip to content

Commit 7373501

Browse files
authored
Improve ergonomics of SupportedParameterType and SupportedReturnType (#476)
Signed-off-by: Jorge Prendes <[email protected]>
1 parent ce8410a commit 7373501

File tree

4 files changed

+79
-328
lines changed

4 files changed

+79
-328
lines changed

src/hyperlight_common/src/flatbuffer_wrappers/function_types.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ pub enum ReturnValue {
9999
/// bool
100100
Bool(bool),
101101
/// ()
102-
Void,
102+
Void(()),
103103
/// Vec<u8>
104104
VecBytes(Vec<u8>),
105105
}
@@ -508,7 +508,7 @@ impl TryFrom<ReturnValue> for () {
508508
#[cfg_attr(feature = "tracing", instrument(err(Debug), skip_all, parent = Span::current(), level= "Trace"))]
509509
fn try_from(value: ReturnValue) -> Result<Self> {
510510
match value {
511-
ReturnValue::Void => Ok(()),
511+
ReturnValue::Void(()) => Ok(()),
512512
_ => {
513513
bail!("Unexpected return value type: {:?}", value)
514514
}
@@ -570,7 +570,7 @@ impl TryFrom<FbFunctionCallResult<'_>> for ReturnValue {
570570
};
571571
Ok(ReturnValue::String(hlstring.unwrap_or("".to_string())))
572572
}
573-
FbReturnValue::hlvoid => Ok(ReturnValue::Void),
573+
FbReturnValue::hlvoid => Ok(ReturnValue::Void(())),
574574
FbReturnValue::hlsizeprefixedbuffer => {
575575
let hlvecbytes =
576576
match function_call_result_fb.return_value_as_hlsizeprefixedbuffer() {
@@ -724,7 +724,7 @@ impl TryFrom<&ReturnValue> for Vec<u8> {
724724
builder.finish_size_prefixed(function_call_result, None);
725725
builder.finished_data().to_vec()
726726
}
727-
ReturnValue::Void => {
727+
ReturnValue::Void(()) => {
728728
let hlvoid = hlvoid::create(&mut builder, &hlvoidArgs {});
729729
let function_call_result = FbFunctionCallResult::create(
730730
&mut builder,

src/hyperlight_host/src/func/host_functions.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ macro_rules! impl_host_function {
169169
let cloned = self_.clone();
170170
let func = Box::new(move |args: Vec<ParameterValue>| {
171171
let ($($P,)*) = match <[ParameterValue; N]>::try_from(args) {
172-
Ok([$($P,)*]) => ($($P::get_inner($P)?,)*),
172+
Ok([$($P,)*]) => ($($P::from_value($P)?,)*),
173173
Err(args) => { log_then_return!(UnexpectedNoOfArguments(args.len(), N)); }
174174
};
175175

@@ -178,10 +178,10 @@ macro_rules! impl_host_function {
178178
.map_err(|e| new_error!("Error locking at {}:{}: {}", file!(), line!(), e))?(
179179
$($P),*
180180
)?;
181-
Ok(result.get_hyperlight_value())
181+
Ok(result.into_value())
182182
});
183183

184-
let parameter_types = Some(vec![$($P::get_hyperlight_type()),*]);
184+
let parameter_types = Some(vec![$($P::TYPE),*]);
185185

186186
if let Some(_eas) = extra_allowed_syscalls {
187187
if cfg!(all(feature = "seccomp", target_os = "linux")) {
@@ -196,7 +196,7 @@ macro_rules! impl_host_function {
196196
&HostFunctionDefinition::new(
197197
name.to_string(),
198198
parameter_types,
199-
R::get_hyperlight_type(),
199+
R::TYPE,
200200
),
201201
HyperlightFunction::new(func),
202202
_eas,
@@ -216,7 +216,7 @@ macro_rules! impl_host_function {
216216
&HostFunctionDefinition::new(
217217
name.to_string(),
218218
parameter_types,
219-
R::get_hyperlight_type(),
219+
R::TYPE,
220220
),
221221
HyperlightFunction::new(func),
222222
)?;

src/hyperlight_host/src/func/param_type.rs

+35-149
Original file line numberDiff line numberDiff line change
@@ -26,167 +26,53 @@ use crate::{log_then_return, Result};
2626
/// For each parameter type Hyperlight supports in host functions, we
2727
/// provide an implementation for `SupportedParameterType`
2828
pub trait SupportedParameterType: Sized {
29-
/// Get the underlying Hyperlight parameter type representing this
30-
/// `SupportedParameterType`
31-
fn get_hyperlight_type() -> ParameterType;
29+
/// The underlying Hyperlight parameter type representing this `SupportedParameterType`
30+
const TYPE: ParameterType;
31+
3232
/// Get the underling Hyperlight parameter value representing this
3333
/// `SupportedParameterType`
34-
fn get_hyperlight_value(&self) -> ParameterValue;
34+
fn into_value(self) -> ParameterValue;
3535
/// Get the actual inner value of this `SupportedParameterType`
36-
fn get_inner(a: ParameterValue) -> Result<Self>;
36+
fn from_value(value: ParameterValue) -> Result<Self>;
3737
}
3838

3939
// We can then implement these traits for each type that Hyperlight supports as a parameter or return type
40-
impl SupportedParameterType 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-
62-
impl SupportedParameterType 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-
84-
impl SupportedParameterType 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-
}
40+
macro_rules! for_each_param_type {
41+
($macro:ident) => {
42+
$macro!(String, String);
43+
$macro!(i32, Int);
44+
$macro!(u32, UInt);
45+
$macro!(i64, Long);
46+
$macro!(u64, ULong);
47+
$macro!(bool, Bool);
48+
$macro!(Vec<u8>, VecBytes);
49+
};
10450
}
10551

106-
impl SupportedParameterType 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-
}
52+
macro_rules! impl_supported_param_type {
53+
($type:ty, $enum:ident) => {
54+
impl SupportedParameterType for $type {
55+
const TYPE: ParameterType = ParameterType::$enum;
11656

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"));
57+
#[instrument(skip_all, parent = Span::current(), level= "Trace")]
58+
fn into_value(self) -> ParameterValue {
59+
ParameterValue::$enum(self)
12360
}
124-
}
125-
}
126-
}
127-
128-
impl SupportedParameterType for u64 {
129-
#[instrument(skip_all, parent = Span::current(), level= "Trace")]
130-
fn get_hyperlight_type() -> ParameterType {
131-
ParameterType::ULong
132-
}
13361

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"));
62+
#[instrument(err(Debug), skip_all, parent = Span::current(), level= "Trace")]
63+
fn from_value(value: ParameterValue) -> Result<Self> {
64+
match value {
65+
ParameterValue::$enum(i) => Ok(i),
66+
other => {
67+
log_then_return!(ParameterValueConversionFailure(
68+
other.clone(),
69+
stringify!($type)
70+
));
71+
}
72+
}
14573
}
14674
}
147-
}
75+
};
14876
}
14977

150-
impl SupportedParameterType 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-
172-
impl SupportedParameterType 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-
}
78+
for_each_param_type!(impl_supported_param_type);

0 commit comments

Comments
 (0)