Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions bindgen-cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub fn main() {
env_logger::init();

match builder_from_flags(env::args()) {
Ok((builder, output, verbose)) => {
Ok((builder, mut output, verbose)) => {
#[cfg(feature = "logging")]
clang_version_check();

Expand All @@ -50,7 +50,7 @@ pub fn main() {

let _ = std::panic::take_hook();

bindings.write(output).expect("Unable to write output");
bindings.write(&mut output).expect("Unable to write output");
}
Err(error) => {
eprintln!("{error}");
Expand Down
20 changes: 3 additions & 17 deletions bindgen-tests/tests/parse_callbacks/add_derives_callback/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,11 @@ mod tests {
}
}

struct WriteAdapter<'a>(&'a mut Vec<u8>);

impl std::io::Write for WriteAdapter<'_> {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
self.0.extend_from_slice(buf);
Ok(buf.len())
}
fn flush(&mut self) -> std::io::Result<()> {
Ok(())
}
}

fn write_bindings_to_string(bindings: &Bindings) -> String {
let mut output = Vec::<u8>::new();
bindings
.write(Box::new(WriteAdapter(&mut output)))
.unwrap_or_else(|e| {
panic!("Failed to write generated bindings: {e}")
});
bindings.write(&mut output).unwrap_or_else(|e| {
panic!("Failed to write generated bindings: {e}")
});
String::from_utf8(output).unwrap_or_else(|e| {
panic!("Failed to convert generated bindings to string: {e}")
})
Expand Down
8 changes: 4 additions & 4 deletions bindgen/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -927,17 +927,17 @@ impl Bindings {

/// Write these bindings as source text to a file.
pub fn write_to_file<P: AsRef<Path>>(&self, path: P) -> io::Result<()> {
let file = OpenOptions::new()
let mut file = OpenOptions::new()
.write(true)
.truncate(true)
.create(true)
.open(path.as_ref())?;
self.write(Box::new(file))?;
self.write(&mut file)?;
Ok(())
}

/// Write these bindings as source text to the given `Write`able.
pub fn write<'a>(&self, mut writer: Box<dyn Write + 'a>) -> io::Result<()> {
pub fn write(&self, writer: &mut dyn Write) -> io::Result<()> {
const NL: &str = if cfg!(windows) { "\r\n" } else { "\n" };

if !self.options.disable_header_comment {
Expand Down Expand Up @@ -1090,7 +1090,7 @@ fn rustfmt_non_fatal_error_diagnostic(msg: &str, _options: &BindgenOptions) {
impl std::fmt::Display for Bindings {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let mut bytes = vec![];
self.write(Box::new(&mut bytes) as Box<dyn Write>)
self.write(&mut bytes)
.expect("writing to a vec cannot fail");
f.write_str(
std::str::from_utf8(&bytes)
Expand Down