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
use std::collections::LinkedList;
use prelude::*;
impl<T> IsViewBuildable for LinkedList<T> where
T: Clone {
fn apply_view(&mut self, view: &View) -> Result<()> {
match view {
&View::Full => { Ok(()) }
&View::Restricted(ref indices) => {
let n = self.len();
if indices.iter().any(|x| x >= &n) {
return Err(ErrorKind::IndexOutOfBounds);
}
let mut new_data = LinkedList::new();
for (i, p) in self.iter().enumerate() {
if indices.contains(&i) {
new_data.push_back(p.clone());
}
}
*self = new_data;
Ok(())
}
}
}
fn from_view(&self, view: &View) -> Result<Box<Self>> {
let mut cloned = self.clone();
cloned.apply_view(view)?;
Ok(Box::new(cloned))
}
}