rust_3d/impls/
vec_deque.rs1use std::collections::VecDeque;
26
27use crate::*;
28
29impl<T> IsRandomAccessible<T> for VecDeque<T> {
32 fn len(&self) -> usize {
33 self.len()
34 }
35}
36
37impl<T> IsRandomInsertible<T> for VecDeque<T> {
38 fn insert(&mut self, index: usize, x: T) -> Result<()> {
39 if index >= self.len() {
40 return Err(ErrorKind::IndexOutOfBounds);
41 }
42 self.insert(index, x);
43 Ok(())
44 }
45}
46
47impl<T> IsPushable<T> for VecDeque<T> {
48 fn push(&mut self, x: T) {
49 self.push_back(x)
50 }
51 fn reserve(&mut self, n: usize) {
52 self.reserve(n)
53 }
54}
55
56impl<T> IsViewBuildable for VecDeque<T>
57where
58 T: Clone,
59{
60 fn apply_view(&mut self, view: &View) -> Result<()> {
61 match view {
62 View::Full => Ok(()),
63 View::Restricted(indices) => {
64 let n = self.len();
65 if indices.iter().any(|x| x >= &n) {
66 return Err(ErrorKind::IndexOutOfBounds);
67 }
68 let mut new_data = VecDeque::new();
69 for (i, p) in self.iter().enumerate() {
70 if indices.contains(&i) {
71 new_data.push(p.clone());
72 }
73 }
74 *self = new_data;
75 Ok(())
76 }
77 }
78 }
79
80 fn from_view(&self, view: &View) -> Result<Self> {
81 let mut cloned = self.clone();
82 cloned.apply_view(view)?;
83 Ok(cloned)
84 }
85}
86
87impl<T> IsMovable2D for VecDeque<T>
88where
89 T: IsMovable2D,
90{
91 fn move_by(&mut self, x: f64, y: f64) {
92 for ref mut p in self.iter_mut() {
93 p.move_by(x, y);
94 }
95 }
96}
97
98impl<T> IsMovable3D for VecDeque<T>
99where
100 T: IsMovable3D,
101{
102 fn move_by(&mut self, x: f64, y: f64, z: f64) {
103 for ref mut p in self.iter_mut() {
104 p.move_by(x, y, z);
105 }
106 }
107}
108
109impl<HB> IsColliderContainer3D for VecDeque<HB>
110where
111 HB: HasColliders3D,
112{
113 fn any_element_collides_with_collider(&self, other: &dyn HasColliders3D) -> bool {
114 self.iter().any(|candidate| candidate.collides_with(other))
115 }
116
117 fn any_element_collides_with_bounding(&self, other: &dyn HasBoundingBox3D) -> bool {
118 self.iter().any(|candidate| {
119 other
120 .bounding_box()
121 .collides_with(&candidate.bounding_box())
122 })
123 }
124}