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
use core::slice::{Chunks, Iter, IterMut};
use core::{borrow, cmp, default, fmt, hash, ops, slice};

/// `Tile` is a tuple struct wrapping an 8x8 byte array:
/// conceptually a tile of SNES graphics.
///
/// It exists because Rust hates arrays larger than 32 --
/// downright hates 'em, I say! --
/// and denies them their rightful impls.

#[derive(Copy, Clone)]
pub struct Tile(pub [u8; 64]);

impl fmt::Debug for Tile {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        fmt::Debug::fmt(&self.0[..], f)
    }
}

impl<'a> IntoIterator for &'a Tile {
    type Item = &'a u8;
    type IntoIter = Iter<'a, u8>;

    fn into_iter(self) -> Iter<'a, u8> {
        self.0.iter()
    }
}

impl<'a> IntoIterator for &'a mut Tile {
    type Item = &'a mut u8;
    type IntoIter = IterMut<'a, u8>;

    fn into_iter(self) -> IterMut<'a, u8> {
        self.0.iter_mut()
    }
}

impl cmp::PartialEq for Tile {
    fn eq(&self, rhs: &Self) -> bool {
        cmp::PartialEq::eq(&self.0[..], &rhs.0[..])
    }
}

impl cmp::Eq for Tile {}

impl hash::Hash for Tile {
    fn hash<H: hash::Hasher>(&self, state: &mut H) {
        hash::Hash::hash(&self.0[..], state)
    }
}

impl AsRef<[u8]> for Tile {
    #[inline]
    fn as_ref(&self) -> &[u8] {
        &self.0[..]
    }
}

impl AsMut<[u8]> for Tile {
    #[inline]
    fn as_mut(&mut self) -> &mut [u8] {
        &mut self.0[..]
    }
}

impl borrow::Borrow<[u8]> for Tile {
    fn borrow(&self) -> &[u8] {
        &self.0
    }
}

impl borrow::BorrowMut<[u8]> for Tile {
    fn borrow_mut(&mut self) -> &mut [u8] {
        &mut self.0
    }
}

impl default::Default for Tile {
    fn default() -> Self {
        Tile([0; 64])
    }
}

impl ops::Index<usize> for Tile {
    type Output = u8;
    #[inline]
    fn index(&self, i: usize)  -> &u8 {
        &self.0[i]
    }
}

impl Tile {
    pub fn iter(&self) -> Iter<u8> {
        slice::SliceExt::iter(&self.0[..])
    }

    pub fn chunks(&self, n: usize) -> Chunks<u8> {
        self.0.chunks(n)
    }
}