spinoso_string/enc/
inspect.rs

1use core::iter::FusedIterator;
2
3use super::{ascii, binary, utf8};
4
5#[derive(Default, Debug, Clone)]
6#[must_use = "this `Inspect` is an `Iterator`, which should be consumed if constructed"]
7pub struct Inspect<'a>(State<'a>);
8
9impl<'a> From<&'a ascii::AsciiString> for Inspect<'a> {
10    #[inline]
11    fn from(value: &'a ascii::AsciiString) -> Self {
12        Self(State::Ascii(value.into()))
13    }
14}
15
16impl<'a> From<&'a binary::BinaryString> for Inspect<'a> {
17    #[inline]
18    fn from(value: &'a binary::BinaryString) -> Self {
19        Self(State::Binary(value.into()))
20    }
21}
22
23impl<'a> From<&'a utf8::Utf8String> for Inspect<'a> {
24    #[inline]
25    fn from(value: &'a utf8::Utf8String) -> Self {
26        Self(State::Utf8(value.into()))
27    }
28}
29
30#[derive(Debug, Clone)]
31enum State<'a> {
32    Ascii(ascii::Inspect<'a>),
33    Binary(binary::Inspect<'a>),
34    Utf8(utf8::Inspect<'a>),
35}
36
37impl Default for State<'_> {
38    /// Construct a `State` that will render debug output for the empty slice.
39    ///
40    /// This constructor produces inspect contents like `""`.
41    #[inline]
42    fn default() -> Self {
43        Self::Ascii(ascii::Inspect::default())
44    }
45}
46
47impl Iterator for Inspect<'_> {
48    type Item = char;
49
50    fn next(&mut self) -> Option<Self::Item> {
51        match &mut self.0 {
52            State::Ascii(iter) => iter.next(),
53            State::Binary(iter) => iter.next(),
54            State::Utf8(iter) => iter.next(),
55        }
56    }
57}
58
59impl FusedIterator for Inspect<'_> {}