@@ -28,17 +28,23 @@ use hyperlight_common::func::{
2828} ;
2929use hyperlight_guest:: error:: { HyperlightGuestError , Result } ;
3030
31- /// The definition of a function exposed from the guest to the host
32- #[ derive( Debug , Clone , PartialEq , Eq ) ]
33- pub struct GuestFunctionDefinition {
31+ /// The function pointer type for Rust guest functions.
32+ pub type GuestFunc = fn ( FunctionCall ) -> Result < Vec < u8 > > ;
33+
34+ /// The definition of a function exposed from the guest to the host.
35+ ///
36+ /// The type parameter `F` is the function pointer type. For Rust guests this
37+ /// is [`GuestFunc`]; the C API uses its own `CGuestFunc` type.
38+ #[ derive( Debug , Clone ) ]
39+ pub struct GuestFunctionDefinition < F : Copy > {
3440 /// The function name
3541 pub function_name : String ,
3642 /// The type of the parameter values for the host function call.
3743 pub parameter_types : Vec < ParameterType > ,
3844 /// The type of the return value from the host function call
3945 pub return_type : ReturnType ,
40- /// The function pointer to the guest function
41- pub function_pointer : usize ,
46+ /// The function pointer to the guest function.
47+ pub function_pointer : F ,
4248}
4349
4450/// Trait for functions that can be converted to a `fn(FunctionCall) -> Result<Vec<u8>>`
5763 fn into_guest_function ( self ) -> fn ( FunctionCall ) -> Result < Vec < u8 > > ;
5864}
5965
60- /// Trait for functions that can be converted to a `GuestFunctionDefinition`
66+ /// Trait for functions that can be converted to a `GuestFunctionDefinition<GuestFunc> `
6167pub trait AsGuestFunctionDefinition < Output , Args >
6268where
6369 Self : Function < Output , Args , HyperlightGuestError > ,
6672 Args : ParameterTuple ,
6773{
6874 /// Get the `GuestFunctionDefinition` for this function
69- fn as_guest_function_definition ( & self , name : impl Into < String > ) -> GuestFunctionDefinition ;
75+ fn as_guest_function_definition (
76+ & self ,
77+ name : impl Into < String > ,
78+ ) -> GuestFunctionDefinition < GuestFunc > ;
7079}
7180
7281fn into_flatbuffer_result ( value : ReturnValue ) -> Vec < u8 > {
@@ -141,17 +150,19 @@ macro_rules! impl_host_function {
141150 } ;
142151}
143152
144- impl < F , Args , Output > AsGuestFunctionDefinition < Output , Args > for F
153+ impl < Func , Args , Output > AsGuestFunctionDefinition < Output , Args > for Func
145154where
146- F : IntoGuestFunction < Output , Args > ,
155+ Func : IntoGuestFunction < Output , Args > ,
147156 Args : ParameterTuple ,
148157 Output : SupportedReturnType ,
149158{
150- fn as_guest_function_definition ( & self , name : impl Into < String > ) -> GuestFunctionDefinition {
159+ fn as_guest_function_definition (
160+ & self ,
161+ name : impl Into < String > ,
162+ ) -> GuestFunctionDefinition < GuestFunc > {
151163 let parameter_types = Args :: TYPE . to_vec ( ) ;
152164 let return_type = Output :: TYPE ;
153165 let function_pointer = self . into_guest_function ( ) ;
154- let function_pointer = function_pointer as usize ;
155166
156167 GuestFunctionDefinition {
157168 function_name : name. into ( ) ,
@@ -164,13 +175,13 @@ where
164175
165176for_each_tuple ! ( impl_host_function) ;
166177
167- impl GuestFunctionDefinition {
178+ impl < F : Copy > GuestFunctionDefinition < F > {
168179 /// Create a new `GuestFunctionDefinition`.
169180 pub fn new (
170181 function_name : String ,
171182 parameter_types : Vec < ParameterType > ,
172183 return_type : ReturnType ,
173- function_pointer : usize ,
184+ function_pointer : F ,
174185 ) -> Self {
175186 Self {
176187 function_name,
@@ -180,12 +191,12 @@ impl GuestFunctionDefinition {
180191 }
181192 }
182193
183- /// Create a new `GuestFunctionDefinition` from a function that implements
184- /// `AsGuestFunctionDefinition`.
194+ /// Create a new `GuestFunctionDefinition<GuestFunc> ` from a function that
195+ /// implements `AsGuestFunctionDefinition`.
185196 pub fn from_fn < Output , Args > (
186197 function_name : String ,
187198 function : impl AsGuestFunctionDefinition < Output , Args > ,
188- ) -> Self
199+ ) -> GuestFunctionDefinition < GuestFunc >
189200 where
190201 Args : ParameterTuple ,
191202 Output : SupportedReturnType ,
0 commit comments