Skip to content

Commit 09e1c87

Browse files
preyasshahnical
authored andcommitted
cargo fmt
1 parent 0c88cc8 commit 09e1c87

File tree

5 files changed

+67
-37
lines changed

5 files changed

+67
-37
lines changed

counters/src/counters.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use std::io;
2-
use std::collections::HashMap;
31
use std::cell::RefCell;
2+
use std::collections::HashMap;
3+
use std::io;
44

55
use crate::filters::Filter;
66

@@ -11,7 +11,7 @@ use crate::filters::Filter;
1111
#[derive(Clone, Debug)]
1212
#[cfg_attr(feature = "serialization", derive(Serialize, Deserialize))]
1313
pub struct Counters {
14-
events: RefCell<HashMap<String, u64>>
14+
events: RefCell<HashMap<String, u64>>,
1515
}
1616

1717
impl Counters {
@@ -38,7 +38,9 @@ impl Counters {
3838

3939
/// Reset some of the counters to zero.
4040
pub fn reset_events<F: Filter>(&self, mut filter: F) {
41-
self.events.borrow_mut().retain(|key, val| !filter.apply(key, *val));
41+
self.events
42+
.borrow_mut()
43+
.retain(|key, val| !filter.apply(key, *val));
4244
}
4345

4446
/// Reset all counters to zero.
@@ -48,7 +50,9 @@ impl Counters {
4850

4951
/// Keep some of the counters and throw away the rest.
5052
pub fn retain<F: Filter>(&self, mut filter: F) {
51-
self.events.borrow_mut().retain(|key, val| filter.apply(key, *val));
53+
self.events
54+
.borrow_mut()
55+
.retain(|key, val| filter.apply(key, *val));
5256
}
5357

5458
/// Get the value of the counter or zero if it does not exist.
@@ -80,7 +84,6 @@ impl Counters {
8084
Ok(())
8185
}
8286

83-
8487
/// Print the counters to stdout.
8588
pub fn print_to_stdout<F: Filter>(&self, filter: F) {
8689
let stdout = io::stdout();
@@ -93,5 +96,5 @@ impl Counters {
9396
for (key, value) in other.events.borrow_mut().drain() {
9497
*self.events.borrow_mut().entry(key).or_insert(0) += value;
9598
}
96-
}
99+
}
97100
}

counters/src/filters.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
21
pub trait Filter {
32
fn apply(&mut self, key: &str, value: u64) -> bool;
43
}
54

65
/// Accept all keys
76
pub struct All;
87
impl Filter for All {
9-
fn apply(&mut self, _: &str, _: u64) -> bool { true }
8+
fn apply(&mut self, _: &str, _: u64) -> bool {
9+
true
10+
}
1011
}
1112

1213
/// Select keys using a callback.

counters/src/lib.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,20 @@
8181
#[cfg(feature = "serialization")]
8282
extern crate serde;
8383

84-
#[cfg(not(feature="noop"))] mod counters;
85-
#[cfg(not(feature="noop"))] pub use crate::counters::*;
84+
#[cfg(not(feature = "noop"))]
85+
mod counters;
86+
#[cfg(not(feature = "noop"))]
87+
pub use crate::counters::*;
8688

87-
#[cfg(not(feature="noop"))] mod table;
88-
#[cfg(not(feature="noop"))] pub use crate::table::*;
89+
#[cfg(not(feature = "noop"))]
90+
mod table;
91+
#[cfg(not(feature = "noop"))]
92+
pub use crate::table::*;
8993

90-
#[cfg(feature="noop")] mod noop;
91-
#[cfg(feature="noop")] pub use crate::noop::*;
94+
#[cfg(feature = "noop")]
95+
mod noop;
96+
#[cfg(feature = "noop")]
97+
pub use crate::noop::*;
9298

9399
pub mod filters;
94100

@@ -155,5 +161,5 @@ fn noop() {
155161

156162
assert_eq!(counters.get("foo::bar"), 0);
157163
assert_eq!(counters.get("foo::baz"), 0);
158-
assert_eq!(counters.accumulate("foo::"), 0);
164+
assert_eq!(counters.accumulate("foo::"), 0);
159165
}

counters/src/noop.rs

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,46 @@
1-
use std::io;
21
use crate::filters::Filter;
2+
use std::io;
33

44
#[derive(Clone, Debug)]
55
#[cfg_attr(feature = "serialization", derive(Serialize, Deserialize))]
66
pub struct Counters;
77

88
impl Counters {
9-
pub fn new() -> Self { Counters }
9+
pub fn new() -> Self {
10+
Counters
11+
}
1012
pub fn event(&self, _key: &str) {}
1113
pub fn reset_event(&self, _key: &str) {}
1214
pub fn reset_events<F: Filter>(&self, _filter: F) {}
1315
pub fn reset_all(&self) {}
1416
pub fn retain<F: Filter>(&self, _filter: F) {}
15-
pub fn get(&self, _key: &str) -> u64 { 0 }
16-
pub fn accumulate<F: Filter>(&self, _filter: F) -> u64 { 0 }
17-
pub fn print<F: Filter>(&self, _filter: F, _out: &mut io::Write) -> io::Result<()> { Ok(()) }
17+
pub fn get(&self, _key: &str) -> u64 {
18+
0
19+
}
20+
pub fn accumulate<F: Filter>(&self, _filter: F) -> u64 {
21+
0
22+
}
23+
pub fn print<F: Filter>(&self, _filter: F, _out: &mut io::Write) -> io::Result<()> {
24+
Ok(())
25+
}
1826
pub fn print_to_stdout<F: Filter>(&self, _filter: F) {}
1927
}
2028

2129
#[cfg_attr(feature = "serialization", derive(Serialize, Deserialize))]
2230
pub struct Table;
2331

2432
impl Table {
25-
pub fn new<Label>(_labels: &[Label]) -> Self where Label: ToString { Table }
26-
pub fn add_row(&self, _row: &Counters) -> usize { 0 }
27-
pub fn print(&self, _to: &mut io::Write) -> io::Result<()> { Ok(()) }
33+
pub fn new<Label>(_labels: &[Label]) -> Self
34+
where
35+
Label: ToString,
36+
{
37+
Table
38+
}
39+
pub fn add_row(&self, _row: &Counters) -> usize {
40+
0
41+
}
42+
pub fn print(&self, _to: &mut io::Write) -> io::Result<()> {
43+
Ok(())
44+
}
2845
pub fn print_to_stdout(&self) {}
2946
}

counters/src/table.rs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
1-
use crate::Counters;
21
use crate::filters::Select;
3-
use std::io;
2+
use crate::Counters;
43
use std::cell::RefCell;
4+
use std::io;
55

66
/// Helper to print counters as a table in csv format.
77
///
88
/// # Example
99
///
1010
/// ```
1111
/// use counters::*;
12-
///
12+
///
1313
/// let counters = Counters::new();
1414
/// let table = Table::new(&["foo", "bar", "meh"]);
15-
///
15+
///
1616
/// for _ in 0..5 {
1717
/// counters.event("bar");
1818
/// }
1919
/// counters.event("foo");
20-
///
20+
///
2121
/// // "baz" isn't in the table labels, it will be ignored.
2222
/// counters.event("baz");
23-
///
23+
///
2424
/// table.add_row(&counters);
25-
///
25+
///
2626
/// // Start a second row...
2727
/// counters.reset_all();
28-
///
28+
///
2929
/// counters.event("foo");
30-
///
30+
///
3131
/// table.add_row(&counters);
3232
///
3333
/// // This prints the following to stdout:
@@ -45,7 +45,9 @@ pub struct Table {
4545

4646
impl Table {
4747
pub fn new<Label>(labels: &[Label]) -> Self
48-
where Label: ToString {
48+
where
49+
Label: ToString,
50+
{
4951
Table {
5052
labels: labels.iter().map(|label| label.to_string()).collect(),
5153
rows: RefCell::new(Vec::new()),
@@ -55,7 +57,9 @@ impl Table {
5557
/// Add collected counters as a row, preserving only the counters that match this table's labels.
5658
pub fn add_row(&self, row: &Counters) -> usize {
5759
let row = row.clone();
58-
row.retain(Select(|key, _| self.labels.iter().any(|label| label == key) ));
60+
row.retain(Select(|key, _| {
61+
self.labels.iter().any(|label| label == key)
62+
}));
5963
self.rows.borrow_mut().push(row);
6064

6165
self.rows.borrow().len()
@@ -71,10 +75,9 @@ impl Table {
7175
}
7276
writeln!(to, "")?;
7377
for row in self.rows.borrow().iter() {
74-
7578
for (i, label) in self.labels.iter().enumerate() {
7679
if i != 0 {
77-
write!(to, ", ")?;
80+
write!(to, ", ")?;
7881
}
7982
write!(to, "{}", row.get(label))?;
8083
}

0 commit comments

Comments
 (0)