Skip to content

Commit 8bf8372

Browse files
committed
change: replace MAX const param with field
1 parent c4390e0 commit 8bf8372

File tree

2 files changed

+60
-26
lines changed

2 files changed

+60
-26
lines changed

.github/workflows/commit-message-check.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,14 @@ jobs:
2121
excludeDescription: 'true' # optional: this excludes the description body of a pull request
2222
excludeTitle: 'true' # optional: this excludes the title of a pull request
2323
accessToken: ${{ secrets.GITHUB_TOKEN }}
24-
pattern: '^(DataChange:|Change:|Feature:|Improve:|Perf:|Dep:|Doc:|Test:|CI:|Refactor:|Fix:|Fixdoc:|Fixup:|Merge|BumpVer:|Chore:|fix:|feat:|perf:|refactor:|test:|docs:|deps:|chore:|ci:|Build\(deps\):) .+$'
24+
pattern: '^(DataChange:|Change:|change:|Feature:|Improve:|Perf:|Dep:|Doc:|Test:|CI:|Refactor:|Fix:|Fixdoc:|Fixup:|Merge|BumpVer:|Chore:|fix:|feat:|perf:|refactor:|test:|docs:|deps:|chore:|ci:|Build\(deps\):) .+$'
2525
flags: 'gm'
2626
error: |
2727
Subject line has to contain a commit type, e.g.: "Change: blabla" or a merge commit e.g.: "Merge xxx".
2828
Valid types are:
2929
DataChange - Persistent data change
3030
Change - API breaking change
31+
change - API breaking change
3132
Feature - API compatible new feature
3233
feat - API compatible new feature
3334
Improve - Become better without functional changes

src/display_slice.rs

Lines changed: 58 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,48 @@ use std::fmt;
1616

1717
/// Implement `Display` for `&[T]` if T is `Display`.
1818
///
19-
/// It outputs at most `MAX` elements, excluding those from the 5th to the second-to-last one:
20-
/// - `DisplaySlice(&[1,2,3,4,5,6])` outputs: `"[1,2,3,4,...,6]"`.
21-
pub struct DisplaySlice<'a, T: fmt::Display, const MAX: usize = 5>(pub &'a [T]);
19+
/// It outputs at most `limit` elements, excluding those from the 5th to the second-to-last one:
20+
/// - `DisplaySlice{ slice: &[1,2,3,4,5,6], ...}` outputs: `"[1,2,3,4,...,6]"`.
21+
pub struct DisplaySlice<'a, T: fmt::Display> {
22+
slice: &'a [T],
23+
/// The maximum number of elements to display. by default, it is 5.
24+
limit: Option<usize>,
25+
}
26+
27+
impl<'a, T: fmt::Display> DisplaySlice<'a, T> {
28+
pub fn new(slice: &'a [T]) -> Self {
29+
Self { slice, limit: None }
30+
}
31+
32+
pub fn at_most(mut self, limit: Option<usize>) -> Self {
33+
self.limit = limit;
34+
self
35+
}
2236

23-
impl<T: fmt::Display, const MAX: usize> fmt::Display for DisplaySlice<'_, T, MAX> {
37+
pub fn limit(&self) -> usize {
38+
self.limit.unwrap_or(5)
39+
}
40+
}
41+
42+
impl<T: fmt::Display> fmt::Display for DisplaySlice<'_, T> {
2443
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
25-
let slice = self.0;
44+
let limit = self.limit();
45+
46+
if limit == 0 {
47+
return write!(f, "[..]");
48+
}
49+
50+
let slice = self.slice;
2651
let len = slice.len();
2752

2853
write!(f, "[")?;
2954

30-
if len > MAX {
31-
for (i, t) in slice[..(MAX - 1)].iter().enumerate() {
32-
if i > 0 {
33-
write!(f, ",")?;
34-
}
35-
36-
write!(f, "{}", t)?;
55+
if len > limit {
56+
for t in slice[..(limit - 1)].iter() {
57+
write!(f, "{},", t)?;
3758
}
3859

39-
write!(f, ",..,")?;
60+
write!(f, "..,")?;
4061
write!(f, "{}", slice.last().unwrap())?;
4162
} else {
4263
for (i, t) in slice.iter().enumerate() {
@@ -68,41 +89,53 @@ impl<T: fmt::Display, const MAX: usize> fmt::Display for DisplaySlice<'_, T, MAX
6889
pub trait DisplaySliceExt<'a, T: fmt::Display> {
6990
fn display(&'a self) -> DisplaySlice<'a, T>;
7091

71-
/// Display at most `MAX` elements.
72-
fn display_n<const MAX: usize>(&'a self) -> DisplaySlice<'a, T, MAX>;
92+
/// Display at most `n` elements.
93+
fn display_n(&'a self, n: usize) -> DisplaySlice<'a, T> {
94+
self.display().at_most(Some(n))
95+
}
7396
}
7497

7598
impl<T> DisplaySliceExt<'_, T> for [T]
7699
where T: fmt::Display
77100
{
78101
fn display(&self) -> DisplaySlice<T> {
79-
DisplaySlice(self)
80-
}
81-
82-
fn display_n<const MAX: usize>(&'_ self) -> DisplaySlice<'_, T, MAX> {
83-
DisplaySlice(self)
102+
DisplaySlice::new(self)
84103
}
85104
}
86105

87106
#[cfg(test)]
88107
mod tests {
89108
use super::DisplaySlice;
109+
use crate::DisplaySliceExt;
90110

91111
#[test]
92112
fn test_display_slice() {
93113
let a = vec![1, 2, 3, 4];
94-
assert_eq!("[1,2,3,4]", DisplaySlice::<_>(&a).to_string());
114+
assert_eq!("[1,2,3,4]", DisplaySlice::new(&a).to_string());
95115

96116
let a = vec![1, 2, 3, 4, 5];
97-
assert_eq!("[1,2,3,4,5]", DisplaySlice::<_>(&a).to_string());
117+
assert_eq!("[1,2,3,4,5]", DisplaySlice::new(&a).to_string());
98118

99119
let a = vec![1, 2, 3, 4, 5, 6];
100-
assert_eq!("[1,2,3,4,..,6]", DisplaySlice::<_>(&a).to_string());
120+
assert_eq!("[1,2,3,4,..,6]", DisplaySlice::new(&a).to_string());
101121

102122
let a = vec![1, 2, 3, 4, 5, 6, 7];
103-
assert_eq!("[1,2,3,4,..,7]", DisplaySlice::<_>(&a).to_string());
123+
assert_eq!("[1,2,3,4,..,7]", DisplaySlice::new(&a).to_string());
124+
125+
// with limit
104126

105127
let a = vec![1, 2, 3, 4, 5, 6, 7];
106-
assert_eq!("[1,..,7]", DisplaySlice::<_, 2>(&a).to_string());
128+
assert_eq!(
129+
"[1,..,7]",
130+
DisplaySlice::new(&a).at_most(Some(2)).to_string()
131+
);
132+
133+
assert_eq!("[1,..,7]", a.display().at_most(Some(2)).to_string());
134+
135+
assert_eq!("[1,..,7]", a.display_n(2).to_string());
136+
137+
assert_eq!("[..,7]", a.display_n(1).to_string());
138+
139+
assert_eq!("[..]", a.display_n(0).to_string());
107140
}
108141
}

0 commit comments

Comments
 (0)