Coverage Report

Created: 2025-02-25 06:39

/rust/registry/src/index.crates.io-6f17d22bba15001f/itertools-0.10.5/src/format.rs
Line
Count
Source (jump to first uncovered line)
1
use std::fmt;
2
use std::cell::RefCell;
3
4
/// Format all iterator elements lazily, separated by `sep`.
5
///
6
/// The format value can only be formatted once, after that the iterator is
7
/// exhausted.
8
///
9
/// See [`.format_with()`](crate::Itertools::format_with) for more information.
10
#[derive(Clone)]
11
pub struct FormatWith<'a, I, F> {
12
    sep: &'a str,
13
    /// FormatWith uses interior mutability because Display::fmt takes &self.
14
    inner: RefCell<Option<(I, F)>>,
15
}
16
17
/// Format all iterator elements lazily, separated by `sep`.
18
///
19
/// The format value can only be formatted once, after that the iterator is
20
/// exhausted.
21
///
22
/// See [`.format()`](crate::Itertools::format)
23
/// for more information.
24
#[derive(Clone)]
25
pub struct Format<'a, I> {
26
    sep: &'a str,
27
    /// Format uses interior mutability because Display::fmt takes &self.
28
    inner: RefCell<Option<I>>,
29
}
30
31
0
pub fn new_format<I, F>(iter: I, separator: &str, f: F) -> FormatWith<'_, I, F>
32
0
    where I: Iterator,
33
0
          F: FnMut(I::Item, &mut dyn FnMut(&dyn fmt::Display) -> fmt::Result) -> fmt::Result
34
0
{
35
0
    FormatWith {
36
0
        sep: separator,
37
0
        inner: RefCell::new(Some((iter, f))),
38
0
    }
39
0
}
40
41
0
pub fn new_format_default<I>(iter: I, separator: &str) -> Format<'_, I>
42
0
    where I: Iterator,
43
0
{
44
0
    Format {
45
0
        sep: separator,
46
0
        inner: RefCell::new(Some(iter)),
47
0
    }
48
0
}
49
50
impl<'a, I, F> fmt::Display for FormatWith<'a, I, F>
51
    where I: Iterator,
52
          F: FnMut(I::Item, &mut dyn  FnMut(&dyn fmt::Display) -> fmt::Result) -> fmt::Result
53
{
54
0
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
55
0
        let (mut iter, mut format) = match self.inner.borrow_mut().take() {
56
0
            Some(t) => t,
57
0
            None => panic!("FormatWith: was already formatted once"),
58
        };
59
60
0
        if let Some(fst) = iter.next() {
61
0
            format(fst, &mut |disp: &dyn fmt::Display| disp.fmt(f))?;
62
0
            iter.try_for_each(|elt| {
63
0
                if !self.sep.is_empty() {
64
0
                    f.write_str(self.sep)?;
65
0
                }
66
0
                format(elt, &mut |disp: &dyn fmt::Display| disp.fmt(f))
67
0
            })?;
68
0
        }
69
0
        Ok(())
70
0
    }
71
}
72
73
impl<'a, I> Format<'a, I>
74
    where I: Iterator,
75
{
76
0
    fn format<F>(&self, f: &mut fmt::Formatter, mut cb: F) -> fmt::Result
77
0
        where F: FnMut(&I::Item, &mut fmt::Formatter) -> fmt::Result,
78
0
    {
79
0
        let mut iter = match self.inner.borrow_mut().take() {
80
0
            Some(t) => t,
81
0
            None => panic!("Format: was already formatted once"),
82
        };
83
84
0
        if let Some(fst) = iter.next() {
85
0
            cb(&fst, f)?;
86
0
            iter.try_for_each(|elt| {
87
0
                if !self.sep.is_empty() {
88
0
                    f.write_str(self.sep)?;
89
0
                }
90
0
                cb(&elt, f)
91
0
            })?;
92
0
        }
93
0
        Ok(())
94
0
    }
95
}
96
97
macro_rules! impl_format {
98
    ($($fmt_trait:ident)*) => {
99
        $(
100
            impl<'a, I> fmt::$fmt_trait for Format<'a, I>
101
                where I: Iterator,
102
                      I::Item: fmt::$fmt_trait,
103
            {
104
0
                fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
105
0
                    self.format(f, fmt::$fmt_trait::fmt)
106
0
                }
Unexecuted instantiation: <itertools::format::Format<_> as core::fmt::Display>::fmt
Unexecuted instantiation: <itertools::format::Format<_> as core::fmt::Debug>::fmt
Unexecuted instantiation: <itertools::format::Format<_> as core::fmt::UpperExp>::fmt
Unexecuted instantiation: <itertools::format::Format<_> as core::fmt::LowerExp>::fmt
Unexecuted instantiation: <itertools::format::Format<_> as core::fmt::UpperHex>::fmt
Unexecuted instantiation: <itertools::format::Format<_> as core::fmt::LowerHex>::fmt
Unexecuted instantiation: <itertools::format::Format<_> as core::fmt::Octal>::fmt
Unexecuted instantiation: <itertools::format::Format<_> as core::fmt::Binary>::fmt
Unexecuted instantiation: <itertools::format::Format<_> as core::fmt::Pointer>::fmt
107
            }
108
        )*
109
    }
110
}
111
112
impl_format!{Display Debug
113
             UpperExp LowerExp UpperHex LowerHex Octal Binary Pointer}