rust_3d/impls/
linked_list.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 LinkedList
24
25use std::collections::LinkedList;
26
27use crate::*;
28
29//------------------------------------------------------------------------------
30
31impl<T> IsViewBuildable for LinkedList<T>
32where
33    T: Clone,
34{
35    fn apply_view(&mut self, view: &View) -> Result<()> {
36        match view {
37            View::Full => Ok(()),
38            View::Restricted(indices) => {
39                let n = self.len();
40                if indices.iter().any(|x| x >= &n) {
41                    return Err(ErrorKind::IndexOutOfBounds);
42                }
43                let mut new_data = LinkedList::new();
44                for (i, p) in self.iter().enumerate() {
45                    if indices.contains(&i) {
46                        new_data.push_back(p.clone());
47                    }
48                }
49                *self = new_data;
50                Ok(())
51            }
52        }
53    }
54
55    fn from_view(&self, view: &View) -> Result<Self> {
56        let mut cloned = self.clone();
57        cloned.apply_view(view)?;
58        Ok(cloned)
59    }
60}
61
62impl<T> IsMovable2D for LinkedList<T>
63where
64    T: IsMovable2D,
65{
66    fn move_by(&mut self, x: f64, y: f64) {
67        for ref mut p in self.iter_mut() {
68            p.move_by(x, y);
69        }
70    }
71}
72
73impl<T> IsMovable3D for LinkedList<T>
74where
75    T: IsMovable3D,
76{
77    fn move_by(&mut self, x: f64, y: f64, z: f64) {
78        for ref mut p in self.iter_mut() {
79            p.move_by(x, y, z);
80        }
81    }
82}
83
84impl<HB> IsColliderContainer3D for LinkedList<HB>
85where
86    HB: HasColliders3D,
87{
88    fn any_element_collides_with_collider(&self, other: &dyn HasColliders3D) -> bool {
89        self.iter().any(|candidate| candidate.collides_with(other))
90    }
91
92    fn any_element_collides_with_bounding(&self, other: &dyn HasBoundingBox3D) -> bool {
93        self.iter().any(|candidate| {
94            other
95                .bounding_box()
96                .collides_with(&candidate.bounding_box())
97        })
98    }
99}