]>
git.proxmox.com Git - rustc.git/blob - src/tools/rustfmt/config_proc_macro/src/utils.rs
1 use proc_macro2
::TokenStream
;
2 use quote
::{quote, ToTokens}
;
4 pub fn fold_quote
<F
, I
, T
>(input
: impl Iterator
<Item
= I
>, f
: F
) -> TokenStream
9 input
.fold(quote
! {}
, |acc
, x
| {
15 pub fn is_unit(v
: &syn
::Variant
) -> bool
{
17 syn
::Fields
::Unit
=> true,
22 #[cfg(feature = "debug-with-rustfmt")]
23 /// Pretty-print the output of proc macro using rustfmt.
24 pub fn debug_with_rustfmt(input
: &TokenStream
) {
28 use std
::process
::{Command, Stdio}
;
30 let rustfmt_var
= env
::var_os("RUSTFMT");
31 let rustfmt
= match &rustfmt_var
{
32 Some(rustfmt
) => rustfmt
,
33 None
=> OsStr
::new("rustfmt"),
35 let mut child
= Command
::new(rustfmt
)
36 .stdin(Stdio
::piped())
37 .stdout(Stdio
::piped())
39 .expect("Failed to spawn rustfmt in stdio mode");
41 let stdin
= child
.stdin
.as_mut().expect("Failed to get stdin");
43 .write_all(format
!("{}", input
).as_bytes())
44 .expect("Failed to write to stdin");
46 let rustfmt_output
= child
.wait_with_output().expect("rustfmt has failed");
50 String
::from_utf8(rustfmt_output
.stdout
).expect("rustfmt returned non-UTF8 string")