-
Notifications
You must be signed in to change notification settings - Fork 198
Expand file tree
/
Copy pathdisplay.rs
More file actions
113 lines (102 loc) · 2.91 KB
/
display.rs
File metadata and controls
113 lines (102 loc) · 2.91 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
//! Display implementation for `Batch`.
use std::fmt;
use std::fmt::Formatter;
use super::RaftBatch;
use crate::OptionalSend;
/// Display helper for types implementing `RaftBatch`.
pub struct DisplayBatch<'a, T, B>
where
T: fmt::Display + OptionalSend + 'static + fmt::Debug,
B: RaftBatch<T>,
{
pub(super) elements: &'a B,
pub(super) max: Option<usize>,
pub(super) _phantom: std::marker::PhantomData<T>,
}
impl<'a, T, B> DisplayBatch<'a, T, B>
where
T: fmt::Display + OptionalSend + 'static + fmt::Debug,
B: RaftBatch<T>,
{
pub(super) fn new(elements: &'a B, max: Option<usize>) -> Self {
Self {
elements,
max,
_phantom: std::marker::PhantomData,
}
}
}
impl<'a, T, B> fmt::Display for DisplayBatch<'a, T, B>
where
T: fmt::Display + OptionalSend + 'static + fmt::Debug,
B: RaftBatch<T>,
{
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
let len = self.elements.len();
let max = self.max.unwrap_or(len);
let shown = max.min(len);
write!(f, "[")?;
for (i, e) in self.elements.iter().take(max).enumerate() {
if i > 0 {
write!(f, ", ")?;
}
write!(f, "{}", e)?;
}
if len > max {
if shown > 0 {
write!(f, ", ")?;
}
write!(f, "... {} more", len - max)?;
}
write!(f, "]")
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::impls::Batch;
#[test]
fn test_display() {
assert_eq!(
format!("{}", <Batch<i32> as RaftBatch<i32>>::display(&Batch::Single(42))),
"[42]"
);
assert_eq!(
format!("{}", <Batch<i32> as RaftBatch<i32>>::display(&Batch::Vec(vec![1, 2]))),
"[1, 2]"
);
assert_eq!(
format!(
"{}",
<Batch<i32> as RaftBatch<i32>>::display(&Batch::<i32>::Vec(vec![]))
),
"[]"
);
}
#[test]
fn test_display_n() {
let v: Batch<i32> = [1, 2, 3, 4, 5].into();
assert_eq!(
format!("{}", <Batch<i32> as RaftBatch<i32>>::display_n(&v, 3)),
"[1, 2, 3, ... 2 more]"
);
assert_eq!(
format!("{}", <Batch<i32> as RaftBatch<i32>>::display_n(&v, 5)),
"[1, 2, 3, 4, 5]"
);
assert_eq!(
format!("{}", <Batch<i32> as RaftBatch<i32>>::display_n(&v, 10)),
"[1, 2, 3, 4, 5]"
);
assert_eq!(
format!("{}", <Batch<i32> as RaftBatch<i32>>::display_n(&v, 0)),
"[... 5 more]"
);
let v2: Batch<i32> = 42.into();
assert_eq!(
format!("{}", <Batch<i32> as RaftBatch<i32>>::display_n(&v2, 0)),
"[... 1 more]"
);
assert_eq!(format!("{}", <Batch<i32> as RaftBatch<i32>>::display_n(&v2, 1)), "[42]");
}
}