Skip to content

Improve ergonomics of SupportedParameterType and SupportedReturnType #476

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ pub enum ReturnValue {
/// bool
Bool(bool),
/// ()
Void,
Void(()),
/// Vec<u8>
VecBytes(Vec<u8>),
}
Expand Down Expand Up @@ -508,7 +508,7 @@ impl TryFrom<ReturnValue> for () {
#[cfg_attr(feature = "tracing", instrument(err(Debug), skip_all, parent = Span::current(), level= "Trace"))]
fn try_from(value: ReturnValue) -> Result<Self> {
match value {
ReturnValue::Void => Ok(()),
ReturnValue::Void(()) => Ok(()),
_ => {
bail!("Unexpected return value type: {:?}", value)
}
Expand Down Expand Up @@ -570,7 +570,7 @@ impl TryFrom<FbFunctionCallResult<'_>> for ReturnValue {
};
Ok(ReturnValue::String(hlstring.unwrap_or("".to_string())))
}
FbReturnValue::hlvoid => Ok(ReturnValue::Void),
FbReturnValue::hlvoid => Ok(ReturnValue::Void(())),
FbReturnValue::hlsizeprefixedbuffer => {
let hlvecbytes =
match function_call_result_fb.return_value_as_hlsizeprefixedbuffer() {
Expand Down Expand Up @@ -724,7 +724,7 @@ impl TryFrom<&ReturnValue> for Vec<u8> {
builder.finish_size_prefixed(function_call_result, None);
builder.finished_data().to_vec()
}
ReturnValue::Void => {
ReturnValue::Void(()) => {
let hlvoid = hlvoid::create(&mut builder, &hlvoidArgs {});
let function_call_result = FbFunctionCallResult::create(
&mut builder,
Expand Down
10 changes: 5 additions & 5 deletions src/hyperlight_host/src/func/host_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ macro_rules! impl_host_function {
let cloned = self_.clone();
let func = Box::new(move |args: Vec<ParameterValue>| {
let ($($P,)*) = match <[ParameterValue; N]>::try_from(args) {
Ok([$($P,)*]) => ($($P::get_inner($P)?,)*),
Ok([$($P,)*]) => ($($P::from_value($P)?,)*),
Err(args) => { log_then_return!(UnexpectedNoOfArguments(args.len(), N)); }
};

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

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

if let Some(_eas) = extra_allowed_syscalls {
if cfg!(all(feature = "seccomp", target_os = "linux")) {
Expand All @@ -196,7 +196,7 @@ macro_rules! impl_host_function {
&HostFunctionDefinition::new(
name.to_string(),
parameter_types,
R::get_hyperlight_type(),
R::TYPE,
),
HyperlightFunction::new(func),
_eas,
Expand All @@ -216,7 +216,7 @@ macro_rules! impl_host_function {
&HostFunctionDefinition::new(
name.to_string(),
parameter_types,
R::get_hyperlight_type(),
R::TYPE,
),
HyperlightFunction::new(func),
)?;
Expand Down
184 changes: 35 additions & 149 deletions src/hyperlight_host/src/func/param_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,167 +26,53 @@ use crate::{log_then_return, Result};
/// For each parameter type Hyperlight supports in host functions, we
/// provide an implementation for `SupportedParameterType`
pub trait SupportedParameterType: Sized {
/// Get the underlying Hyperlight parameter type representing this
/// `SupportedParameterType`
fn get_hyperlight_type() -> ParameterType;
/// The underlying Hyperlight parameter type representing this `SupportedParameterType`
const TYPE: ParameterType;

/// Get the underling Hyperlight parameter value representing this
/// `SupportedParameterType`
fn get_hyperlight_value(&self) -> ParameterValue;
fn into_value(self) -> ParameterValue;
/// Get the actual inner value of this `SupportedParameterType`
fn get_inner(a: ParameterValue) -> Result<Self>;
fn from_value(value: ParameterValue) -> Result<Self>;
}

// We can then implement these traits for each type that Hyperlight supports as a parameter or return type
impl SupportedParameterType for String {
#[instrument(skip_all, parent = Span::current(), level= "Trace")]
fn get_hyperlight_type() -> ParameterType {
ParameterType::String
}

#[instrument(skip_all, parent = Span::current(), level= "Trace")]
fn get_hyperlight_value(&self) -> ParameterValue {
ParameterValue::String(self.clone())
}

#[instrument(err(Debug), skip_all, parent = Span::current(), level= "Trace")]
fn get_inner(a: ParameterValue) -> Result<String> {
match a {
ParameterValue::String(i) => Ok(i),
other => {
log_then_return!(ParameterValueConversionFailure(other.clone(), "String"));
}
}
}
}

impl SupportedParameterType for i32 {
#[instrument(skip_all, parent = Span::current(), level= "Trace")]
fn get_hyperlight_type() -> ParameterType {
ParameterType::Int
}

#[instrument(skip_all, parent = Span::current(), level= "Trace")]
fn get_hyperlight_value(&self) -> ParameterValue {
ParameterValue::Int(*self)
}

#[instrument(err(Debug), skip_all, parent = Span::current(), level= "Trace")]
fn get_inner(a: ParameterValue) -> Result<i32> {
match a {
ParameterValue::Int(i) => Ok(i),
other => {
log_then_return!(ParameterValueConversionFailure(other.clone(), "i32"));
}
}
}
}

impl SupportedParameterType for u32 {
#[instrument(skip_all, parent = Span::current(), level= "Trace")]
fn get_hyperlight_type() -> ParameterType {
ParameterType::UInt
}

#[instrument(skip_all, parent = Span::current(), level= "Trace")]
fn get_hyperlight_value(&self) -> ParameterValue {
ParameterValue::UInt(*self)
}

#[instrument(err(Debug), skip_all, parent = Span::current(), level= "Trace")]
fn get_inner(a: ParameterValue) -> Result<u32> {
match a {
ParameterValue::UInt(ui) => Ok(ui),
other => {
log_then_return!(ParameterValueConversionFailure(other.clone(), "u32"));
}
}
}
macro_rules! for_each_param_type {
($macro:ident) => {
$macro!(String, String);
$macro!(i32, Int);
$macro!(u32, UInt);
$macro!(i64, Long);
$macro!(u64, ULong);
$macro!(bool, Bool);
$macro!(Vec<u8>, VecBytes);
};
}

impl SupportedParameterType for i64 {
#[instrument(skip_all, parent = Span::current(), level= "Trace")]
fn get_hyperlight_type() -> ParameterType {
ParameterType::Long
}

#[instrument(skip_all, parent = Span::current(), level= "Trace")]
fn get_hyperlight_value(&self) -> ParameterValue {
ParameterValue::Long(*self)
}
macro_rules! impl_supported_param_type {
($type:ty, $enum:ident) => {
impl SupportedParameterType for $type {
const TYPE: ParameterType = ParameterType::$enum;

#[instrument(err(Debug), skip_all, parent = Span::current(), level= "Trace")]
fn get_inner(a: ParameterValue) -> Result<i64> {
match a {
ParameterValue::Long(l) => Ok(l),
other => {
log_then_return!(ParameterValueConversionFailure(other.clone(), "i64"));
#[instrument(skip_all, parent = Span::current(), level= "Trace")]
fn into_value(self) -> ParameterValue {
ParameterValue::$enum(self)
}
}
}
}

impl SupportedParameterType for u64 {
#[instrument(skip_all, parent = Span::current(), level= "Trace")]
fn get_hyperlight_type() -> ParameterType {
ParameterType::ULong
}

#[instrument(skip_all, parent = Span::current(), level= "Trace")]
fn get_hyperlight_value(&self) -> ParameterValue {
ParameterValue::ULong(*self)
}

#[instrument(err(Debug), skip_all, parent = Span::current(), level= "Trace")]
fn get_inner(a: ParameterValue) -> Result<u64> {
match a {
ParameterValue::ULong(ul) => Ok(ul),
other => {
log_then_return!(ParameterValueConversionFailure(other.clone(), "u64"));
#[instrument(err(Debug), skip_all, parent = Span::current(), level= "Trace")]
fn from_value(value: ParameterValue) -> Result<Self> {
match value {
ParameterValue::$enum(i) => Ok(i),
other => {
log_then_return!(ParameterValueConversionFailure(
other.clone(),
stringify!($type)
));
}
}
}
}
}
};
}

impl SupportedParameterType for bool {
#[instrument(skip_all, parent = Span::current(), level= "Trace")]
fn get_hyperlight_type() -> ParameterType {
ParameterType::Bool
}

#[instrument(skip_all, parent = Span::current(), level= "Trace")]
fn get_hyperlight_value(&self) -> ParameterValue {
ParameterValue::Bool(*self)
}

#[instrument(err(Debug), skip_all, parent = Span::current(), level= "Trace")]
fn get_inner(a: ParameterValue) -> Result<bool> {
match a {
ParameterValue::Bool(i) => Ok(i),
other => {
log_then_return!(ParameterValueConversionFailure(other.clone(), "bool"));
}
}
}
}

impl SupportedParameterType for Vec<u8> {
#[instrument(skip_all, parent = Span::current(), level= "Trace")]
fn get_hyperlight_type() -> ParameterType {
ParameterType::VecBytes
}

#[instrument(skip_all, parent = Span::current(), level= "Trace")]
fn get_hyperlight_value(&self) -> ParameterValue {
ParameterValue::VecBytes(self.clone())
}

#[instrument(err(Debug), skip_all, parent = Span::current(), level= "Trace")]
fn get_inner(a: ParameterValue) -> Result<Vec<u8>> {
match a {
ParameterValue::VecBytes(i) => Ok(i),
other => {
log_then_return!(ParameterValueConversionFailure(other.clone(), "Vec<u8>"));
}
}
}
}
for_each_param_type!(impl_supported_param_type);
Loading
Loading