flv_util/
actions.rs

1//!
2//! # Actions Template
3//!
4//! Template to enable chaining actions in a list.
5//! Operations supported:
6//! * push, count compare, traverse(iter.)
7//!
8use std::collections::vec_deque::Iter;
9use std::collections::vec_deque::IntoIter;
10use std::collections::VecDeque;
11
12
13/// queue of Action
14#[derive(Debug,Clone)]
15pub struct Actions<T>(VecDeque<T>);
16
17
18impl<T> ::std::default::Default for Actions<T> {
19    fn default() -> Self {
20        Self(VecDeque::new())
21    }
22}
23
24
25impl<T> ::std::cmp::PartialEq for Actions<T>
26where
27    T: PartialEq,
28{
29    fn eq(&self, other: &Actions<T>) -> bool {
30        let local_actions = &self.0;
31        let other_actions = &other.0;
32
33        if local_actions.len() != other_actions.len() {
34            return false;
35        }
36        for (idx, action) in local_actions.iter().enumerate() {
37            if action != &other_actions[idx] {
38                return false;
39            }
40        }
41
42        true
43    }
44}
45
46impl<T> Actions<T> {
47
48    pub fn add_all(&mut self,items: Vec<T>) {
49        for item in items.into_iter() {
50            self.push(item);
51        }
52    }
53
54    pub fn push(&mut self, action: T) {
55        self.0.push_back(action);
56    }
57
58    pub fn push_once(&mut self, action: T)
59    where
60        T: PartialEq,
61    {
62        let mut found = false;
63        for local_action in self.0.iter() {
64            if local_action == &action {
65                found = true;
66                break;
67            }
68        }
69        if !found {
70            self.0.push_back(action);
71        }
72    }
73
74    pub fn pop_front(&mut self) -> Option<T> {
75        self.0.pop_front()
76    }
77
78
79    pub fn count(&self) -> usize {
80        self.0.len()
81    }
82
83    pub fn iter<'a>(&'a self) -> Iter<'a, T> {
84        self.0.iter()
85    }
86
87    pub fn into_iter(self) -> IntoIter<T> {
88        self.0.into_iter()
89    }
90}
91
92
93impl <T>From<Vec<T>> for Actions<T> {
94    fn from(vec: Vec<T>) -> Self {
95        let mut actions = Self::default();
96        for item in vec.into_iter() {
97            actions.push(item);
98        }
99        actions
100    }
101}