[−][src]Trait gridly::location::Component
A component of a Location
This trait comprises 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 column in a grid.
In practice, most code will call methods directly on Row
or Column
values. However, a lot of gridly and 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>
type Distance: VecComponent<Point = Self>
Required methods
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));
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));
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");
fn value(self) -> isize
Get the integer value of this component
use gridly::prelude::*; assert_eq!(Row(5).value(), 5);
fn add_distance(self, amount: 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));
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
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));
fn transpose(self) -> Self::Converse
Convert a Row into a Column or vice versa
/// ``` use gridly::prelude::*;
assert_eq!(Row(3).transpose(), Column(3));