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
use alloc::vec::Vec;

use super::{Chunks, Rope};

pub struct Chars<'a> {
  index: usize,
  current: Option<Vec<char>>,
  chunks: Chunks<'a>,
}

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

impl<'a> From<&'a Rope> for Chars<'a> {
  #[inline]
  fn from(rope: &'a Rope) -> Chars<'a> {
    let mut chunks = Chunks::from(rope);

    Chars {
      index: 0,
      current: chunks.next().map(|s| s.chars().collect()),
      chunks: chunks,
    }
  }
}

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

  #[inline]
  fn next(&mut self) -> Option<Self::Item> {
    if let Some(ref chars) = self.current {
      match chars.get(self.index) {
        Some(ch) => {
          self.index += 1;
          return Some(*ch);
        }
        None => {
          self.index = 0;
        }
      }
    } else {
      return None;
    }

    self.current = self.chunks.next().map(|s| s.chars().collect());
    self.next()
  }
}