Skip to content

Commit f9856b2

Browse files
committed
Move result unwrap used in host_function macro from hl-common to hl-guest-bin
Signed-off-by: Jorge Prendes <[email protected]>
1 parent 836d8ef commit f9856b2

File tree

3 files changed

+35
-15
lines changed

3 files changed

+35
-15
lines changed

src/hyperlight_common/src/func/ret_type.rs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ pub trait SupportedReturnType: Sized + Clone + Send + Sync + 'static {
3232
fn from_value(value: ReturnValue) -> Result<Self, Error>;
3333
}
3434

35+
#[macro_export]
36+
#[doc(hidden)]
3537
macro_rules! for_each_return_type {
3638
($macro:ident) => {
3739
$macro!((), Void);
@@ -76,9 +78,6 @@ pub trait ResultType<E: core::fmt::Debug> {
7678

7779
/// Convert the return type into a `Result<impl SupportedReturnType>`
7880
fn into_result(self) -> Result<Self::ReturnType, E>;
79-
80-
/// Convert a result into this type, panicking if needed
81-
fn from_result(res: Result<Self::ReturnType, E>) -> Self;
8281
}
8382

8483
impl<T, E> ResultType<E> for T
@@ -91,11 +90,6 @@ where
9190
fn into_result(self) -> Result<Self::ReturnType, E> {
9291
Ok(self)
9392
}
94-
95-
fn from_result(res: Result<Self::ReturnType, E>) -> Self {
96-
#![allow(clippy::unwrap_used)]
97-
res.unwrap()
98-
}
9993
}
10094

10195
impl<T, E> ResultType<E> for Result<T, E>
@@ -108,10 +102,6 @@ where
108102
fn into_result(self) -> Result<Self::ReturnType, E> {
109103
self
110104
}
111-
112-
fn from_result(res: Result<Self::ReturnType, E>) -> Self {
113-
res
114-
}
115105
}
116106

117107
for_each_return_type!(impl_supported_return_type);

src/hyperlight_guest_bin/src/lib.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,36 @@ pub mod __private {
260260

261261
#[linkme::distributed_slice]
262262
pub static GUEST_FUNCTION_INIT: [fn()];
263+
264+
pub trait FromResult {
265+
type Output;
266+
fn from_result(res: Result<Self::Output, HyperlightGuestError>) -> Self;
267+
}
268+
269+
use alloc::string::String;
270+
use alloc::vec::Vec;
271+
272+
use hyperlight_common::for_each_return_type;
273+
274+
macro_rules! impl_maybe_unwrap {
275+
($ty:ty, $enum:ident) => {
276+
impl FromResult for $ty {
277+
type Output = Self;
278+
fn from_result(res: Result<Self::Output, HyperlightGuestError>) -> Self {
279+
res.unwrap()
280+
}
281+
}
282+
283+
impl FromResult for Result<$ty, HyperlightGuestError> {
284+
type Output = $ty;
285+
fn from_result(res: Result<Self::Output, HyperlightGuestError>) -> Self {
286+
res
287+
}
288+
}
289+
};
290+
}
291+
292+
for_each_return_type!(impl_maybe_unwrap);
263293
}
264294

265295
#[cfg(feature = "macros")]

src/hyperlight_guest_macro/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ pub fn guest_function(attr: TokenStream, item: TokenStream) -> TokenStream {
8585
#[#crate_name::__private::linkme::distributed_slice(#crate_name::__private::GUEST_FUNCTION_INIT)]
8686
#[linkme(crate = #crate_name::__private::linkme)]
8787
static REGISTRATION: fn() = || {
88-
hyperlight_guest_bin::guest_function::register::register_fn(#exported_name, #ident);
88+
#crate_name::guest_function::register::register_fn(#exported_name, #ident);
8989
};
9090
};
9191
};
@@ -194,9 +194,9 @@ pub fn host_function(attr: TokenStream, item: TokenStream) -> TokenStream {
194194

195195
let output = quote! {
196196
#(#attrs)* #vis #sig {
197-
use #crate_name::__private::{ResultType, HyperlightGuestError};
197+
use #crate_name::__private::FromResult;
198198
use #crate_name::host_comm::call_host;
199-
<#ret as ResultType<HyperlightGuestError>>::from_result(call_host(#exported_name, (#(#args,)*)))
199+
<#ret as FromResult>::from_result(call_host(#exported_name, (#(#args,)*)))
200200
}
201201
};
202202

0 commit comments

Comments
 (0)