You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am currently trying to see how scalable would it be to wrap some rust code in order to be used as a API client for iOS platform.
Most of the code would involved API callbacks and I was not sure how to use the different safe callback types implemented in the closure module.
There is three types defined in the documentation:
Box<dyn FnMut()>
Arc<dyn Fn()>
RefDynFnMut
Any guide on know which type should be used for a specific case ?
For example:
// Which type of callback should I use here 🤔?#[ffi_export]pubfnmobile_client_login(client:Option<repr_c::Box<HTTPClient>>,login: char_p::Ref<'_>,password: char_p::Ref<'_>,mutcallback: ??){let client = client.unwrap();let login = login.to_str().to_string();let password = password.to_string().to_string();(*RUN_TIME).spawn(asyncmove{let response = client.login(&login,&password).await.unwrap();
callback.call(response);});}
In this case, the callback would need to be instantiated from the C code.
My guess is t the way the equivalent structure is instantiated in the C code will determined which version should I use:
RefDynFnMut if the struct is a simple pointer allocated on the stack
Arc<dyn Fn()> or Box<dyn FnMut()> if allocated on the heap
In this case, as the closure is called after the mobile_client_login returns, I think I need to choose the later option.
Those version holds pointer to free or release/retain functions.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I am currently trying to see how scalable would it be to wrap some rust code in order to be used as a API client for iOS platform.
Most of the code would involved API callbacks and I was not sure how to use the different safe callback types implemented in the
closure
module.There is three types defined in the documentation:
Box<dyn FnMut()>
Arc<dyn Fn()>
RefDynFnMut
Any guide on know which type should be used for a specific case ?
For example:
In this case, the callback would need to be instantiated from the C code.
My guess is t the way the equivalent structure is instantiated in the C code will determined which version should I use:
RefDynFnMut
if the struct is a simple pointer allocated on the stackArc<dyn Fn()>
orBox<dyn FnMut()>
if allocated on the heapIn this case, as the closure is called after the
mobile_client_login
returns, I think I need to choose the later option.Those version holds pointer to
free
orrelease/retain
functions.What are the purpose of those functions?
Beta Was this translation helpful? Give feedback.
All reactions