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
the parameter _policy in this function is &String, but this function only need a immutable reference, i recommend use &str.
Similarly, in this function:
the type of _pks should be &[Aw11PublicKey] instead of &Vec<Aw11PublicKey>
Reason
I can construct a String from a *const c_char by let s = String::from_raw_parts(policy as *mut u8,len, len) during FFI calling.But, because this String not really own its memory, so we need a std::mem::forget(s) to make sure not free the memory passed by FFI calling.
If use &str, i can construct a slice from pointer directly and no need to care about memory.
Also, When i pass a array of pointer for &Vec<Aw11PublicKey>, i need to use Vec::from_raw_partsand not free the memory.
Not only in FFI, if i want to pass a let policy = "xxxxx", i also need to construct a String and pass it to function. But if the parameter is &str, i can pass policy directly.
And because String had impl trait Deref<str>, pass a &String to a &str is ok.
Similarly, use &Vec<T> instead of &[T] has the same problem. Such as &Vec<String>, all of these are FFI unfriendly and may cause unnecessary overhead under certain circumstances.
The text was updated successfully, but these errors were encountered:
Hi, I'm writing a C-FFI binding for this project, but i think some function signature is not reasonable enough.
Example
the parameter _policy in this function is &String, but this function only need a immutable reference, i recommend use
&str
.Similarly, in this function:
the type of _pks should be
&[Aw11PublicKey]
instead of&Vec<Aw11PublicKey>
Reason
String
from a*const c_char
bylet s = String::from_raw_parts(policy as *mut u8,len, len)
during FFI calling.But, because thisString
not really own its memory, so we need astd::mem::forget(s)
to make sure not free the memory passed by FFI calling.&str
, i can construct a slice from pointer directly and no need to care about memory.&Vec<Aw11PublicKey>
, i need to useVec::from_raw_parts
and not free the memory.let policy = "xxxxx"
, i also need to construct aString
and pass it to function. But if the parameter is &str, i can passpolicy
directly.String
had impl traitDeref<str>
, pass a&String
to a&str
is ok.&Vec<T>
instead of&[T]
has the same problem. Such as&Vec<String>
, all of these are FFI unfriendly and may cause unnecessary overhead under certain circumstances.The text was updated successfully, but these errors were encountered: