rust-lang / rust-lang/rust-bindgen

`generate_cstr(true)` results in weird spacing for long strings

Open Beginner friendly
#3,399 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
5.3k
Forks
829
Avg merge
1d 1h
Merged PRs (30d)
15

Description

With bindgen 0.72.1, the following code:

fn main() {
    let builder = bindgen::Builder::default()
        .header_contents(
            "php_config.h",
            r#"
#define PHP_OS "Linux"
#define PHP_BUILD_SYSTEM "Linux b355e0901ee6 5.15.167.4-microsoft-standard-WSL2 #1 SMP Tue Nov 5 00:21:55 UTC 2024 x86_64 GNU/Linux"
"#
        )
        .generate_cstr(true);

    let bindings = builder.generate().expect("Unable to generate bindings");

    bindings
        .write_to_file("bindings.rs")
        .expect("Couldn't write bindings!");
}

resulted in:

/* automatically generated by rust-bindgen 0.72.1 */

pub const PHP_OS: &::std::ffi::CStr = c"Linux";
pub const PHP_BUILD_SYSTEM : & :: std :: ffi :: CStr = c"Linux b355e0901ee6 5.15.167.4-microsoft-standard-WSL2 #1 SMP Tue Nov 5 00:21:55 UTC 2024 x86_64 GNU/Linux" ;

the spacing of & :: std :: ffi :: CStr for the second string looks odd, especially since the first string was done with &::std::ffi::CStr

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the issue with the provided Builder::header_contents and generate_cstr(true) example, then inspect the generate_cstr output path used by Builder::generate. Compare the spacing of the short and long generated CStr declarations; done means equivalent declarations use consistent formatting without changing the generated values.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.