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
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109

//! Convenience methods for working with arrays.

/// Implemented by arrays of different lengths.
pub trait Array<T> {
    /// Creates array from a function of each component index.
    fn from_fn<F>(f: F) -> Self where F: FnMut(usize) -> T;

    /// Creates an array from an iterator.
    /// Will fail if the iterator does not contain enough elements.
    fn from_iter<I: Iterator<Item=T>>(mut iter: I) -> Self where Self: Sized {
        Array::from_fn(|_| { iter.next().unwrap() })
    }
}

impl<T> Array<T> for [T; 2] {
    fn from_fn<F>(mut f: F) -> [T; 2] where F: FnMut(usize) -> T {
        [f(0), f(1)]
    }
}

/// An array with 2 components.
pub trait Array2<T> {
    /// Converts array into another type,
    /// by executing a function for each component.
    fn map<U, F>(self, f: F) -> [U; 2] where F: Fn(T) -> U;
    /// Returns the `x` component.
    fn x(self) -> T;
    /// Returns the `y` component.
    fn y(self) -> T;
}

impl<T: Copy> Array2<T> for [T; 2] {
    fn map<U, F>(self, f: F) -> [U; 2] where F: Fn(T) -> U {
        [f(self[0]), f(self[1])]
    }
    fn x(self) -> T { self[0] }
    fn y(self) -> T { self[1] }
}

impl<T> Array<T> for [T; 3] {
    fn from_fn<F>(mut f: F) -> [T; 3] where F: FnMut(usize) -> T {
        [f(0), f(1), f(2)]
    }
}

/// An array with 3 components.
pub trait Array3<T> {
    /// Converts array into another type,
    /// by executing a function for each component.
    fn map<U, F>(self, f: F) -> [U; 3] where F: Fn(T) -> U;
    /// Returns the `x` component.
    fn x(self) -> T;
    /// Returns the `y` component.
    fn y(self) -> T;
    /// Returns the `z` component.
    fn z(self) -> T;
}

impl<T: Copy> Array3<T> for [T; 3] {
    fn map<U, F>(self, f: F) -> [U; 3] where F: Fn(T) -> U {
        [f(self[0]), f(self[1]), f(self[2])]
    }
    fn x(self) -> T { self[0] }
    fn y(self) -> T { self[1] }
    fn z(self) -> T { self[2] }
}

impl<T> Array<T> for [T; 4] {
    fn from_fn<F>(mut f: F) -> [T; 4] where F: FnMut(usize) -> T {
        [f(0), f(1), f(2), f(3)]
    }
}

/// An array with 4 components.
pub trait Array4<T> {
    /// Converts array into another type,
    /// by executing a function for each component.
    fn map<U, F>(self, f: F) -> [U; 4] where F: Fn(T) -> U;
    /// Returns the `x` component.
    fn x(self) -> T;
    /// Returns the `y` component.
    fn y(self) -> T;
    /// Returns the `z` component.
    fn z(self) -> T;
    /// Returns the `w` component.
    fn w(self) -> T;
}

impl<T: Copy> Array4<T> for [T; 4] {
    fn map<U, F>(self, f: F) -> [U; 4] where F: Fn(T) -> U {
        [f(self[0]), f(self[1]), f(self[2]), f(self[3])]
    }
    fn x(self) -> T { self[0] }
    fn y(self) -> T { self[1] }
    fn z(self) -> T { self[2] }
    fn w(self) -> T { self[3] }
}

impl<T> Array<T> for [T; 16] {
    fn from_fn<F>(mut f: F) -> [T; 16] where F: FnMut(usize) -> T {
        [
            f(0), f(1), f(2), f(3),
            f(4), f(5), f(6), f(7),
            f(8), f(9), f(10),f(11),
            f(12),f(13),f(14),f(15)
        ]
    }
}