[][src]Trait gridly::prelude::LocationComponent

pub trait LocationComponent: Sized + From<isize> + Copy + Debug + Ord + Eq + Hash + Default {
    type Converse: Component<Converse = Self>;
    type Distance: VecComponent<Point = Self>;
#[must_use]
    fn from_location<L: LocationLike>(location: L) -> Self;
#[must_use] fn combine(self, other: Self::Converse) -> Location;
#[must_use] fn name() -> &'static str;
#[must_use] fn value(self) -> isize;
#[must_use] fn add_distance(self, amount: impl Into<Self::Distance>) -> Self;
#[must_use] fn distance_from(self, origin: Self) -> Self::Distance; #[must_use] fn distance_to(self, target: Self) -> Self::Distance { ... }
#[must_use] fn transpose(self) -> Self::Converse { ... }
#[must_use] fn span(self, length: Self::Distance) -> ComponentRange<Self> { ... }
#[must_use] fn range_to(self, end: Self) -> ComponentRange<Self> { ... } }

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.

Associated Types

type Converse: Component<Converse = Self>

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

type Distance: VecComponent<Point = Self>

The associated vector component (Rows or Columns)

Loading content...

Required methods

#[must_use] 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));

#[must_use] 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));

#[must_use] 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");

#[must_use] fn value(self) -> isize

Get the integer value of this component

use gridly::prelude::*;

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

#[must_use] 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));

#[must_use] 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));
Loading content...

Provided methods

#[must_use] 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));

#[must_use] fn transpose(self) -> Self::Converse

Convert a Row into a Column or vice versa

use gridly::prelude::*;

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

Important traits for ComponentRange<C>
#[must_use] 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)));

Important traits for ComponentRange<C>
#[must_use] 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)));
Loading content...

Implementors

impl Component for Column[src]

type Converse = Row

type Distance = Columns

impl Component for Row[src]

type Converse = Column

type Distance = Rows

Loading content...