1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
use core::{
cell::Cell,
fmt::{Debug, Formatter, Result as FmtResult, Write},
};
fn get_flags(f: &Formatter) -> u32 {
#[allow(deprecated)]
f.flags()
}
struct StoreFlags(Cell<u32>);
impl Debug for StoreFlags {
fn fmt(&self, f: &mut Formatter) -> FmtResult {
self.0.set(get_flags(f));
Ok(())
}
}
struct Discard;
impl Write for Discard {
fn write_str(&mut self, _s: &str) -> FmtResult {
Ok(())
}
}
pub enum IsDebugHex {
No,
Lower,
Upper,
}
pub fn is_debug_hex(f: &Formatter) -> IsDebugHex {
let flags = get_flags(f);
if flags == 0 {
return IsDebugHex::No;
}
let store_flags = StoreFlags(Cell::new(0));
if write!(Discard, "{:x?}", store_flags).is_err() {
return IsDebugHex::No;
}
let lower_flags = store_flags.0.get();
if write!(Discard, "{:X?}", store_flags).is_err() {
return IsDebugHex::No;
}
let upper_flags = store_flags.0.get();
let lower_mask = lower_flags & !upper_flags;
let upper_mask = upper_flags & !lower_flags;
if flags & lower_mask != 0 {
IsDebugHex::Lower
} else if flags & upper_mask != 0 {
IsDebugHex::Upper
} else {
IsDebugHex::No
}
}