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)
| 40 | Any bits that aren't part of a contained flag will be formatted as a hex number. |
| 41 | */ |
| 42 | pub fn to_writer<B: Flags>(flags: &B, mut writer: impl Write) -> Result<(), fmt::Error> |
| 43 | where |
| 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")] |
| 81 | pub(crate) struct AsDisplay<'a, B>(pub(crate) &'a B); |