@@ -14,6 +14,7 @@ See the License for the specific language governing permissions and
1414limitations under the License.
1515*/
1616
17+ use alloc:: format;
1718use alloc:: string:: ToString ;
1819use alloc:: vec:: Vec ;
1920use core:: arch:: global_asm;
@@ -39,94 +40,18 @@ pub enum OutBAction {
3940 Abort = 102 ,
4041}
4142
42- pub fn get_host_value_return_as_void ( ) -> Result < ( ) > {
43+ pub fn get_host_value_return_as < T : TryFrom < ReturnValue > > ( ) -> Result < T > {
4344 let return_value = try_pop_shared_input_data_into :: < ReturnValue > ( )
4445 . expect ( "Unable to deserialize a return value from host" ) ;
45- if let ReturnValue :: Void = return_value {
46- Ok ( ( ) )
47- } else {
48- Err ( HyperlightGuestError :: new (
49- ErrorCode :: GuestError ,
50- "Host return value was not void as expected" . to_string ( ) ,
51- ) )
52- }
53- }
54-
55- pub fn get_host_value_return_as_int ( ) -> Result < i32 > {
56- let return_value = try_pop_shared_input_data_into :: < ReturnValue > ( )
57- . expect ( "Unable to deserialize return value from host" ) ;
58-
59- // check that return value is an int and return
60- if let ReturnValue :: Int ( i) = return_value {
61- Ok ( i)
62- } else {
63- Err ( HyperlightGuestError :: new (
64- ErrorCode :: GuestError ,
65- "Host return value was not an int as expected" . to_string ( ) ,
66- ) )
67- }
68- }
69-
70- pub fn get_host_value_return_as_uint ( ) -> Result < u32 > {
71- let return_value = try_pop_shared_input_data_into :: < ReturnValue > ( )
72- . expect ( "Unable to deserialize return value from host" ) ;
73-
74- // check that return value is an int and return
75- if let ReturnValue :: UInt ( ui) = return_value {
76- Ok ( ui)
77- } else {
78- Err ( HyperlightGuestError :: new (
79- ErrorCode :: GuestError ,
80- "Host return value was not a uint as expected" . to_string ( ) ,
81- ) )
82- }
83- }
84-
85- pub fn get_host_value_return_as_long ( ) -> Result < i64 > {
86- let return_value = try_pop_shared_input_data_into :: < ReturnValue > ( )
87- . expect ( "Unable to deserialize return value from host" ) ;
88-
89- // check that return value is an int and return
90- if let ReturnValue :: Long ( l) = return_value {
91- Ok ( l)
92- } else {
93- Err ( HyperlightGuestError :: new (
94- ErrorCode :: GuestError ,
95- "Host return value was not a long as expected" . to_string ( ) ,
96- ) )
97- }
98- }
99-
100- pub fn get_host_value_return_as_ulong ( ) -> Result < u64 > {
101- let return_value = try_pop_shared_input_data_into :: < ReturnValue > ( )
102- . expect ( "Unable to deserialize return value from host" ) ;
103-
104- // check that return value is an int and return
105- if let ReturnValue :: ULong ( ul) = return_value {
106- Ok ( ul)
107- } else {
108- Err ( HyperlightGuestError :: new (
46+ T :: try_from ( return_value) . map_err ( |_| {
47+ HyperlightGuestError :: new (
10948 ErrorCode :: GuestError ,
110- "Host return value was not a ulong as expected" . to_string ( ) ,
111- ) )
112- }
113- }
114-
115- // TODO: Make this generic, return a Result<T, ErrorCode>
116-
117- pub fn get_host_value_return_as_vecbytes ( ) -> Result < Vec < u8 > > {
118- let return_value = try_pop_shared_input_data_into :: < ReturnValue > ( )
119- . expect ( "Unable to deserialize return value from host" ) ;
120-
121- // check that return value is an Vec<u8> and return
122- if let ReturnValue :: VecBytes ( v) = return_value {
123- Ok ( v)
124- } else {
125- Err ( HyperlightGuestError :: new (
126- ErrorCode :: GuestError ,
127- "Host return value was not an VecBytes as expected" . to_string ( ) ,
128- ) )
129- }
49+ format ! (
50+ "Host return value was not a {} as expected" ,
51+ core:: any:: type_name:: <T >( )
52+ ) ,
53+ )
54+ } )
13055}
13156
13257// TODO: Make this generic, return a Result<T, ErrorCode> this should allow callers to call this function and get the result type they expect
@@ -194,7 +119,7 @@ pub fn print_output_as_guest_function(function_call: &FunctionCall) -> Result<Ve
194119 Some ( Vec :: from ( & [ ParameterValue :: String ( message. to_string ( ) ) ] ) ) ,
195120 ReturnType :: Int ,
196121 ) ?;
197- let res_i = get_host_value_return_as_int ( ) ?;
122+ let res_i = get_host_value_return_as :: < i32 > ( ) ?;
198123 Ok ( get_flatbuffer_result ( res_i) )
199124 } else {
200125 Err ( HyperlightGuestError :: new (
0 commit comments