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
use core::fmt::*;

pub(crate) mod types {
    #[allow(unused)]
    use super::*;

    /// See [`repeat()`].
    #[derive(Clone, Copy)]
    pub struct Repeat<T> {
        pub(super) value: T,
        pub(super) n: usize,
    }

    /// See [`repeat_with()`].
    #[derive(Clone, Copy)]
    pub struct RepeatWith<F> {
        // Although this could alias `Concat<iter::Take<iter::RepeatWith<F>>>`,
        // it would not be able to implement `Copy` because `Take` doesn't.
        pub(super) f: F,
        pub(super) n: usize,
    }
}

use types::*;

/// Repeats a value `n` times.
///
/// This is a non-allocating alternative to
/// [`[T]::repeat()`](https://doc.rust-lang.org/std/primitive.slice.html#method.repeat) or
/// [`str::repeat()`](https://doc.rust-lang.org/std/primitive.str.html#method.repeat).
///
/// # Examples
///
/// ```
/// let value = fmty::repeat("123", 3);
/// assert_eq!(value.to_string(), "123123123");
/// ```
pub fn repeat<T>(value: T, n: usize) -> Repeat<T> {
    Repeat { value, n }
}

/// Repeats `n` results of a closure.
///
/// # Examples
///
/// ```
/// use std::cell::Cell;
///
/// let counter = Cell::new(1);
///
/// let value = fmty::repeat_with(3, || {
///     let result = counter.get();
///     counter.set(result + 1);
///     result
/// });
///
/// assert_eq!(value.to_string(), "123");
/// ```
pub fn repeat_with<F>(n: usize, f: F) -> RepeatWith<F> {
    RepeatWith { n, f }
}

impl<T: Debug> Debug for Repeat<T> {
    fn fmt(&self, f: &mut Formatter) -> Result {
        for _ in 0..self.n {
            write!(f, "{:?}", self.value)?;
        }
        Ok(())
    }
}

impl<T: Display> Display for Repeat<T> {
    fn fmt(&self, f: &mut Formatter) -> Result {
        for _ in 0..self.n {
            write!(f, "{}", self.value)?;
        }
        Ok(())
    }
}

impl<F, R> Debug for RepeatWith<F>
where
    F: Fn() -> R,
    R: Debug,
{
    fn fmt(&self, f: &mut Formatter) -> Result {
        for _ in 0..self.n {
            write!(f, "{:?}", (self.f)())?;
        }
        Ok(())
    }
}

impl<F, R> Display for RepeatWith<F>
where
    F: Fn() -> R,
    R: Display,
{
    fn fmt(&self, f: &mut Formatter) -> Result {
        for _ in 0..self.n {
            write!(f, "{}", (self.f)())?;
        }
        Ok(())
    }
}