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

/////////////////////////////////////////////////////////////////////////////

/// An Iterator created by `<RString as IntoIterator>::into_iter`,
/// which yields all the characters from the RString,
/// consuming it in the process.
pub struct IntoIter {
    pub(super) _buf: RString,
    pub(super) iter: Chars<'static>,
}

impl IntoIter {
    pub fn as_str(&self) -> &str {
        self.iter.as_str()
    }
}

impl Iterator for IntoIter {
    type Item = char;

    #[inline]
    fn next(&mut self) -> Option<char> {
        self.iter.next()
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        self.iter.size_hint()
    }
}

impl DoubleEndedIterator for IntoIter {
    #[inline]
    fn next_back(&mut self) -> Option<char> {
        self.iter.next_back()
    }
}

impl FusedIterator for IntoIter {}

/////////////////////////////////////////////////////////////////////////////

/// An Iterator returned by `RString::drain` ,
/// which removes and yields all the characters in a range from the RString.
pub struct Drain<'a> {
    pub(super) string: *mut RString,
    pub(super) removed: Range<usize>,
    pub(super) iter: Chars<'a>,
    pub(super) variance:PhantomData<&'a mut [char]>,
}

impl<'a> fmt::Debug for Drain<'a> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.pad("Drain { .. }")
    }
}

unsafe impl<'a> Sync for Drain<'a> {}
unsafe impl<'a> Send for Drain<'a> {}

impl<'a> Drain<'a> {
    pub fn as_str(&self) -> &str {
        self.iter.as_str()
    }
}

impl<'a> Drop for Drain<'a> {
    fn drop(&mut self) {
        unsafe {
            let self_vec = &mut (*self.string).inner;
            if self.removed.start <= self.removed.end && self.removed.end <= self_vec.len() {
                self_vec.drain(self.removed.start..self.removed.end);
            }
        }
    }
}

impl<'a> Iterator for Drain<'a> {
    type Item = char;

    #[inline]
    fn next(&mut self) -> Option<char> {
        self.iter.next()
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        self.iter.size_hint()
    }
}

impl<'a> DoubleEndedIterator for Drain<'a> {
    #[inline]
    fn next_back(&mut self) -> Option<char> {
        self.iter.next_back()
    }
}

impl<'a> FusedIterator for Drain<'a> {}