MCPcopy Create free account
hub / github.com/bitflags/bitflags / to_writer

Function to_writer

src/parser.rs:42–78  ·  view source on GitHub ↗

Write a flags value as text. Any bits that aren't part of a contained flag will be formatted as a hex number. */

(flags: &B, mut writer: impl Write)

Source from the content-addressed store, hash-verified

40Any bits that aren't part of a contained flag will be formatted as a hex number.
41*/
42pub fn to_writer<B: Flags>(flags: &B, mut writer: impl Write) -> Result<(), fmt::Error>
43where
44 B::Bits: WriteHex,
45{
46 // A formatter for bitflags that produces text output like:
47 //
48 // A | B | 0xf6
49 //
50 // The names of set flags are written in a bar-separated-format,
51 // followed by a hex number of any remaining bits that are set
52 // but don't correspond to any flags.
53
54 // Iterate over known flag values
55 let mut first = true;
56 let mut iter = flags.iter_names();
57 for (name, _) in &mut iter {
58 if !first {
59 writer.write_str(" | ")?;
60 }
61
62 first = false;
63 writer.write_str(name)?;
64 }
65
66 // Append any extra bits that correspond to flags to the end of the format
67 let remaining = iter.remaining().bits();
68 if remaining != B::Bits::EMPTY {
69 if !first {
70 writer.write_str(" | ")?;
71 }
72
73 writer.write_str("0x")?;
74 remaining.write_hex(writer)?;
75 }
76
77 fmt::Result::Ok(())
78}
79
80#[cfg(feature = "serde")]
81pub(crate) struct AsDisplay<'a, B>(pub(crate) &'a B);

Callers 6

fmtMethod · 0.85
to_writer_truncateFunction · 0.85
roundtripFunction · 0.85
writeFunction · 0.85
fmtMethod · 0.85
fmtMethod · 0.85

Calls 5

OkClass · 0.85
iter_namesMethod · 0.80
remainingMethod · 0.80
write_hexMethod · 0.80
bitsMethod · 0.45

Tested by 2

roundtripFunction · 0.68
writeFunction · 0.68