pub trait Component: Sized + From<isize> + Copy + Debug + Ord + Eq + Hash + Default {
    type Converse: Component<Converse = Self>;
    type Distance: VecComponent<Point = Self>;

    // Required methods
    fn from_location<L: LocationLike>(location: L) -> Self;
    fn combine(self, other: Self::Converse) -> Location;
    fn name() -> &'static str;
    fn value(self) -> isize;
    fn add_distance(self, amount: impl Into<Self::Distance>) -> Self;
    fn distance_from(self, origin: Self) -> Self::Distance;

    // Provided methods
    fn distance_to(self, target: Self) -> Self::Distance { ... }
    fn transpose(self) -> Self::Converse { ... }
    fn span(self, length: Self::Distance) -> ComponentRange<Self>  { ... }
    fn range_to(self, end: Self) -> ComponentRange<Self>  { ... }
}
Expand description

A component of a Location, which may be either a Row or a Column. It is effectively an index into a given row or column of a grid; for instance, a Row can index a row in a grid.

In practice, most code will call methods directly on Row or Column values. However, a lot of gridly functionality is agnostic towards rows and columns (for instance, a view over a row in a grid is functionally the same as a view over a column), so the Component trait allows such functionality to be written generically.

The key methods for Component that allow it to work in generic contexts are from_location, which gets a component from a Location, and combine, which combines a Row or Column with its converse (a Column or a Row) to create a new Location.

Required Associated Types§

source

type Converse: Component<Converse = Self>

The converse component (Row to Column, or vice versa)

source

type Distance: VecComponent<Point = Self>

The associated vector component (Rows or Columns)

Required Methods§

source

fn from_location<L: LocationLike>(location: L) -> Self

Get this component type from a Location.

Example:

use gridly::prelude::*;

let loc = Location::new(2, 5);
assert_eq!(Row::from_location(&loc), Row(2));
assert_eq!(Column::from_location(&loc), Column(5));
source

fn combine(self, other: Self::Converse) -> Location

Combine this component with its converse to create a Location. Note that Row and Column can simply be added together with + to create a new Location; this method exists to assist with generic code.

Example:

use gridly::prelude::*;

let loc = Row(2).combine(Column(5));
assert_eq!(loc, Location::new(2, 5));
assert_eq!(loc, Row(2) + Column(5));
source

fn name() -> &'static str

Return the lowercase name of this component type– “row” or “column”. Intended for debug printing, error messages, etc.

use gridly::prelude::*;

assert_eq!(Row::name(), "row");
assert_eq!(Column::name(), "column");
source

fn value(self) -> isize

Get the integer value of this component

use gridly::prelude::*;

assert_eq!(Row(5).value(), 5);
source

fn add_distance(self, amount: impl Into<Self::Distance>) -> Self

Add a distance to this component. This method is provided because we can’t require a trait bound on Add for Component, but in general just using + is preferable.

use gridly::prelude::*;

assert_eq!(Row(4).add_distance(Rows(5)), Row(9));
source

fn distance_from(self, origin: Self) -> Self::Distance

Find the distance between two components, using the other component as the origin

use gridly::prelude::*;

assert_eq!(Row(8).distance_from(Row(3)), Rows(5));

Provided Methods§

source

fn distance_to(self, target: Self) -> Self::Distance

Find the distance between two components, using this component as the origin

use gridly::prelude::*;

assert_eq!(Row(3).distance_to(Row(8)), Rows(5));
source

fn transpose(self) -> Self::Converse

Convert a Row into a Column or vice versa

use gridly::prelude::*;

assert_eq!(Row(3).transpose(), Column(3));
source

fn span(self, length: Self::Distance) -> ComponentRange<Self>

Create a range, starting at this component, with the given length

use gridly::prelude::*;

assert_eq!(Row(-1).span(Rows(2)), RowRange::bounded(Row(-1), Row(1)));
source

fn range_to(self, end: Self) -> ComponentRange<Self>

Create a range, starting at this component, ending at the given component

use gridly::prelude::*;

assert_eq!(Row(10).range_to(Row(20)), RowRange::span(Row(10), Rows(10)));

Object Safety§

This trait is not object safe.

Implementors§