rust/vec2d/src/lib.rs
changeset 14160 c24a76f131d6
parent 14121 69db1d2e4cec
child 14170 a4c1a2d0ac24
equal deleted inserted replaced
14159:0aeea29ef890 14160:c24a76f131d6
    53 impl<T: Copy> Vec2D<T> {
    53 impl<T: Copy> Vec2D<T> {
    54     pub fn new(size: Size, value: T) -> Self {
    54     pub fn new(size: Size, value: T) -> Self {
    55         Self { size, data: vec![value; size.area()] }
    55         Self { size, data: vec![value; size.area()] }
    56     }
    56     }
    57 
    57 
    58     pub fn raw_data(&self) -> &[T] {
    58     #[inline]
    59         &self.data
    59     pub fn as_slice(&self) -> &[T] {
       
    60         self.data.as_slice()
       
    61     }
       
    62 
       
    63     #[inline]
       
    64     pub fn as_mut_slice(&mut self) -> &mut [T] {
       
    65         self.data.as_mut_slice()
    60     }
    66     }
    61 
    67 
    62     #[inline]
    68     #[inline]
    63     pub fn get(&self, row: usize, column: usize) -> Option<&<usize as SliceIndex<[T]>>::Output> {
    69     pub fn get(&self, row: usize, column: usize) -> Option<&<usize as SliceIndex<[T]>>::Output> {
    64         self.data.get(row * self.width() + column)
    70         self.data.get(row * self.width() + column)
    80     }
    86     }
    81 
    87 
    82     #[inline]
    88     #[inline]
    83     pub fn rows(&self) -> impl Iterator<Item = &[T]> {
    89     pub fn rows(&self) -> impl Iterator<Item = &[T]> {
    84         self.data.chunks(self.width())
    90         self.data.chunks(self.width())
       
    91     }
       
    92 
       
    93     #[inline]
       
    94     pub fn rows_mut(&mut self) -> impl Iterator<Item = &mut [T]> {
       
    95         let width = self.width();
       
    96         self.data.chunks_mut(width)
       
    97     }
       
    98 }
       
    99 
       
   100 impl<T: Copy> AsRef<[T]> for Vec2D<T> {
       
   101     fn as_ref(&self) -> &[T] {
       
   102         self.as_slice()
       
   103     }
       
   104 }
       
   105 
       
   106 impl<T: Copy> AsMut<[T]> for Vec2D<T> {
       
   107     fn as_mut(&mut self) -> &mut [T] {
       
   108         self.as_mut_slice()
    85     }
   109     }
    86 }
   110 }
    87 
   111 
    88 #[cfg(test)]
   112 #[cfg(test)]
    89 mod tests {
   113 mod tests {