I have generated a couple of -sys
crates with bindgen and have also written some more ergonomic wrappers around them but I'm still not sure how to deal with strings calling into the FFI. I'm curious what other people do?
Say for instance, your bound API has a function device_t device_create(protocol_t protocol, const char *name)
that creates some device with a name and you want to wrap that..
struct Device(foo_sys::device_t);
would you just take a CString
to create the device?
impl Device {
pub fn new(protocol: Protocol, name: CString) -> Self;
}
or make the creation fallible, use String
and hide the CString
?
impl Device {
pub fn new(protocol: Protocol, name: String) -> Option<Self>;
}
or panic?
impl Device {
pub fn new(protocol: Protocol, name: String) -> Self {
let name = CString::new(name).expect("failed to convert name to CString");
....
}
}
or??
I'm feeling like the fallible one with String
is the best from a usage standpoint..