rust_3d/impls/
vec_deque.rs

1/*
2Copyright 2017 Martin Buck
3
4Permission is hereby granted, free of charge, to any person obtaining a copy
5of this software and associated documentation files (the "Software"),
6to deal in the Software without restriction, including without limitation the
7rights to use, copy, modify, merge, publish, distribute, sublicense,
8and/or sell copies of the Software, and to permit persons to whom the Software
9is furnished to do so, subject to the following conditions:
10
11The above copyright notice and this permission notice shall
12be included all copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
20OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21*/
22
23//! rust-3d trait implementations for the standard VecDeque
24
25use std::collections::VecDeque;
26
27use crate::*;
28
29//------------------------------------------------------------------------------
30
31impl<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}