rust_3d/impls/
vec.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 Vec
24
25use crate::*;
26
27//------------------------------------------------------------------------------
28
29impl<T> IsRandomAccessible<T> for Vec<T> {
30    fn len(&self) -> usize {
31        self.len()
32    }
33}
34
35impl<T> IsRandomInsertible<T> for Vec<T> {
36    fn insert(&mut self, index: usize, x: T) -> Result<()> {
37        if index >= self.len() {
38            return Err(ErrorKind::IndexOutOfBounds);
39        }
40        self.insert(index, x);
41        Ok(())
42    }
43}
44
45impl<T> IsPushable<T> for Vec<T> {
46    fn push(&mut self, x: T) {
47        self.push(x)
48    }
49
50    fn reserve(&mut self, n: usize) {
51        self.reserve(n)
52    }
53}
54
55impl<T> IsViewBuildable for Vec<T>
56where
57    T: Clone,
58{
59    fn apply_view(&mut self, view: &View) -> Result<()> {
60        match view {
61            View::Full => Ok(()),
62            View::Restricted(indices) => {
63                let n = self.len();
64                if indices.iter().any(|x| x >= &n) {
65                    return Err(ErrorKind::IndexOutOfBounds);
66                }
67                let mut new_data = Vec::new();
68                for (i, p) in self.iter().enumerate() {
69                    if indices.contains(&i) {
70                        new_data.push(p.clone());
71                    }
72                }
73                *self = new_data;
74                Ok(())
75            }
76        }
77    }
78
79    fn from_view(&self, view: &View) -> Result<Self> {
80        let mut cloned = self.clone();
81        cloned.apply_view(view)?;
82        Ok(cloned)
83    }
84}
85
86impl<T> IsMovable2D for Vec<T>
87where
88    T: IsMovable2D,
89{
90    fn move_by(&mut self, x: f64, y: f64) {
91        for ref mut p in self.iter_mut() {
92            p.move_by(x, y);
93        }
94    }
95}
96
97impl<T> IsMovable3D for Vec<T>
98where
99    T: IsMovable3D,
100{
101    fn move_by(&mut self, x: f64, y: f64, z: f64) {
102        for ref mut p in self.iter_mut() {
103            p.move_by(x, y, z);
104        }
105    }
106}
107
108impl<HB> IsColliderContainer3D for Vec<HB>
109where
110    HB: HasColliders3D,
111{
112    fn any_element_collides_with_collider(&self, other: &dyn HasColliders3D) -> bool {
113        self.iter().any(|candidate| candidate.collides_with(other))
114    }
115
116    fn any_element_collides_with_bounding(&self, other: &dyn HasBoundingBox3D) -> bool {
117        self.iter().any(|candidate| {
118            other
119                .bounding_box()
120                .collides_with(&candidate.bounding_box())
121        })
122    }
123}
124
125impl IsIndexContainer for Vec<usize> {
126    fn ensure_supported(&mut self, _x: usize) {
127        // always supported
128    }
129
130    fn reserve(&mut self, n: usize) {
131        self.reserve(n)
132    }
133
134    fn len(&self) -> usize {
135        self.len()
136    }
137
138    fn get(&self, index: usize) -> usize {
139        self[index]
140    }
141
142    fn set(&mut self, index: usize, value: usize) {
143        self[index] = value
144    }
145
146    fn push(&mut self, value: usize) {
147        self.push(value)
148    }
149
150    fn iter(&self) -> IsIndexContainerIterator<Self> {
151        IsIndexContainerIterator::new(self)
152    }
153}