rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
97.09k stars 12.55k forks source link

Implement TryInto<CString> for str and String #71448

Open kornelski opened 4 years ago

kornelski commented 4 years ago

Higher-level FFI wrappers that need to present a CString to C don't have a convenient + efficient type to take on the Rust side.

ffi_string(arg: ???) {
   let c_string = CString::new(arg)?;
   ffify(c_string.as_ptr());
}

Taking impl TryInto<CString> would be ideal, if that trait was implemented for common string-like types.

EvanCarroll commented 10 months ago

There is already .into_string() I would love to know why this isn't deprecated and TryInto implemented instead.

kornelski commented 10 months ago

cstring.into_string is a conversion in the opposite direction than what I had in mind, but I wouldn't mind if both were supported via TryFrom/TryInto.