pub trait Direction:
Sized
+ PartialEq
+ Copy {
// Required methods
fn iter() -> impl Iterator<Item = Self>;
fn delta(self) -> (i8, i8);
fn rotate_right(self) -> Self;
fn rotate_left(self) -> Self;
fn rotate_right_90(self) -> Self;
fn rotate_left_90(self) -> Self;
fn opposite(self) -> Self;
// Provided methods
fn iter_from(initial_dir: Self) -> impl Iterator<Item = Self> { ... }
fn iter_valid_usizes_deltas(
curr: (usize, usize),
size: (usize, usize),
) -> impl Iterator<Item = (usize, usize)> { ... }
fn apply_delta_to_usizes(self, usizes: (usize, usize)) -> (usize, usize) { ... }
}Required Methods§
Sourcefn iter() -> impl Iterator<Item = Self>
fn iter() -> impl Iterator<Item = Self>
Returns an iterator through all values of the Direction, starting with Up and
proceeding clockwise.
Sourcefn rotate_right(self) -> Self
fn rotate_right(self) -> Self
Returns a new Direction which represents the given one being rotated “right” or clockwise by one unit.
Sourcefn rotate_left(self) -> Self
fn rotate_left(self) -> Self
Returns a new Direction which represents the given one being rotated “left” or counter-clockwise by one unit.
Sourcefn rotate_right_90(self) -> Self
fn rotate_right_90(self) -> Self
Returns a new Direction which represents the given one being rotated “right” or clockwise by 90 degrees.
Sourcefn rotate_left_90(self) -> Self
fn rotate_left_90(self) -> Self
Returns a new Direction which represents the given one being rotated “left” or counter-clockwise by 90 degrees.
Provided Methods§
Sourcefn iter_from(initial_dir: Self) -> impl Iterator<Item = Self>
fn iter_from(initial_dir: Self) -> impl Iterator<Item = Self>
Returns an iterator through all values of the Direction, starting with the given
initial_dir and proceeding clockwise.
use aoclib_rs::dir::{Dir4, Dir8, Direction};
assert_eq!(
Dir4::iter_from(Dir4::Left).collect::<Vec<_>>(),
[Dir4::Left, Dir4::Up, Dir4::Right, Dir4::Down]
);
assert_eq!(
Dir8::iter_from(Dir8::Dir4(Dir4::Left)).collect::<Vec<_>>(),
[
Dir8::Dir4(Dir4::Left),
Dir8::UpLeft,
Dir8::Dir4(Dir4::Up),
Dir8::UpRight,
Dir8::Dir4(Dir4::Right),
Dir8::DownRight,
Dir8::Dir4(Dir4::Down),
Dir8::DownLeft
]
);Sourcefn iter_valid_usizes_deltas(
curr: (usize, usize),
size: (usize, usize),
) -> impl Iterator<Item = (usize, usize)>
fn iter_valid_usizes_deltas( curr: (usize, usize), size: (usize, usize), ) -> impl Iterator<Item = (usize, usize)>
Similar to apply_delta_to_usizes(), but iterates through all valid directions. A valid
direction is defined as any whose result is non-negative and is less than size in the
respective dimension.
use aoclib_rs::dir::{Dir8, Direction};
assert_eq!(
Dir8::iter_valid_usizes_deltas((0, 5), (4, 6)).collect::<Vec<_>>(),
[(0, 4), (1, 4), (1, 5)]
);Sourcefn apply_delta_to_usizes(self, usizes: (usize, usize)) -> (usize, usize)
fn apply_delta_to_usizes(self, usizes: (usize, usize)) -> (usize, usize)
Takes in a pair of usizes representing (x, y) coordinates, and returns the result of
“moving” one unit in the given direction. Can panic (see usizes_plus_i() documentation).
use aoclib_rs::dir::{Dir4, Dir8, Direction};
assert_eq!(Dir4::Right.apply_delta_to_usizes((4, 5)), (5, 5));
assert_eq!(Dir8::UpRight.apply_delta_to_usizes((4, 5)), (5, 4));
// Dir4::Left.apply_delta_to_usizes((0, 0));
// panics: -1 is an invalid usizeDyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.