spinoso_string/enc/
inspect.rs

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
use core::iter::FusedIterator;

use super::{ascii, binary, utf8};

#[derive(Default, Debug, Clone)]
#[must_use = "this `Inspect` is an `Iterator`, which should be consumed if constructed"]
pub struct Inspect<'a>(State<'a>);

impl<'a> From<&'a ascii::AsciiString> for Inspect<'a> {
    #[inline]
    fn from(value: &'a ascii::AsciiString) -> Self {
        Self(State::Ascii(value.into()))
    }
}

impl<'a> From<&'a binary::BinaryString> for Inspect<'a> {
    #[inline]
    fn from(value: &'a binary::BinaryString) -> Self {
        Self(State::Binary(value.into()))
    }
}

impl<'a> From<&'a utf8::Utf8String> for Inspect<'a> {
    #[inline]
    fn from(value: &'a utf8::Utf8String) -> Self {
        Self(State::Utf8(value.into()))
    }
}

#[derive(Debug, Clone)]
enum State<'a> {
    Ascii(ascii::Inspect<'a>),
    Binary(binary::Inspect<'a>),
    Utf8(utf8::Inspect<'a>),
}

impl Default for State<'_> {
    /// Construct a `State` that will render debug output for the empty slice.
    ///
    /// This constructor produces inspect contents like `""`.
    #[inline]
    fn default() -> Self {
        Self::Ascii(ascii::Inspect::default())
    }
}

impl Iterator for Inspect<'_> {
    type Item = char;

    fn next(&mut self) -> Option<Self::Item> {
        match &mut self.0 {
            State::Ascii(iter) => iter.next(),
            State::Binary(iter) => iter.next(),
            State::Utf8(iter) => iter.next(),
        }
    }
}

impl FusedIterator for Inspect<'_> {}