visdom/mesdoc/interface/
texts.rs1
2use super::BoxDynText;
3
4pub struct Texts<'a> {
5 nodes: Vec<BoxDynText<'a>>,
6}
7
8impl<'a> Texts<'a> {
9 pub fn with_capacity(cap: usize) -> Self {
10 Texts {
11 nodes: Vec::with_capacity(cap),
12 }
13 }
14 pub fn length(&self) -> usize {
15 self.nodes.len()
16 }
17 pub fn is_empty(&self) -> bool {
18 self.length() == 0
19 }
20 pub fn get_ref(&self) -> &Vec<BoxDynText<'a>> {
22 &self.nodes
23 }
24 pub fn get_mut_ref(&mut self) -> &mut Vec<BoxDynText<'a>> {
26 &mut self.nodes
27 }
28 pub fn for_each<F>(&mut self, mut handle: F) -> &mut Self
30 where
31 F: FnMut(usize, &mut BoxDynText) -> bool,
32 {
33 for (index, ele) in self.get_mut_ref().iter_mut().enumerate() {
34 if !handle(index, ele) {
35 break;
36 }
37 }
38 self
39 }
40
41 pub fn each<F>(&mut self, handle: F) -> &mut Self
43 where
44 F: FnMut(usize, &mut BoxDynText) -> bool,
45 {
46 self.for_each(handle)
47 }
48
49 pub fn filter_by<F>(&self, handle: F) -> Texts<'a>
51 where
52 F: Fn(usize, &BoxDynText) -> bool,
53 {
54 let mut result: Texts = Texts::with_capacity(self.length());
55 for (index, ele) in self.get_ref().iter().enumerate() {
56 if handle(index, ele) {
57 result.get_mut_ref().push(
58 ele
59 .clone_node()
60 .typed()
61 .into_text()
62 .expect("Text ele must can use 'into_text'."),
63 );
64 }
65 }
66 result
67 }
68 pub fn remove(self) {
70 for ele in self.into_iter() {
71 ele.remove();
72 }
73 }
74}
75
76impl<'a> IntoIterator for Texts<'a> {
77 type Item = BoxDynText<'a>;
78 type IntoIter = Box<dyn Iterator<Item = Self::Item> + 'a>;
79 fn into_iter(self) -> Self::IntoIter {
80 Box::new(self.nodes.into_iter())
81 }
82}