@@ -16,7 +16,6 @@ use soroban_env_host::xdr::{
16
16
AccountId , InvokeHostFunctionOp , LedgerFootprint , OperationBody , ReadXdr , WriteXdr ,
17
17
} ;
18
18
use soroban_env_host:: LedgerInfo ;
19
- use std:: convert:: { TryFrom , TryInto } ;
20
19
use std:: ffi:: { CStr , CString } ;
21
20
use std:: mem;
22
21
use std:: panic;
@@ -36,12 +35,10 @@ pub struct CLedgerInfo {
36
35
pub autobump_ledgers : u32 ,
37
36
}
38
37
39
- impl TryFrom < CLedgerInfo > for LedgerInfo {
40
- type Error = anyhow:: Error ;
41
-
42
- fn try_from ( c : CLedgerInfo ) -> Result < Self > {
43
- let network_passphrase = from_c_string ( c. network_passphrase ) ?;
44
- Ok ( Self {
38
+ impl From < CLedgerInfo > for LedgerInfo {
39
+ fn from ( c : CLedgerInfo ) -> Self {
40
+ let network_passphrase = from_c_string ( c. network_passphrase ) ;
41
+ Self {
45
42
protocol_version : c. protocol_version ,
46
43
sequence_number : c. sequence_number ,
47
44
timestamp : c. timestamp ,
@@ -51,7 +48,7 @@ impl TryFrom<CLedgerInfo> for LedgerInfo {
51
48
min_persistent_entry_expiration : c. min_persistent_entry_expiration ,
52
49
max_entry_expiration : c. max_entry_expiration ,
53
50
autobump_ledgers : c. autobump_ledgers ,
54
- } )
51
+ }
55
52
}
56
53
}
57
54
@@ -69,30 +66,28 @@ pub struct CPreflightResult {
69
66
pub pre_restore_min_fee : i64 , // Minimum recommended resource fee for a prerequired RestoreFootprint operation
70
67
}
71
68
72
- impl TryFrom < PreflightResult > for CPreflightResult {
73
- type Error = anyhow:: Error ;
74
-
75
- fn try_from ( p : PreflightResult ) -> Result < Self > {
69
+ impl From < PreflightResult > for CPreflightResult {
70
+ fn from ( p : PreflightResult ) -> Self {
76
71
let mut result = Self {
77
72
error : null_mut ( ) ,
78
- auth : xdr_vec_to_base64_c_null_terminated_char_array ( p. auth ) ? ,
73
+ auth : xdr_vec_to_base64_c_null_terminated_char_array ( p. auth ) ,
79
74
result : match p. result {
80
75
None => null_mut ( ) ,
81
- Some ( v) => xdr_to_base64_c ( v) ? ,
76
+ Some ( v) => xdr_to_base64_c ( v) ,
82
77
} ,
83
- transaction_data : xdr_to_base64_c ( p. transaction_data ) ? ,
78
+ transaction_data : xdr_to_base64_c ( p. transaction_data ) ,
84
79
min_fee : p. min_fee ,
85
- events : xdr_vec_to_base64_c_null_terminated_char_array ( p. events ) ? ,
80
+ events : xdr_vec_to_base64_c_null_terminated_char_array ( p. events ) ,
86
81
cpu_instructions : p. cpu_instructions ,
87
82
memory_bytes : p. memory_bytes ,
88
83
pre_restore_transaction_data : null_mut ( ) ,
89
84
pre_restore_min_fee : 0 ,
90
85
} ;
91
86
if let Some ( p) = p. restore_preamble {
92
87
result. pre_restore_min_fee = p. min_fee ;
93
- result. pre_restore_transaction_data = xdr_to_base64_c ( p. transaction_data ) ? ;
88
+ result. pre_restore_transaction_data = xdr_to_base64_c ( p. transaction_data ) ;
94
89
} ;
95
- Ok ( result)
90
+ result
96
91
}
97
92
}
98
93
@@ -122,18 +117,18 @@ fn preflight_invoke_hf_op_or_maybe_panic(
122
117
source_account : * const libc:: c_char , // AccountId XDR in base64
123
118
ledger_info : CLedgerInfo ,
124
119
) -> Result < CPreflightResult > {
125
- let invoke_hf_op = InvokeHostFunctionOp :: from_xdr_base64 ( from_c_string ( invoke_hf_op) ? ) ? ;
126
- let source_account = AccountId :: from_xdr_base64 ( from_c_string ( source_account) ? ) ? ;
120
+ let invoke_hf_op = InvokeHostFunctionOp :: from_xdr_base64 ( from_c_string ( invoke_hf_op) ) . unwrap ( ) ;
121
+ let source_account = AccountId :: from_xdr_base64 ( from_c_string ( source_account) ) . unwrap ( ) ;
127
122
let ledger_storage = LedgerStorage :: with_restore_tracking ( handle, ledger_info. sequence_number )
128
123
. context ( "cannot create LedgerStorage" ) ?;
129
124
let result = preflight:: preflight_invoke_hf_op (
130
125
ledger_storage,
131
126
bucket_list_size,
132
127
invoke_hf_op,
133
128
source_account,
134
- ledger_info . try_into ( ) ? ,
129
+ LedgerInfo :: from ( ledger_info ) ,
135
130
) ?;
136
- result. try_into ( )
131
+ Ok ( result. into ( ) )
137
132
}
138
133
139
134
#[ no_mangle]
@@ -162,8 +157,8 @@ fn preflight_footprint_expiration_op_or_maybe_panic(
162
157
footprint : * const libc:: c_char ,
163
158
current_ledger_seq : u32 ,
164
159
) -> Result < CPreflightResult > {
165
- let op_body = OperationBody :: from_xdr_base64 ( from_c_string ( op_body) ? ) ? ;
166
- let footprint = LedgerFootprint :: from_xdr_base64 ( from_c_string ( footprint) ? ) ? ;
160
+ let op_body = OperationBody :: from_xdr_base64 ( from_c_string ( op_body) ) . unwrap ( ) ;
161
+ let footprint = LedgerFootprint :: from_xdr_base64 ( from_c_string ( footprint) ) . unwrap ( ) ;
167
162
let ledger_storage = & LedgerStorage :: new ( handle) ;
168
163
let result = preflight:: preflight_footprint_expiration_op (
169
164
ledger_storage,
@@ -172,7 +167,7 @@ fn preflight_footprint_expiration_op_or_maybe_panic(
172
167
footprint,
173
168
current_ledger_seq,
174
169
) ?;
175
- result. try_into ( )
170
+ Ok ( result. into ( ) )
176
171
}
177
172
178
173
fn preflight_error ( str : String ) -> CPreflightResult {
@@ -209,35 +204,35 @@ fn catch_preflight_panic(op: Box<dyn Fn() -> Result<CPreflightResult>>) -> *mut
209
204
Box :: into_raw ( Box :: new ( c_preflight_result) )
210
205
}
211
206
212
- fn xdr_to_base64_c ( v : impl WriteXdr ) -> Result < * mut libc:: c_char > {
213
- string_to_c ( v. to_xdr_base64 ( ) ? )
207
+ fn xdr_to_base64_c ( v : impl WriteXdr ) -> * mut libc:: c_char {
208
+ string_to_c ( v. to_xdr_base64 ( ) . unwrap ( ) )
214
209
}
215
210
216
- fn string_to_c ( str : String ) -> Result < * mut libc:: c_char > {
217
- Ok ( CString :: new ( str) ? . into_raw ( ) )
211
+ fn string_to_c ( str : String ) -> * mut libc:: c_char {
212
+ CString :: new ( str) . unwrap ( ) . into_raw ( )
218
213
}
219
214
220
215
fn xdr_vec_to_base64_c_null_terminated_char_array (
221
216
payloads : Vec < impl WriteXdr > ,
222
- ) -> Result < * mut * mut libc:: c_char > {
217
+ ) -> * mut * mut libc:: c_char {
223
218
let xdr_base64_vec: Vec < String > = payloads
224
219
. iter ( )
225
- . map ( WriteXdr :: to_xdr_base64)
226
- . collect :: < Result < Vec < _ > , _ > > ( ) ? ;
220
+ . map ( |a| WriteXdr :: to_xdr_base64 ( a ) . unwrap ( ) )
221
+ . collect ( ) ;
227
222
string_vec_to_c_null_terminated_char_array ( xdr_base64_vec)
228
223
}
229
224
230
- fn string_vec_to_c_null_terminated_char_array ( v : Vec < String > ) -> Result < * mut * mut libc:: c_char > {
225
+ fn string_vec_to_c_null_terminated_char_array ( v : Vec < String > ) -> * mut * mut libc:: c_char {
231
226
let mut out_vec: Vec < * mut libc:: c_char > = Vec :: new ( ) ;
232
227
for s in & v {
233
- let c_str = string_to_c ( s. clone ( ) ) ? ;
228
+ let c_str = string_to_c ( s. clone ( ) ) ;
234
229
out_vec. push ( c_str) ;
235
230
}
236
231
237
232
// Add the ending NULL
238
233
out_vec. push ( null_mut ( ) ) ;
239
234
240
- Ok ( vec_to_c_array ( out_vec) )
235
+ vec_to_c_array ( out_vec)
241
236
}
242
237
243
238
fn vec_to_c_array < T > ( mut v : Vec < T > ) -> * mut T {
@@ -305,7 +300,7 @@ fn free_c_null_terminated_char_array(array: *mut *mut libc::c_char) {
305
300
_ = Vec :: from_raw_parts ( array, len, len) ;
306
301
}
307
302
}
308
- fn from_c_string ( str : * const libc:: c_char ) -> Result < String > {
303
+ fn from_c_string ( str : * const libc:: c_char ) -> String {
309
304
let c_str = unsafe { CStr :: from_ptr ( str) } ;
310
- Ok ( c_str. to_str ( ) ? . to_string ( ) )
305
+ c_str. to_str ( ) . unwrap ( ) . to_string ( )
311
306
}
0 commit comments