1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
//! Utilites related to the axes of a plane.
use std::{fmt, ops::Not, slice};
/// The axes of a plane.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(
feature = "impl-serde",
derive(serde::Serialize, serde::Deserialize)
)]
pub enum Axis {
/// The Y axis (changes vertically).
Y,
/// The X axis (changes horizontally).
X,
}
impl Axis {
/// List of all possible axes. Please note that this requires no
/// heap-allocation and is very cheap.
pub const ALL: [Axis; 2] = [Axis::Y, Axis::X];
/// Iterator over all axes.
///
/// # Examples
///
/// Note that these examples put the axes in a vector, but if you want an
/// array of axes, just use [`Axis::ALL`].
///
/// ## Default Order
/// ```rust
/// use gardiz::axis::{self, Axis};
///
/// # fn main() {
/// let axes: Vec<Axis> = Axis::iter().collect();
/// assert_eq!(vec![Axis::Y, Axis::X], axes);
/// # }
/// ```
///
/// ## Reverse Order
/// ```rust
/// use gardiz::axis::{self, Axis};
///
/// # fn main() {
/// let axes: Vec<Axis> = Axis::iter().rev().collect();
/// assert_eq!(vec![Axis::X, Axis::Y], axes);
/// # }
/// ```
pub fn iter() -> Iter {
Iter { inner: Self::ALL.iter() }
}
}
impl fmt::Display for Axis {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.write_str(match self {
Axis::Y => "y",
Axis::X => "x",
})
}
}
impl Not for Axis {
type Output = Axis;
fn not(self) -> Self::Output {
match self {
Axis::Y => Axis::X,
Axis::X => Axis::Y,
}
}
}
/// Iterator over all axes. See [`Axis::iter`].
#[derive(Debug, Clone)]
pub struct Iter {
inner: slice::Iter<'static, Axis>,
}
impl Iterator for Iter {
type Item = Axis;
fn next(&mut self) -> Option<Self::Item> {
self.inner.next().copied()
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.inner.size_hint()
}
}
impl DoubleEndedIterator for Iter {
fn next_back(&mut self) -> Option<Self::Item> {
self.inner.next_back().copied()
}
}
impl ExactSizeIterator for Iter {}