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
use crate::{bounds_for, boxed::BoxedString, inline::InlineString, SmartString, SmartStringMode};
use alloc::string::Drain as StringDrain;
use core::{
    fmt::{Debug, Error, Formatter},
    iter::FusedIterator,
    ops::RangeBounds,
    str::Chars,
};

/// A draining iterator for a [`SmartString`][SmartString].
///
/// [SmartString]: struct.SmartString.html
pub struct Drain<'a, Mode>(DrainCast<'a, Mode>)
where
    Mode: SmartStringMode;

enum DrainCast<'a, Mode>
where
    Mode: SmartStringMode,
{
    Boxed {
        string: *mut SmartString<Mode>,
        iter: Option<StringDrain<'a>>,
    },
    Inline {
        string: *mut InlineString<Mode>,
        start: usize,
        end: usize,
        iter: Chars<'a>,
    },
}

impl<'a, Mode> Drain<'a, Mode>
where
    Mode: SmartStringMode,
{
    pub(crate) fn new<R>(string: &'a mut SmartString<Mode>, range: R) -> Self
    where
        R: RangeBounds<usize>,
    {
        let string_ptr: *mut _ = string;
        Drain(match string.cast_mut() {
            crate::casts::StringCastMut::Boxed(boxed) => DrainCast::Boxed {
                string: string_ptr,
                iter: Some(boxed.string_mut().drain(range)),
            },
            crate::casts::StringCastMut::Inline(inline) => {
                let len = inline.len();
                let (start, end) = bounds_for(&range, len);
                let string_ptr: *mut _ = inline;
                let iter = inline.as_str()[start..end].chars();
                DrainCast::Inline {
                    string: string_ptr,
                    start,
                    end,
                    iter,
                }
            }
        })
    }
}

impl<'a, Mode> Drop for Drain<'a, Mode>
where
    Mode: SmartStringMode,
{
    fn drop(&mut self) {
        match self.0 {
            DrainCast::Boxed {
                string,
                ref mut iter,
            } => unsafe {
                iter.take();
                (*string).try_demote();
            },
            DrainCast::Inline {
                string, start, end, ..
            } => {
                unsafe { (*string).remove_bytes(start, end) };
            }
        }
    }
}

impl<'a, Mode> Iterator for Drain<'a, Mode>
where
    Mode: SmartStringMode,
{
    type Item = char;

    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        match &mut self.0 {
            DrainCast::Boxed {
                iter: Some(iter), ..
            } => iter.next(),
            DrainCast::Boxed { iter: None, .. } => unreachable!(),
            DrainCast::Inline { iter, .. } => iter.next(),
        }
    }

    #[inline]
    fn size_hint(&self) -> (usize, Option<usize>) {
        match &self.0 {
            DrainCast::Boxed {
                iter: Some(iter), ..
            } => iter.size_hint(),
            DrainCast::Boxed { iter: None, .. } => unreachable!(),
            DrainCast::Inline { iter, .. } => iter.size_hint(),
        }
    }

    #[inline]
    fn last(mut self) -> Option<Self::Item> {
        match &mut self.0 {
            DrainCast::Boxed {
                iter: Some(iter), ..
            } => iter.next_back(),
            DrainCast::Boxed { iter: None, .. } => unreachable!(),
            DrainCast::Inline { iter, .. } => iter.next_back(),
        }
    }
}

impl<'a, Mode> DoubleEndedIterator for Drain<'a, Mode>
where
    Mode: SmartStringMode,
{
    #[inline]
    fn next_back(&mut self) -> Option<Self::Item> {
        match &mut self.0 {
            DrainCast::Boxed {
                iter: Some(iter), ..
            } => iter.next_back(),
            DrainCast::Boxed { iter: None, .. } => unreachable!(),
            DrainCast::Inline { iter, .. } => iter.next_back(),
        }
    }
}

impl<'a, Mode> FusedIterator for Drain<'a, Mode> where Mode: SmartStringMode {}

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