pub struct Cell { /* private fields */ }
Expand description
Cell
represents the basic unit of Grid
.
Consists of global positions global_width: u8
and global_depth: u8
, alongside with methods implementing
common mathematical operations for safe interactions with grids and other cells
Due to low memory size, Cell
implements Copy
trait, so all methods take self
(copy) as first argument
§Examples
You can create Cell using new(global_width, global_depth):
use grid_math::Cell;
let cell = Cell::new(10, 15);
Or use functionality of implemented From
and Into
traits:
use grid_math::Cell;
let cell = Cell::from((9, 9));
let cell: Cell = (6, 7).into();
To read global_width or global_depth values, use getters:
use grid_math::Cell;
let cell = Cell::new(10, 10);
let w = cell.global_width();
let d = cell.global_depth();
Or use into()
provided by Into
trait:
use grid_math::Cell;
let cell = Cell::new(10, 10);
let (w, d): (u8, u8) = cell.into();
‘Cell’ implements Display
and Debug
trait, so you can easily print it out:
use grid_math::Cell;
let cell = Cell::new(10, 10);
println!("Cell: {cell}"); // Cell: (10, 10)
assert_eq!(format!("{cell}"), "(10, 10)");
Other methods involve interactions with Grid
Cell
is designed to not mutate it’s contents.
Instead, all operations return new instances of Cell
Also worth noting, that all operations on Cell
are verified to be logically correct,
otherwise logically incorrect operations will be met with panic!
Here is a brief overview of Cell
and Grid
interactions:
Check if Cell
is within the Grid
:
use grid_math::{Cell, Grid};
let cell = Cell::new(3, 4);
let grid = Grid::new(10, 10); // 10x10 grid starting at (0,0)
assert!(cell.within(grid));
Get relative to the Grid
position of Cell
:
(Grid
can start not only from (0,0))
use grid_math::{Cell, Grid};
let cell = Cell::new(3, 4);
let grid = Grid::indented(8, 8, (2, 1)); // 8x8 grid starting at (2,1)
let (width, depth) = (cell.width(grid), cell.depth(grid));
// cell's width on grid = cell.global_width - grid.start.global_width
// cell's depth on grid = cell.global_depth - grid.start.global_depth
assert_eq!((width, depth), (1, 3));
// get gaps between width and depth grid borders and cell:
let (width_gap, depth_gap) = (cell.width_gap(grid), cell.depth_gap(grid));
assert_eq!((width_gap, depth_gap), (6, 4));
// get member of grid by relative position:
let member = grid.member(width, depth);
assert_eq!(cell, member);
Perform some move calculations of Cell
on Grid
:
use grid_math::{Cell, Grid};
let grid = Grid::new(10, 10);
let cell = grid.start(); // get grid's first cell
let next = cell.strict_right(grid, 3); // move to the right by 3, panics if grid bounds overflow occures
assert_eq!(next, Cell::new(3, 0));
let next = cell.saturating_down(grid, 15); // move down by 15, returns grid bound if overflow occures
assert_eq!(next, Cell::new(0, 9));
let next = cell.wrapping_right(grid, 5).strict_left(grid, 2).project_down(grid); // chain of movements
assert_eq!(next, Cell::new(3, 9));
To get more examples, look at Cell
and Grid
methods documentation.
Implementations§
Source§impl Cell
impl Cell
Sourcepub fn new(global_width: u8, global_depth: u8) -> Self
pub fn new(global_width: u8, global_depth: u8) -> Self
Creates new Cell
with specified global_width: u8
and global_depth: u8
global position
§Examples
use grid_math::Cell;
let cell = Cell::new(10, 15);
Sourcepub fn within(self, grid: Grid) -> bool
pub fn within(self, grid: Grid) -> bool
Checks if the Cell
is within the given Grid
§Examples
use grid_math::{Cell, Grid};
let grid = Grid::new(10, 10);
let cell = Cell::new(5, 5);
assert!(cell.within(grid));
let cell = Cell::new(9, 15);
assert!(!cell.within(grid));
Sourcepub fn within_panic(self, grid: Grid)
pub fn within_panic(self, grid: Grid)
Sourcepub fn global_width(self) -> u8
pub fn global_width(self) -> u8
Returns global_width
field of Cell
§Examples
use grid_math::Cell;
let cell = Cell::new(8, 8);
let w = cell.global_width();
assert_eq!(w, 8);
Sourcepub fn global_depth(self) -> u8
pub fn global_depth(self) -> u8
Returns global_depth
field of Cell
§Examples
use grid_math::Cell;
let cell = Cell::new(8, 8);
let d = cell.global_depth();
assert_eq!(d, 8);
Sourcepub fn width(self, grid: Grid) -> u8
pub fn width(self, grid: Grid) -> u8
Calculates the width
of the Cell
relative to the given Grid
width
here means position / index / x of Cell
on width axis
§Panics
Panics if the Cell
is not within the given Grid
§Examples
use grid_math::{Cell, Grid};
let cell = Cell::new(8, 8);
let grid = Grid::indented(7, 7, (4, 4)); // 7x7 grid starting at (4,4)
let width = cell.width(grid); // width = 4
assert_eq!(width, 4);
Sourcepub fn width_gap(self, grid: Grid) -> u8
pub fn width_gap(self, grid: Grid) -> u8
Calculates the gap between the width
of Cell
and the width
of Grid
§Panics
Panics if the Cell
is not within the given Grid
§Examples
use grid_math::{Cell, Grid};
let cell = Cell::new(8, 8);
let grid = Grid::indented(7, 7, (4, 4)); // 7x7 grid starting at (4,4)
let width_gap = cell.width_gap(grid); // width_gap = 2
assert_eq!(width_gap, 2);
Sourcepub fn depth(self, grid: Grid) -> u8
pub fn depth(self, grid: Grid) -> u8
Calculates the depth
of Cell
relative to the given Grid
depth
here means position / index / y of Cell
on depth axis
§Panics
Panics if the Cell
is not within the given Grid
§Examples
use grid_math::{Cell, Grid};
let cell = Cell::new(8, 8);
let grid = Grid::indented(7, 7, (4, 4)); // 7x7 grid starting at (4,4)
let depth = cell.depth(grid); // depth = 4
assert_eq!(depth, 4);
Sourcepub fn depth_gap(self, grid: Grid) -> u8
pub fn depth_gap(self, grid: Grid) -> u8
Calculates the gap between the depth
of Cell
and the depth
of Grid
§Panics
Panics if the Cell
is not within the given Grid
§Examples
use grid_math::{Cell, Grid};
let cell = Cell::new(8, 8);
let grid = Grid::indented(7, 7, (4, 4)); // 7x7 grid starting at (4,4)
let depth_gap = cell.depth_gap(grid); // depth_gap = 2
assert_eq!(depth_gap, 2);
Sourcepub fn will_underflow_depth(self, grid: Grid, step: u8) -> bool
pub fn will_underflow_depth(self, grid: Grid, step: u8) -> bool
Checks if the up
operation on Cell
will violate the given Grid
upper border
§Panics
Panics if the Cell
is not within the given Grid
§Examples
use grid_math::{Cell, Grid};
let grid = Grid::new(10, 10);
let cell = Cell::new(2, 2);
assert!(cell.will_underflow_depth(grid, 3));
assert!(!cell.will_underflow_depth(grid, 2));
Sourcepub fn will_overflow_depth(self, grid: Grid, step: u8) -> bool
pub fn will_overflow_depth(self, grid: Grid, step: u8) -> bool
Checks if the down
operation on Cell
will violate the given Grid
lower border
§Panics
Panics if the Cell
is not within the given Grid
§Examples
use grid_math::{Cell, Grid};
let grid = Grid::new(10, 10);
let cell = Cell::new(7, 7);
assert!(cell.will_overflow_depth(grid, 3));
assert!(!cell.will_overflow_depth(grid, 2));
Sourcepub fn will_underflow_width(self, grid: Grid, step: u8) -> bool
pub fn will_underflow_width(self, grid: Grid, step: u8) -> bool
Checks if the left
operation on Cell
will violate the given Grid
left border
§Panics
Panics if the Cell
is not within the given Grid
§Examples
use grid_math::{Cell, Grid};
let grid = Grid::new(10, 10);
let cell = Cell::new(2, 2);
assert!(cell.will_underflow_width(grid, 3));
assert!(!cell.will_underflow_width(grid, 2));
Sourcepub fn will_overflow_width(self, grid: Grid, step: u8) -> bool
pub fn will_overflow_width(self, grid: Grid, step: u8) -> bool
Checks if the right
operation on Cell
will violate the given Grid
right border
§Panics
Panics if the Cell
is not within the given Grid
§Examples
use grid_math::{Cell, Grid};
let grid = Grid::new(10, 10);
let cell = Cell::new(7, 7);
assert!(cell.will_overflow_width(grid, 3));
assert!(!cell.will_overflow_width(grid, 2));
Sourcepub fn strict_up(self, grid: Grid, step: u8) -> Cell
pub fn strict_up(self, grid: Grid, step: u8) -> Cell
Moves current Cell
upwards by step
relative to the given Grid
This operation does not mutate current Cell
fields,
instead it calculates new position and returns new Cell
§Panics
Panics if the Cell
is not within the given Grid
Panics if this operation will violate the given Grid
upper border
§Examples
use grid_math::{Cell, Grid};
let grid = Grid::new(10, 10);
let cell = Cell::new(2, 2);
let next = cell.strict_up(grid, 2);
assert_eq!(next, Cell::new(2, 0));
use grid_math::{Cell, Grid};
let grid = Grid::new(10, 10);
let cell = Cell::new(2, 2);
let next = cell.strict_up(grid, 3); // panic!
Sourcepub fn strict_down(self, grid: Grid, step: u8) -> Cell
pub fn strict_down(self, grid: Grid, step: u8) -> Cell
Moves current Cell
downwards by step
relative to the given Grid
This operation does not mutate current Cell
fields,
instead it calculates new position and returns new Cell
§Panics
Panics if the Cell
is not within the given Grid
Panics if this operation will violate the given Grid
lower border
§Examples
use grid_math::{Cell, Grid};
let grid = Grid::new(10, 10);
let cell = Cell::new(7, 7);
let next = cell.strict_down(grid, 2);
assert_eq!(next, Cell::new(7, 9));
use grid_math::{Cell, Grid};
let grid = Grid::new(10, 10);
let cell = Cell::new(7, 7);
let next = cell.strict_down(grid, 3); // panic!
Sourcepub fn strict_left(self, grid: Grid, step: u8) -> Cell
pub fn strict_left(self, grid: Grid, step: u8) -> Cell
Moves current Cell
to the left by step
relative to the given Grid
This operation does not mutate current Cell
fields,
instead it calculates new position and returns new Cell
§Panics
Panics if the Cell
is not within the given Grid
Panics if this operation will violate the given Grid
left border
§Examples
use grid_math::{Cell, Grid};
let grid = Grid::new(10, 10);
let cell = Cell::new(2, 2);
let next = cell.strict_left(grid, 2);
assert_eq!(next, Cell::new(0, 2));
use grid_math::{Cell, Grid};
let grid = Grid::new(10, 10);
let cell = Cell::new(2, 2);
let next = cell.strict_left(grid, 3); // panic!
Sourcepub fn strict_right(self, grid: Grid, step: u8) -> Cell
pub fn strict_right(self, grid: Grid, step: u8) -> Cell
Moves current Cell
to the right by step
relative to the given Grid
This operation does not mutate current Cell
fields,
instead it calculates new position and returns new Cell
§Panics
Panics if the Cell
is not within the given Grid
Panics if this operation will violate the given Grid
right border
§Examples
use grid_math::{Cell, Grid};
let grid = Grid::new(10, 10);
let cell = Cell::new(7, 7);
let next = cell.strict_right(grid, 2);
assert_eq!(next, Cell::new(9, 7));
use grid_math::{Cell, Grid};
let grid = Grid::new(10, 10);
let cell = Cell::new(7, 7);
let next = cell.strict_right(grid, 3); // panic!
Sourcepub fn saturating_up(self, grid: Grid, step: u8) -> Cell
pub fn saturating_up(self, grid: Grid, step: u8) -> Cell
Moves current Cell
upwards by step
relative to the given Grid
This operation does not mutate current Cell
fields,
instead it calculates new position and returns new Cell
If this operation will cross Grid
upper border,
returns Cell
with depth
= Grid
upper depth limit
§Panics
Panics if the Cell
is not within the given Grid
§Examples
use grid_math::{Cell, Grid};
let grid = Grid::new(10, 10);
let cell = Cell::new(2, 2);
let next = cell.saturating_up(grid, 2);
assert_eq!(next, Cell::new(2, 0));
let next = cell.saturating_up(grid, 5);
assert_eq!(next, Cell::new(2, 0));
Sourcepub fn saturating_down(self, grid: Grid, step: u8) -> Cell
pub fn saturating_down(self, grid: Grid, step: u8) -> Cell
Moves current Cell
downwards by step
relative to the given Grid
This operation does not mutate current Cell
fields,
instead it calculates new position and returns new Cell
If this operation will cross Grid
lower border,
returns Cell
with depth
= Grid
lower depth limit
§Panics
Panics if the Cell
is not within the given Grid
§Examples
use grid_math::{Cell, Grid};
let grid = Grid::new(10, 10);
let cell = Cell::new(7, 7);
let next = cell.saturating_down(grid, 2);
assert_eq!(next, Cell::new(7, 9));
let next = cell.saturating_down(grid, 5);
assert_eq!(next, Cell::new(7, 9));
Sourcepub fn saturating_left(self, grid: Grid, step: u8) -> Cell
pub fn saturating_left(self, grid: Grid, step: u8) -> Cell
Moves current Cell
to the left by step
relative to the given Grid
This operation does not mutate current Cell
fields,
instead it calculates new position and returns new Cell
If this operation will cross Grid
left border,
returns Cell
with width
= Grid
left width limit
§Panics
Panics if the Cell
is not within the given Grid
§Examples
use grid_math::{Cell, Grid};
let grid = Grid::new(10, 10);
let cell = Cell::new(2, 2);
let next = cell.saturating_left(grid, 2);
assert_eq!(next, Cell::new(0, 2));
let next = cell.saturating_left(grid, 5);
assert_eq!(next, Cell::new(0, 2));
Sourcepub fn saturating_right(self, grid: Grid, step: u8) -> Cell
pub fn saturating_right(self, grid: Grid, step: u8) -> Cell
Moves current Cell
to the right by step
relative to the given Grid
This operation does not mutate current Cell
fields,
instead it calculates new position and returns new Cell
If this operation will cross Grid
right border,
returns Cell
with width
= Grid
right width limit
§Panics
Panics if the Cell
is not within the given Grid
§Examples
use grid_math::{Cell, Grid};
let grid = Grid::new(10, 10);
let cell = Cell::new(7, 7);
let next = cell.saturating_right(grid, 2);
assert_eq!(next, Cell::new(9, 7));
let next = cell.saturating_right(grid, 5);
assert_eq!(next, Cell::new(9, 7));
Sourcepub fn overflowing_up(self, grid: Grid, step: u8) -> (Cell, bool)
pub fn overflowing_up(self, grid: Grid, step: u8) -> (Cell, bool)
Moves current Cell
upwards by step
relative to the given Grid
This operation does not mutate current Cell
fields,
instead it calculates new position and returns new Cell
and bool
This operation is similar to the overflowing operations on integer types
It returns new Cell
and ‘bool’ signaling that overflow happened
§Panics
Panics if the Cell
is not within the given Grid
§Examples
use grid_math::{Cell, Grid};
let grid = Grid::new(10, 10);
let cell = Cell::new(2, 2);
let (next, overflowed) = cell.overflowing_up(grid, 2);
assert_eq!((next, overflowed), (Cell::new(2, 0), false));
let (next, overflowed) = cell.overflowing_up(grid, 5);
assert_eq!((next, overflowed), (Cell::new(2, 7), true));
Sourcepub fn overflowing_down(self, grid: Grid, step: u8) -> (Cell, bool)
pub fn overflowing_down(self, grid: Grid, step: u8) -> (Cell, bool)
Moves current Cell
downwards by step
relative to the given Grid
This operation does not mutate current Cell
fields,
instead it calculates new position and returns new Cell
and bool
This operation is similar to the overflowing operations on integer types
It returns new Cell
and ‘bool’ signaling that overflow happened
§Panics
Panics if the Cell
is not within the given Grid
§Examples
use grid_math::{Cell, Grid};
let grid = Grid::new(10, 10);
let cell = Cell::new(7, 7);
let (next, overflowed) = cell.overflowing_down(grid, 2);
assert_eq!((next, overflowed), (Cell::new(7, 9), false));
let (next, overflowed) = cell.overflowing_down(grid, 5);
assert_eq!((next, overflowed), (Cell::new(7, 2), true));
Sourcepub fn overflowing_left(self, grid: Grid, step: u8) -> (Cell, bool)
pub fn overflowing_left(self, grid: Grid, step: u8) -> (Cell, bool)
Moves current Cell
to the left by step
relative to the given Grid
This operation does not mutate current Cell
fields,
instead it calculates new position and returns new Cell
and bool
This operation is similar to the overflowing operations on integer types
It returns new Cell
and ‘bool’ signaling that overflow happened
§Panics
Panics if the Cell
is not within the given Grid
§Examples
use grid_math::{Cell, Grid};
let grid = Grid::new(10, 10);
let cell = Cell::new(2, 2);
let (next, overflowed) = cell.overflowing_left(grid, 2);
assert_eq!((next, overflowed), (Cell::new(0, 2), false));
let (next, overflowed) = cell.overflowing_left(grid, 5);
assert_eq!((next, overflowed), (Cell::new(7, 2), true));
Sourcepub fn overflowing_right(self, grid: Grid, step: u8) -> (Cell, bool)
pub fn overflowing_right(self, grid: Grid, step: u8) -> (Cell, bool)
Moves current Cell
to the right by step
relative to the given Grid
This operation does not mutate current Cell
fields,
instead it calculates new position and returns new Cell
and bool
This operation is similar to the overflowing operations on integer types
It returns new Cell
and ‘bool’ signaling that overflow happened
§Panics
Panics if the Cell
is not within the given Grid
§Examples
use grid_math::{Cell, Grid};
let grid = Grid::new(10, 10);
let cell = Cell::new(7, 7);
let (next, overflowed) = cell.overflowing_right(grid, 2);
assert_eq!((next, overflowed), (Cell::new(9, 7), false));
let (next, overflowed) = cell.overflowing_right(grid, 5);
assert_eq!((next, overflowed), (Cell::new(2, 7), true));
Sourcepub fn wrapping_up(self, grid: Grid, step: u8) -> Cell
pub fn wrapping_up(self, grid: Grid, step: u8) -> Cell
Moves current Cell
upwards by step
relative to the given Grid
This operation is a wrapper around the overflowing_up()
method,
and returns only new Cell
, without bool
§Panics
Panics if the Cell
is not within the given Grid
§Examples
use grid_math::{Cell, Grid};
let grid = Grid::new(10, 10);
let cell = Cell::new(2, 2);
let next = cell.wrapping_up(grid, 2);
assert_eq!(next, Cell::new(2, 0));
let next = cell.wrapping_up(grid, 5);
assert_eq!(next, Cell::new(2, 7));
Sourcepub fn wrapping_down(self, grid: Grid, step: u8) -> Cell
pub fn wrapping_down(self, grid: Grid, step: u8) -> Cell
Moves current Cell
downwards by step
relative to the given Grid
This operation is a wrapper around the overflowing_down()
method,
and returns only new Cell
, without bool
§Panics
Panics if the Cell
is not within the given Grid
§Examples
use grid_math::{Cell, Grid};
let grid = Grid::new(10, 10);
let cell = Cell::new(7, 7);
let next = cell.wrapping_down(grid, 2);
assert_eq!(next, Cell::new(7, 9));
let next = cell.wrapping_down(grid, 5);
assert_eq!(next, Cell::new(7, 2));
Sourcepub fn wrapping_left(self, grid: Grid, step: u8) -> Cell
pub fn wrapping_left(self, grid: Grid, step: u8) -> Cell
Moves current Cell
to the left by step
relative to the given Grid
This operation is a wrapper around the overflowing_left()
method,
and returns only new Cell
, without bool
§Panics
Panics if the Cell
is not within the given Grid
§Examples
use grid_math::{Cell, Grid};
let grid = Grid::new(10, 10);
let cell = Cell::new(2, 2);
let next = cell.wrapping_left(grid, 2);
assert_eq!(next, Cell::new(0, 2));
let next = cell.wrapping_left(grid, 5);
assert_eq!(next, Cell::new(7, 2));
Sourcepub fn wrapping_right(self, grid: Grid, step: u8) -> Cell
pub fn wrapping_right(self, grid: Grid, step: u8) -> Cell
Moves current Cell
to the right by step
relative to the given Grid
This operation is a wrapper around the overflowing_right()
method,
and returns only new Cell
, without bool
§Panics
Panics if the Cell
is not within the given Grid
§Examples
use grid_math::{Cell, Grid};
let grid = Grid::new(10, 10);
let cell = Cell::new(7, 7);
let next = cell.wrapping_right(grid, 2);
assert_eq!(next, Cell::new(9, 7));
let next = cell.wrapping_right(grid, 5);
assert_eq!(next, Cell::new(2, 7));
Sourcepub fn project_up(self, grid: Grid) -> Cell
pub fn project_up(self, grid: Grid) -> Cell
Projects current Cell
onto the top side of the given Grid
This operation does not mutate current Cell
fields,
instead it calculates new position and returns new Cell
§Panics
Panics if the Cell
is not within the given Grid
§Examples
use grid_math::{Cell, Grid};
let grid = Grid::new(10, 10);
let cell = Cell::new(2, 2);
let next = cell.project_up(grid);
assert_eq!(next, Cell::new(2, 0));
Sourcepub fn project_down(self, grid: Grid) -> Cell
pub fn project_down(self, grid: Grid) -> Cell
Projects current Cell
onto the bottom side of the given Grid
This operation does not mutate current Cell
fields,
instead it calculates new position and returns new Cell
§Panics
Panics if the Cell
is not within the given Grid
§Examples
use grid_math::{Cell, Grid};
let grid = Grid::new(10, 10);
let cell = Cell::new(7, 7);
let next = cell.project_down(grid);
assert_eq!(next, Cell::new(7, 9));
Sourcepub fn project_left(self, grid: Grid) -> Cell
pub fn project_left(self, grid: Grid) -> Cell
Projects current Cell
onto the left side of the given Grid
This operation does not mutate current Cell
fields,
instead it calculates new position and returns new Cell
§Panics
Panics if the Cell
is not within the given Grid
§Examples
use grid_math::{Cell, Grid};
let grid = Grid::new(10, 10);
let cell = Cell::new(2, 2);
let next = cell.project_left(grid);
assert_eq!(next, Cell::new(0, 2));
Sourcepub fn project_right(self, grid: Grid) -> Cell
pub fn project_right(self, grid: Grid) -> Cell
Projects current Cell
onto the right side of the given Grid
This operation does not mutate current Cell
fields,
instead it calculates new position and returns new Cell
§Panics
Panics if the Cell
is not within the given Grid
§Examples
use grid_math::{Cell, Grid};
let grid = Grid::new(10, 10);
let cell = Cell::new(7, 7);
let next = cell.project_right(grid);
assert_eq!(next, Cell::new(9, 7));
Sourcepub fn on_the_edge(self, grid: Grid) -> bool
pub fn on_the_edge(self, grid: Grid) -> bool
Checks if the Cell
is on the edge of the given Grid
§Examples
use grid_math::{Cell, Grid};
let grid = Grid::new(10, 10);
let cell = Cell::new(7, 9);
assert!(cell.on_the_edge(grid));
let cell = Cell::new(4, 3);
assert!(!cell.on_the_edge(grid));