Struct das_grid::Grid[][src]

pub struct Grid<T: Copy + Clone> { /* fields omitted */ }

Stores the grid values and the cells The grid itself representation is a flatten vector which is transformed for 2D representation when called by the user

The cells are internally manage by a Vec<T>

So to create a grid with 4x4 (collums and rows)

let grid = das_grid::Grid::new(4, 4, 0);
assert_eq!(grid.size(), 16);

Or if you like let’s say a Tetris style grid

let grid = das_grid::Grid::new(10, 20, 0);

// And it will have 200 cells!
assert_eq!(grid.size(), 200);

Implementations

impl<T: Copy + Clone> Grid<T>[src]

pub fn new(width: i32, height: i32, value: T) -> Self where
    T: Clone + Copy + Display
[src]

Creates a grid of size rows x columns with default value passed on the third parameter For example this will generate a 2x2 grid of value 1:

let grid = das_grid::Grid::new(2, 2, 1);
assert_eq!(grid.size(), 4);

pub fn set(&mut self, index: (i32, i32), value: &T) -> Result<(), OutOfGridErr> where
    T: Copy
[src]

Sets a given value to the position (x, y)

Be careful if the value is out of the bounds of grid it will return an error with the type of OutOfGridErr

let mut grid = das_grid::Grid::new(2, 2, 1);
assert!(grid.set((0, 0), &1).is_ok());

pub fn get_mut(&mut self, index: (i32, i32)) -> Result<&mut T, OutOfGridErr>[src]

Gets a give value to the position (x, y) as mutable

Be careful if the value is out of the bounds of grid it will return an error with the type of OutOfGridErr

let mut grid = das_grid::Grid::new(2, 2, 1);
let mut v = grid.get_mut((0, 0)).expect("cannnot get pos at (0, 0)");
*v = 50;
assert_eq!(grid.get((0, 0)).unwrap_or(&0), &50);

pub fn get(&self, index: (i32, i32)) -> Result<&T, OutOfGridErr>[src]

Gets a give value to the position (x, y)

Be careful if the value is out of the bounds of grid it will return an error with the type of OutOfGridErr

let grid = das_grid::Grid::new(2, 2, 1);
let v = grid.get((0, 0));
assert_eq!(v, Ok(&1));

pub fn mov(
    &mut self,
    index: (i32, i32),
    dest: (i32, i32)
) -> Result<(), OutOfGridErr>
[src]

Moves a given value from position (x, y) to destiny position (x, y)

Be careful if the value is out of the bounds of grid it will return an error with the type of OutOfGridErr

let mut grid = das_grid::Grid::new(2, 2, 1);
assert_eq!(grid.mov((0, 0), (1, 1)), Ok(()));

pub fn mov_to(
    &mut self,
    index: (i32, i32),
    direction: MoveDirection
) -> Result<(), OutOfGridErr>
[src]

Moves a given value from position (x, y) to another position based on the direction

The directions can be Left, Right, Top, Down:

  • DasGrid::MoveDirection::Left, translates to (-1, 0)
  • DasGrid::MoveDirection::Right, translates to (1, 0)
  • DasGrid::MoveDirection::Top, translates to (0, -1)
  • DasGrid::MoveDirection::Down, translates to (0, 1)

Be careful if the value is out of the bounds of grid it will return an error with the type of OutOfGridErr

let mut grid = das_grid::Grid::new(2, 2, 1);
assert_eq!(grid.mov_to((0, 0), das_grid::MoveDirection::Right), Ok(()));

pub fn size(&self) -> usize[src]

Get the size of grid based on cells length

For instance a 10x10 grid will return the size of 100

let mut grid = das_grid::Grid::new(2, 2, 1);
assert_eq!(grid.size(), 4);

pub fn width(&self) -> i32[src]

The width of the grid

let mut grid = das_grid::Grid::new(3, 2, 1);
assert_eq!(grid.width(), 3);

pub fn height(&self) -> i32[src]

The height of the grid

let mut grid = das_grid::Grid::new(3, 2, 1);
assert_eq!(grid.height(), 2);

pub fn enumerate(&self) -> Vec<(i32, i32)>[src]

Returns the grid as a tuple of (x, y)

let mut grid = das_grid::Grid::new(3, 2, 1);
for (x, y) in grid.enumerate() {
    println!("x {} y {}", x, y);
}

Trait Implementations

impl<T: Copy + Clone + Display> Debug for Grid<T>[src]

fn fmt(&self, f: &mut Formatter<'_>) -> Result[src]

Formats the value using the given formatter. Read more

impl<T: Copy + Clone> Display for Grid<T>[src]

fn fmt(&self, f: &mut Formatter<'_>) -> Result[src]

Formats the value using the given formatter. Read more

impl<T: Copy + Clone> Index<(i32, i32)> for Grid<T>[src]

type Output = T

The returned type after indexing.

fn index(&self, index: (i32, i32)) -> &T[src]

Performs the indexing (container[index]) operation. Read more

impl<T: Copy + Clone> IndexMut<(i32, i32)> for Grid<T>[src]

fn index_mut(&mut self, index: (i32, i32)) -> &mut T[src]

Performs the mutable indexing (container[index]) operation. Read more

impl<'a, T: Copy + Clone> IntoIterator for &'a Grid<T>[src]

type Item = &'a T

The type of the elements being iterated over.

type IntoIter = Iter<'a, T>

Which kind of iterator are we turning this into?

fn into_iter(self) -> Self::IntoIter[src]

Creates an iterator from a value. Read more

impl<'a, T: Copy + Clone> IntoIterator for &'a mut Grid<T>[src]

type Item = &'a mut T

The type of the elements being iterated over.

type IntoIter = IterMut<'a, T>

Which kind of iterator are we turning this into?

fn into_iter(self) -> Self::IntoIter[src]

Creates an iterator from a value. Read more

Auto Trait Implementations

impl<T> RefUnwindSafe for Grid<T> where
    T: RefUnwindSafe

impl<T> Send for Grid<T> where
    T: Send

impl<T> Sync for Grid<T> where
    T: Sync

impl<T> Unpin for Grid<T> where
    T: Unpin

impl<T> UnwindSafe for Grid<T> where
    T: UnwindSafe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

pub fn type_id(&self) -> TypeId[src]

Gets the TypeId of self. Read more

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

pub fn borrow(&self) -> &T[src]

Immutably borrows from an owned value. Read more

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

pub fn borrow_mut(&mut self) -> &mut T[src]

Mutably borrows from an owned value. Read more

impl<T> From<T> for T[src]

pub fn from(t: T) -> T[src]

Performs the conversion.

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

pub fn into(self) -> U[src]

Performs the conversion.

impl<T> ToString for T where
    T: Display + ?Sized
[src]

pub default fn to_string(&self) -> String[src]

Converts the given value to a String. Read more

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>[src]

Performs the conversion.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

pub fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>[src]

Performs the conversion.