-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
44 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
use std::fmt::{Display, Formatter}; | ||
|
||
pub struct DisplaySlice<'a, D: Display> { | ||
items: &'a [D] | ||
} | ||
impl<'a, D: Display> Display for DisplaySlice<'a, D> where D: Display { | ||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { | ||
write!(f, "[")?; | ||
let len = self.items.len(); | ||
for (index, item) in self.items.iter().enumerate() { | ||
write!(f, "{item}")?; | ||
if index != len - 1 { | ||
write!(f, ", ")?; | ||
} | ||
} | ||
write!(f, "]")?; | ||
Ok(()) | ||
} | ||
} | ||
|
||
impl<'a, D: Display> From<&'a [D]> for DisplaySlice<'a, D> { | ||
fn from(items: &'a [D]) -> Self { | ||
DisplaySlice { items } | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn display_slice() { | ||
let data = vec![4, 5, 6, 8]; | ||
assert_eq!("[4, 5, 6, 8]", format!("{}", DisplaySlice::from(&*data))); | ||
|
||
let data: Vec<usize> = vec![]; | ||
assert_eq!("[]", format!("{}", DisplaySlice::from(&*data))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
|
||
pub mod capture; | ||
pub mod data; | ||
pub mod display; | ||
pub mod linear; | ||
pub mod mc; | ||
pub mod opt; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters