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§
Required Methods§
sourcefn from_location<L: LocationLike>(location: L) -> Self
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));
sourcefn combine(self, other: Self::Converse) -> Location
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));
sourcefn name() -> &'static str
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");
sourcefn value(self) -> isize
fn value(self) -> isize
Get the integer value of this component
use gridly::prelude::*;
assert_eq!(Row(5).value(), 5);
sourcefn add_distance(self, amount: impl Into<Self::Distance>) -> Self
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));
sourcefn distance_from(self, origin: Self) -> Self::Distance
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§
sourcefn distance_to(self, target: Self) -> Self::Distance
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));
sourcefn transpose(self) -> Self::Converse
fn transpose(self) -> Self::Converse
Convert a Row into a Column or vice versa
use gridly::prelude::*;
assert_eq!(Row(3).transpose(), Column(3));
sourcefn span(self, length: Self::Distance) -> ComponentRange<Self> ⓘ
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)));
sourcefn range_to(self, end: Self) -> ComponentRange<Self> ⓘ
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)));