File tree Expand file tree Collapse file tree 3 files changed +35
-15
lines changed
hyperlight_common/src/func
hyperlight_guest_macro/src Expand file tree Collapse file tree 3 files changed +35
-15
lines changed Original file line number Diff line number Diff 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) ]
3537macro_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
8483impl < T , E > ResultType < E > for T
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
10195impl < 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
117107for_each_return_type ! ( impl_supported_return_type) ;
Original file line number Diff line number Diff 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" ) ]
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments