Trait gridly::grid::Grid

source ·
pub trait Grid: GridBounds {
    type Item;

    // Required method
    unsafe fn get_unchecked(&self, location: Location) -> &Self::Item;

    // Provided methods
    fn get(
        &self,
        location: impl LocationLike
    ) -> Result<&Self::Item, BoundsError> { ... }
    fn view<T: LocComponent>(&self) -> View<'_, Self, T> { ... }
    fn rows(&self) -> RowsView<'_, Self> { ... }
    fn columns(&self) -> ColumnsView<'_, Self> { ... }
    unsafe fn single_view_unchecked<T: LocComponent>(
        &self,
        index: T
    ) -> SingleView<'_, Self, T> { ... }
    unsafe fn row_unchecked(&self, row: Row) -> RowView<'_, Self> { ... }
    unsafe fn column_unchecked(&self, column: Column) -> ColumnView<'_, Self> { ... }
    fn single_view<T: LocComponent>(
        &self,
        index: T
    ) -> Result<SingleView<'_, Self, T>, RangeError<T>> { ... }
    fn row(
        &self,
        row: impl Into<Row>
    ) -> Result<RowView<'_, Self>, RowRangeError> { ... }
    fn column(
        &self,
        column: impl Into<Column>
    ) -> Result<ColumnView<'_, Self>, ColumnRangeError> { ... }
    fn display_with<T, F>(&self, func: F) -> DisplayAdapter<&Self, F>
       where F: Fn(&Self::Item) -> T,
             T: Display { ... }
}
Expand description

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.

Required Associated Types§

source

type Item

The item type stored in the grid

Required Methods§

source

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§

source

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.

source

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.

source

fn rows(&self) -> RowsView<'_, Self>

Get a view of a grid’s rows. See [View] for details.

source

fn columns(&self) -> ColumnsView<'_, Self>

Get a view of a grid’s columns. See [View] for details.

source

unsafe fn single_view_unchecked<T: LocComponent>( &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.

source

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.

source

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.

source

fn single_view<T: LocComponent>( &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.

source

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.

source

fn column( &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.

source

fn display_with<T, F>(&self, func: F) -> DisplayAdapter<&Self, F>
where F: Fn(&Self::Item) -> T, T: Display,

Make a grid Displayable, 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.

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl<G: Grid> Grid for &G

§

type Item = <G as Grid>::Item

source§

unsafe fn get_unchecked(&self, location: Location) -> &Self::Item

source§

impl<G: Grid> Grid for &mut G

§

type Item = <G as Grid>::Item

source§

unsafe fn get_unchecked(&self, location: Location) -> &Self::Item

Implementors§