rust-bindgen icon indicating copy to clipboard operation
rust-bindgen copied to clipboard

[feature request] Support Cstr literals

Open novafacing opened this issue 6 months ago • 7 comments

CStr literals (c"Hello, world!") and raw literals (cr"Hello, World!") are supported as of https://github.com/rust-lang/rust/pull/117472 and will be stabilized in ~1month. Would it be possible to add an option to emit this type of literal instead of &'static [u8] for string literals? If it sounds like a good idea, I can try and take a pass at it, it'd be nice for several of my projects.

novafacing avatar Jan 05 '24 21:01 novafacing

I think the stabilization is getting reverted, unfortunately: https://github.com/rust-lang/rust/pull/119528

Property404 avatar Jan 08 '24 21:01 Property404

A bit unfortunate -- although I think bindgen support will be benefitted by the revert so more features (like proc_macro C string literals in https://github.com/rust-lang/rust/issues/119750) can make it in.

Bindgen support for this I think will be blocked by quote support for CStrs, which is blocked on syn support for them, which is blocked on proc-macro2 support, which is blocked on the issue/PR to proc_macro above. I'm planning on adding support to all of the above, but it'll take some time.

novafacing avatar Jan 09 '24 01:01 novafacing

Seems fine to add an experimental feature. See bindgen/features.rs.

emilio avatar Jan 12 '24 22:01 emilio

Looks like it will be re-stabilized in 1.78 on 3/21/24.

How would this interact with the existing generate_cstr option that will "will become enabled by default in a future release"? Would this just change the generated code from using ::core::ffi::CStr::from_bytes_with_nul_unchecked to using the new literal syntax?

wmmc88 avatar Mar 11 '24 18:03 wmmc88

I agree with @emilio, adding this as a nightly only feature meanwhile makes sense. If this feature is merged before the compiler feature is stabilized we can stabilize this feature and update the generate_cstr option in a single PR.

pvdrz avatar Mar 12 '24 16:03 pvdrz

C-string literals was stabilized in Rust 1.77: https://blog.rust-lang.org/2024/03/21/Rust-1.77.0.html#c-string-literals

teohhanhui avatar May 07 '24 19:05 teohhanhui