Skip to content

Commit 6decf66

Browse files
committed
Don't require a Box wrapper on Bindings::write()
The function in question doesn't store the `dyn Write` trait object anywhere, allowing us to pass a plain `&mut dyn Write` (which is also implemented for `&mut Box<dyn Write>` allowing the existing CLI parser to operate normally). This in turn no longer requires users of `bindgen` to wrap their `&mut File` or `&mut Vec<u8>` in a moved `Box` just to be able to give temporary mutable access to the `write() function`.
1 parent 5813198 commit 6decf66

File tree

3 files changed

+9
-23
lines changed

3 files changed

+9
-23
lines changed

bindgen-cli/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pub fn main() {
2929
env_logger::init();
3030

3131
match builder_from_flags(env::args()) {
32-
Ok((builder, output, verbose)) => {
32+
Ok((builder, mut output, verbose)) => {
3333
#[cfg(feature = "logging")]
3434
clang_version_check();
3535

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

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

53-
bindings.write(output).expect("Unable to write output");
53+
bindings.write(&mut output).expect("Unable to write output");
5454
}
5555
Err(error) => {
5656
eprintln!("{error}");

bindgen-tests/tests/parse_callbacks/add_derives_callback/mod.rs

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,11 @@ mod tests {
1919
}
2020
}
2121

22-
struct WriteAdapter<'a>(&'a mut Vec<u8>);
23-
24-
impl std::io::Write for WriteAdapter<'_> {
25-
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
26-
self.0.extend_from_slice(buf);
27-
Ok(buf.len())
28-
}
29-
fn flush(&mut self) -> std::io::Result<()> {
30-
Ok(())
31-
}
32-
}
33-
3422
fn write_bindings_to_string(bindings: &Bindings) -> String {
3523
let mut output = Vec::<u8>::new();
36-
bindings
37-
.write(Box::new(WriteAdapter(&mut output)))
38-
.unwrap_or_else(|e| {
39-
panic!("Failed to write generated bindings: {e}")
40-
});
24+
bindings.write(&mut output).unwrap_or_else(|e| {
25+
panic!("Failed to write generated bindings: {e}")
26+
});
4127
String::from_utf8(output).unwrap_or_else(|e| {
4228
panic!("Failed to convert generated bindings to string: {e}")
4329
})

bindgen/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -927,17 +927,17 @@ impl Bindings {
927927

928928
/// Write these bindings as source text to a file.
929929
pub fn write_to_file<P: AsRef<Path>>(&self, path: P) -> io::Result<()> {
930-
let file = OpenOptions::new()
930+
let mut file = OpenOptions::new()
931931
.write(true)
932932
.truncate(true)
933933
.create(true)
934934
.open(path.as_ref())?;
935-
self.write(Box::new(file))?;
935+
self.write(&mut file)?;
936936
Ok(())
937937
}
938938

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

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

0 commit comments

Comments
 (0)