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
//! It is a look-ahead chunk.  

//! 先読みチャンクです。  

use crate::LookAheadItems;
use std::fmt;

impl<T> LookAheadItems<T>
where
    T: std::clone::Clone,
{
    pub fn new(index: usize, items: &Vec<T>) -> Self {
        LookAheadItems {
            items: items.clone(),
            index: index,
        }
    }

    pub fn get(&self, index: usize) -> Option<&T> {
        if index < self.items.len() {
            return Some(&self.items[index]);
        }
        None
    }

    pub fn get_items(&self) -> &Vec<T> {
        &self.items
    }
}
impl<T> fmt::Display for LookAheadItems<T>
where
    T: std::fmt::Display + std::clone::Clone,
{
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let mut buf = String::new();
        for item in &self.items {
            buf.push_str(&format!("{}", item));
        }
        write!(f, "{}", buf)
    }
}
impl<T> fmt::Debug for LookAheadItems<T>
where
    T: std::fmt::Debug + std::clone::Clone,
{
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let mut buf = String::new();
        for item in &self.items {
            buf.push_str(&format!("{:?}", item));
        }
        write!(f, "{}", buf)
    }
}