[−][src]Trait gridly::grid::Grid
Base Reader trait for grids. This trait provides the grid's cell type,
Item
, and an unsafe getter method for fetching a cell at a bounds-checked
location. It uses this unsafe getter, plus GridBounds
based
bounds-checking, to provide a comprehensive and safe interface for reading
and iterating over elements in a grid.
Associated Types
type Item
The item type stored in the grid
Required methods
unsafe fn get_unchecked(&self, location: &Location) -> &Self::Item
Get a reference to a cell, without doing bounds checking. Implementors
of this method are allowed to assume that bounds checking has already
been performed on the location, which means that implementors are allowed
to do their own unsafe get
operations on the underlying storage,
where relevant / possible.
Safety
Callers must ensure that the location has been bounds-checked before
calling this method. The safe interface to Grid
automatically performs
this checking for you.
Provided methods
fn get(&self, location: impl LocationLike) -> Result<&Self::Item, BoundsError>
Get a reference to a cell in a grid. Returns an error if the location is out of bounds with the specific boundary violation.
fn view<T: LocComponent>(&self) -> View<Self, T>
Get a view of a grid, over its rows or columns. A view of a grid is
similar to a slice, but instead of being a view over specific elements,
it's a view over the rows and columns. See [View]
for details.
fn rows(&self) -> RowsView<Self>
Get a view of a grid's rows. See [View]
for details.
fn columns(&self) -> ColumnsView<Self>
Get a view of a grid's columns. See [View]
for details.
unsafe fn single_view_unchecked<T: LocComponent>(
&self,
index: T
) -> SingleView<Self, T>
&self,
index: T
) -> SingleView<Self, T>
Get a view of a single row or column in a grid, without bounds checking that row or column index.
Safety
Callers must ensure that the index has been bounds-checked before calling this method.
unsafe fn row_unchecked(&self, row: Row) -> RowView<Self>
Get a view of a single row in a grid, without bounds checking that row's index.
Safety
Callers must ensure that the row index has been bounds-checked before calling this method.
unsafe fn column_unchecked(&self, column: Column) -> ColumnView<Self>
Get a view of a single column in a grid, without bounds checking that column's index.
Safety
Callers must ensure that the column index has been bounds-checked before calling this method.
fn single_view<T: LocComponent>(
&self,
index: T
) -> Result<SingleView<Self, T>, RangeError<T>>
&self,
index: T
) -> Result<SingleView<Self, T>, RangeError<T>>
Get a view of a single row or column in a grid. Returns an error if the index of the row or column is out of bounds for the grid.
fn row(&self, row: impl Into<Row>) -> Result<RowView<Self>, RowRangeError>
Get a view of a single row in a grid. Returns an error if the index of the row is out of bounds for the grid.
fn column(
&self,
column: impl Into<Column>
) -> Result<ColumnView<Self>, ColumnRangeError>
&self,
column: impl Into<Column>
) -> Result<ColumnView<Self>, ColumnRangeError>
Get a view of a single column in a grid. Returns an error if the index of the column is out of bounds for the grid.
fn display_with<T, F>(&self, func: F) -> DisplayAdapter<&Self, F> where
F: Fn(&Self::Item) -> T,
T: Display,
F: Fn(&Self::Item) -> T,
T: Display,
Make a grid Display
able, using a function that defines how each of its
cells are printed. For each row, the adapter simply prints each cell
in the row, followed by a newline.
Note that this adapter doesn't make any attempt to ensure the printed
grid is visually a rectangle. It is up to the display adapter function
func
to ensure that each cell has the same width when printed.