diff --git a/crates/libs/bindgen/src/rust/mod.rs b/crates/libs/bindgen/src/rust/mod.rs index 6b917612bf2..6e6116bb567 100644 --- a/crates/libs/bindgen/src/rust/mod.rs +++ b/crates/libs/bindgen/src/rust/mod.rs @@ -38,6 +38,11 @@ pub fn from_reader( writer.no_inner_attributes = config.remove("no-inner-attributes").is_some(); writer.no_bindgen_comment = config.remove("no-bindgen-comment").is_some(); writer.vtbl = config.remove("vtbl").is_some(); + writer.rustfmt_config = if let Some(config) = config.remove("rustfmt-config") { + config.to_string() + } else { + String::new() + }; if writer.package && writer.flatten { return Err(Error::new( diff --git a/crates/libs/bindgen/src/rust/try_format.rs b/crates/libs/bindgen/src/rust/try_format.rs index b5ac27c414b..d5aeeef4235 100644 --- a/crates/libs/bindgen/src/rust/try_format.rs +++ b/crates/libs/bindgen/src/rust/try_format.rs @@ -23,12 +23,17 @@ pub fn try_format(writer: &super::Writer, tokens: &str) -> String { }; let tokens = format!("{preamble}{allow}{tokens}"); - let Ok(mut child) = std::process::Command::new("rustfmt") - .stdin(std::process::Stdio::piped()) - .stdout(std::process::Stdio::piped()) - .stderr(std::process::Stdio::null()) - .spawn() - else { + let mut cmd = std::process::Command::new("rustfmt"); + cmd.stdin(std::process::Stdio::piped()); + cmd.stdout(std::process::Stdio::piped()); + cmd.stderr(std::process::Stdio::null()); + + if !writer.rustfmt_config.is_empty() { + cmd.arg("--config"); + cmd.arg(writer.rustfmt_config.as_str()); + } + + let Ok(mut child) = cmd.spawn() else { return tokens; }; diff --git a/crates/libs/bindgen/src/rust/writer.rs b/crates/libs/bindgen/src/rust/writer.rs index 4f6e8a9f04a..e6e6189eee6 100644 --- a/crates/libs/bindgen/src/rust/writer.rs +++ b/crates/libs/bindgen/src/rust/writer.rs @@ -22,6 +22,8 @@ pub struct Writer { pub no_bindgen_comment: bool, // skips the bindgen comment at the start of the file pub vtbl: bool, // include minimal vtbl layout support for interfaces pub prepend: std::collections::HashMap, + /// If this is not empty, then it is passed to rustfmt in a `--config` argument. + pub rustfmt_config: String, } impl Writer { @@ -40,6 +42,7 @@ impl Writer { no_bindgen_comment: false, vtbl: false, prepend: Default::default(), + rustfmt_config: String::new(), } } diff --git a/crates/tools/sys/bindings.txt b/crates/tools/sys/bindings.txt index 56db292a402..7f6a6b5dfa8 100644 --- a/crates/tools/sys/bindings.txt +++ b/crates/tools/sys/bindings.txt @@ -1,6 +1,7 @@ --in crates/libs/bindgen/default --out crates/libs/sys/src/lib.rs --config package sys no-bindgen-comment +--config rustfmt-config=max_width=800,newline_style=Unix --filter Windows.Wdk diff --git a/crates/tools/windows/bindings.txt b/crates/tools/windows/bindings.txt index c998ffab1b9..3cd700917cd 100644 --- a/crates/tools/windows/bindings.txt +++ b/crates/tools/windows/bindings.txt @@ -5,6 +5,7 @@ --config prepend:Windows.Win32.Foundation.BOOL=#[must_use] --config prepend:Windows.Win32.Foundation.NTSTATUS=#[must_use] --config prepend:Windows.Win32.System.Rpc.RPC_STATUS=#[must_use] +--config rustfmt-config=max_width=800,newline_style=Unix --filter Windows