Bindgen add a line at the top of the the file

Hello,

I would like to add a line at the top of the file while using Bindgen.

How can I do that ? Bindgen seems to overwrite the whole file.

I would like to add #[allow(clippy::all)] at the top of the file.

Thank you very much in advance for any help.

are you using bindgen in the build script? assuming you are using Bindings::write_to_file(), you can manually create the file and write the header contents to it, then pass the file to Bindings::write() instead.

// build.rs

let bindings = bindgen::Builder::new().header("wrapper.h").generate().unwrap();
let mut out = File::create("path/to/bindings/file").unwrap();
writeln!(&mut out, "#![allow(clippy::all)]");
bindings.write(Box::new(out)).unwrap();

btw, it is common practice that to use include!() to pull the generated code into the crate (as opposed to generating the bindings directly in src directory). then you can simpy add the clippy directive to e.g. lib.rs, something like:

// lib.rs
#![allow(cippy::all)]

include!(concat!(env!("OUT_DIR"), "/bindings.rs"));

Indeed, I'm using it in the build.rs script.

I wrote this:

let bindgens = bindgen::Builder::default()
            .header(self.sqlite_3_h_path.display().to_string())
            .generate()
            .expect("bindgen failed");

        let mut file_bindgen = std::fs::File::create(&self.bindgen_out_path).unwrap();
        writeln!(&mut file_bindgen, "#![allow(clippy::all)]");
        bindgens.write(&mut file_bindgen).unwrap();

And I got this error:

error[E0308]: mismatched types
   --> sqlite-sys/build.rs:70:24
    |
 70 |         bindgens.write(&mut file_bindgen).unwrap();
    |                  ----- ^^^^^^^^^^^^^^^^^ expected `Box<dyn Write>`, found `&mut File`
    |                  |
    |                  arguments to this method are incorrect
    |
    = note:         expected struct `Box<dyn std::io::Write>`
            found mutable reference `&mut File`
    = note: for more on the distinction between the stack and the heap, read https://doc.rust-lang.org/book/ch15-01-box.html, https://doc.rust-lang.org/rust-by-example/std/box.html, and https://doc.rust-lang.org/std/boxed/index.html
note: method defined here
   --> /home/coredump/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/bindgen-0.72.1/lib.rs:947:12
    |
947 |     pub fn write<'a>(&self, mut writer: Box<dyn Write + 'a>) -> io::Result<()> {
    |            ^^^^^
help: store this in the heap by calling `Box::new`
    |
 70 |         bindgens.write(Box::new(&mut file_bindgen)).unwrap();
    |                        +++++++++                 +

For more information about this error, try `rustc --explain E0308`.
error: could not compile `sqlite-sys` (build script) due to 1 previous error

Thank you very much in advance for any help

whoopsie, I thought the parameter type was a reference, but it actually is a Box. to fix this error, you can box the file directly, or alternatively, box the mut reference, as suggested by the compiler, both should work:

// option 1: box the file directly, this consumes the file
bindgens.write(Box::new(file_bindgen)).unwrap();

// option 2: box the reference of the file
// use this when you still want to insert code after the generated bindings
bindgens.write(Box::new(&mut file_bindgen)).unwrap();
writeln!(&mut file_bindgen, "// code after the auto generated bindings");

Thank you.

With your solution I now get the expected result.