1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
use std::marker::PhantomData;
use linear::{Vec2, Vec3};
use numeric::Zero;
pub trait Bounds {
type Index : Copy;
fn in_bounds (&self, index: Self::Index) -> bool;
}
pub trait IsUnbounded : Bounds {}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct Unbounded<I> {
_phantom: PhantomData<I>,
}
impl<I> Unbounded<I> {
pub fn new () -> Unbounded<I> { Unbounded { _phantom: PhantomData } }
}
impl<I: Copy> Bounds for Unbounded<I> {
type Index = I;
fn in_bounds (&self, _: I) -> bool { true }
}
impl<I: Copy> IsUnbounded for Unbounded<I> {}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct Size1<T> (pub T);
impl<T> Size1<T> {
pub fn len (&self) -> T where T: Copy { self.0 }
}
impl<T: Copy + PartialOrd + Zero> Bounds for Size1<T> {
type Index = T;
fn in_bounds (&self, index: T) -> bool {
index >= T::zero() && index < self.0
}
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct Size2<T> (pub Vec2<T>);
impl<T> Size2<T> {
pub fn height (&self) -> T where T: Copy { self.0.y() }
pub fn width (&self) -> T where T: Copy { self.0.x() }
}
impl<T: Copy + PartialOrd + Zero> Bounds for Size2<T> {
type Index = Vec2<T>;
fn in_bounds (&self, index: Vec2<T>) -> bool {
let zero = T::zero();
index.x() >= zero && index.x() < self.0.x() && index.y() >= zero && index.y() < self.0.y()
}
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct Size3<T> (pub Vec3<T>);
impl<T> Size3<T> {
pub fn depth (&self) -> T where T: Copy { self.0.z() }
pub fn height (&self) -> T where T: Copy { self.0.y() }
pub fn width (&self) -> T where T: Copy { self.0.x() }
}
impl<T: Copy + PartialOrd + Zero> Bounds for Size3<T> {
type Index = Vec3<T>;
fn in_bounds (&self, index: Vec3<T>) -> bool {
let zero = T::zero();
index.x() >= zero && index.x() < self.0.x() && index.y() >= zero && index.y() < self.0.y()
&& index.z() >= zero && index.z() < self.0.z()
}
}