1#![feature(test)]
2
3extern crate test;
4
5use std::{
6 fmt::{self, Display},
7 str::FromStr,
8};
9
10bitflags::bitflags! {
11 struct Flags10: u32 {
12 const A = 0b0000_0000_0000_0001;
13 const B = 0b0000_0000_0000_0010;
14 const C = 0b0000_0000_0000_0100;
15 const D = 0b0000_0000_0000_1000;
16 const E = 0b0000_0000_0001_0000;
17 const F = 0b0000_0000_0010_0000;
18 const G = 0b0000_0000_0100_0000;
19 const H = 0b0000_0000_1000_0000;
20 const I = 0b0000_0001_0000_0000;
21 const J = 0b0000_0010_0000_0000;
22 }
23}
24
25impl FromStr for Flags10 {
26 type Err = bitflags::parser::ParseError;
27
28 fn from_str(flags: &str) -> Result<Self, Self::Err> {
29 Ok(Flags10(flags.parse()?))
30 }
31}
32
33impl Display for Flags10 {
34 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
35 Display::fmt(&self.0, f)
36 }
37}
38
39#[bench]
40fn format_flags_1_present(b: &mut test::Bencher) {
41 b.iter(|| Flags10::J.to_string())
42}
43
44#[bench]
45fn format_flags_5_present(b: &mut test::Bencher) {
46 b.iter(|| (Flags10::F | Flags10::G | Flags10::H | Flags10::I | Flags10::J).to_string())
47}
48
49#[bench]
50fn format_flags_10_present(b: &mut test::Bencher) {
51 b.iter(|| {
52 (Flags10::A
53 | Flags10::B
54 | Flags10::C
55 | Flags10::D
56 | Flags10::E
57 | Flags10::F
58 | Flags10::G
59 | Flags10::H
60 | Flags10::I
61 | Flags10::J)
62 .to_string()
63 })
64}
65
66#[bench]
67fn parse_flags_1_10(b: &mut test::Bencher) {
68 b.iter(|| {
69 let flags: Flags10 = "J".parse().unwrap();
70 flags
71 })
72}
73
74#[bench]
75fn parse_flags_5_10(b: &mut test::Bencher) {
76 b.iter(|| {
77 let flags: Flags10 = "F | G | H | I | J".parse().unwrap();
78 flags
79 })
80}
81
82#[bench]
83fn parse_flags_10_10(b: &mut test::Bencher) {
84 b.iter(|| {
85 let flags: Flags10 = "A | B | C | D | E | F | G | H | I | J".parse().unwrap();
86 flags
87 })
88}
89
90#[bench]
91fn parse_flags_1_10_hex(b: &mut test::Bencher) {
92 b.iter(|| {
93 let flags: Flags10 = "0xFF".parse().unwrap();
94 flags
95 })
96}
97