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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#![no_std]

//! A writable, fixed-length `char` buffer usable in a `no_std` environment.
//!
//! ```
//! use char_buf::CharBuf;
//! use core::fmt::Write;
//!
//! // `CharBuf` with capacity `8`
//! type CharBuf8 = CharBuf<8>;
//!
//! let mut w = CharBuf8::new();
//! write!(w, "x{:?}x", [1, 2]).unwrap();
//!
//! assert_eq!(w, "x[1, 2]x");
//! ```

use core::fmt::{self, Write};

/// A writable, fixed-length `char` buffer.
pub struct CharBuf<const N: usize> {
    buf: [char; N],
    len: usize,
}

impl<const N: usize> CharBuf<N> {
    /// Constructs a new, empty `CharBuf<N>`.
    pub const fn new() -> Self {
        CharBuf {
            buf: ['\0'; N],
            len: 0,
        }
    }
}

impl<const N: usize> Default for CharBuf<N> {
    fn default() -> Self {
        CharBuf::new()
    }
}

impl<const N: usize> fmt::Debug for CharBuf<N> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_char('"')?;
        for c in self.buf.split_at(self.len).0.iter() {
            f.write_char(*c)?;
        }
        f.write_char('"')
    }
}

impl<const N: usize> fmt::Write for CharBuf<N> {
    /// Writes a string slice into this writer, returning whether the write succeeded. [Read more]
    ///
    /// # Errors
    ///
    /// This function will return an instance of [`Error`] if the buffer length is not enough to write `s`.
    ///
    /// [Read more]: https://doc.rust-lang.org/core/fmt/trait.Write.html#tymethod.write_str
    /// [`Error`]: https://doc.rust-lang.org/core/fmt/struct.Error.html
    fn write_str(&mut self, s: &str) -> fmt::Result {
        let mut chars = s.chars();
        self.buf
            .split_at_mut(self.len)
            .1
            .iter_mut()
            .zip(chars.by_ref())
            .for_each(|(x, c)| {
                *x = c;
                self.len += 1;
            });
        if chars.next().is_some() {
            Err(fmt::Error)
        } else {
            Ok(())
        }
    }
}

impl<const N1: usize, const N2: usize> PartialEq<CharBuf<N2>> for CharBuf<N1> {
    fn eq(&self, other: &CharBuf<N2>) -> bool {
        self.len == other.len
            && self
                .buf
                .split_at(self.len)
                .0
                .iter()
                .zip(other.buf.split_at(other.len).0.iter())
                .all(|(x, y)| x == y)
    }
}

impl<const N: usize> Eq for CharBuf<N> {}

impl<const N: usize> PartialEq<str> for CharBuf<N> {
    fn eq(&self, other: &str) -> bool {
        let mut buf = self.buf.split_at(self.len).0.iter();
        let mut chars = other.chars();
        buf.by_ref().zip(chars.by_ref()).all(|(x, c)| *x == c)
            && buf.next().is_none()
            && chars.next().is_none()
    }
}

impl<const N: usize> PartialEq<&str> for CharBuf<N> {
    fn eq(&self, other: &&str) -> bool {
        *self == **other
    }
}

impl<const N: usize> PartialEq<CharBuf<N>> for str {
    fn eq(&self, other: &CharBuf<N>) -> bool {
        *other == *self
    }
}

impl<const N: usize> PartialEq<CharBuf<N>> for &str {
    fn eq(&self, other: &CharBuf<N>) -> bool {
        *other == **self
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use fmt::Write;

    const N1: usize = 3;
    const N2: usize = 5;
    type CharBufN1 = CharBuf<N1>;
    type CharBufN2 = CharBuf<N2>;

    #[test]
    fn new() {
        let w1 = CharBufN1::new();
        let w2 = CharBufN2::new();

        assert_eq!(w1.buf, ['\0'; N1]);
        assert_eq!(w1.len, 0);

        assert_eq!(w2.buf, ['\0'; N2]);
        assert_eq!(w2.len, 0);
    }

    #[test]
    fn default() {
        let w1 = CharBufN1::default();
        let w2 = CharBufN2::default();

        assert_eq!(w1.buf, ['\0'; N1]);
        assert_eq!(w1.len, 0);

        assert_eq!(w2.buf, ['\0'; N2]);
        assert_eq!(w2.len, 0);
    }

    #[test]
    fn debug() {
        let mut w1a = CharBufN1::new();
        let mut w1b = CharBufN1::new();

        let mut w2a = CharBufN2::new();
        let mut w2b = CharBufN2::new();

        write!(w1a, "a").unwrap();
        write!(w1b, "{:?}", w1a).unwrap();

        assert_eq!(w1a.buf, ['a', '\0', '\0']);
        assert_eq!(w1b.buf, ['"', 'a', '"']);

        write!(w2a, "a").unwrap();
        write!(w2b, "{:?}", w2a).unwrap();

        assert_eq!(w2a.buf, ['a', '\0', '\0', '\0', '\0']);
        assert_eq!(w2b.buf, ['"', 'a', '"', '\0', '\0']);
    }

    #[test]
    fn write() {
        let mut w1 = CharBufN1::new();
        let mut w2 = CharBufN2::new();

        write!(w1, "a").unwrap();
        write!(w2, "a").unwrap();

        assert_eq!(w1.buf, ['a', '\0', '\0']);
        assert_eq!(w1.len, 1);

        assert_eq!(w2.buf, ['a', '\0', '\0', '\0', '\0']);
        assert_eq!(w2.len, 1);

        write!(w1, "{:?}", [(); 0]).unwrap();
        write!(w2, "{:?}", [(); 0]).unwrap();

        assert_eq!(w1.buf, ['a', '[', ']']);
        assert_eq!(w1.len, 3);

        assert_eq!(w2.buf, ['a', '[', ']', '\0', '\0']);
        assert_eq!(w2.len, 3);

        let mut w1 = CharBufN1::new();
        let mut w2 = CharBufN2::new();

        write!(w1, "{:?}", [0]).unwrap();
        write!(w2, "{:?}", [0]).unwrap();

        assert_eq!(w1.buf, ['[', '0', ']']);
        assert_eq!(w1.len, 3);

        assert_eq!(w2.buf, ['[', '0', ']', '\0', '\0']);
        assert_eq!(w2.len, 3);

        assert_eq!(write!(w1, "!"), Err(fmt::Error));
        assert_eq!(write!(w2, "!"), Ok(()));

        assert_eq!(w1.buf, ['[', '0', ']']);
        assert_eq!(w1.len, 3);

        assert_eq!(w2.buf, ['[', '0', ']', '!', '\0']);
        assert_eq!(w2.len, 4);
    }

    #[test]
    fn eq() {
        let mut w1 = CharBufN1::new();
        let mut w2 = CharBufN2::new();

        write!(w1, "ab").unwrap();
        write!(w2, "ab").unwrap();

        assert_eq!(w1, "ab");
        assert_eq!(w1, *"ab");

        assert_eq!(w2, "ab");
        assert_eq!(w2, *"ab");

        assert_eq!(w1, w2);
        assert_eq!(w2, w1);

        write!(w2, "c").unwrap();

        assert_eq!("ab", w1);
        assert_eq!(*"ab", w1);

        assert_eq!("abc", w2);
        assert_eq!(*"abc", w2);

        assert_ne!(w1, w2);
        assert_ne!(w2, w1);
    }
}