use direction::Orientation; use std::iter; /// A generic structure with a value for each axis. #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub struct XY { /// X-axis value pub x: T, /// Y-axis value pub y: T, } impl IntoIterator for XY { type Item = T; type IntoIter = iter::Chain, iter::Once>; fn into_iter(self) -> Self::IntoIter { iter::once(self.x).chain(iter::once(self.y)) } } impl XY { /// Creates a new `XY` from the given values. pub fn new(x: T, y: T) -> Self { XY { x, y } } /// Swaps the x and y values. pub fn swap(self) -> Self { XY::new(self.y, self.x) } /// Returns `f(self.x, self.y)` pub fn fold(self, f: F) -> U where F: FnOnce(T, T) -> U, { f(self.x, self.y) } /// Creates a new `XY` by applying `f` to `x` and `y`. pub fn map(self, f: F) -> XY where F: Fn(T) -> U, { XY::new(f(self.x), f(self.y)) } /// Applies `f` on axis where `condition` is true. /// /// Carries over `self` otherwise. pub fn map_if(self, condition: XY, f: F) -> Self where F: Fn(T) -> T, { self.zip_map(condition, |v, c| if c { f(v) } else { v }) } /// Applies `f` on axis where `condition` is true. /// /// Returns `None` otherwise. pub fn run_if(self, condition: XY, f: F) -> XY> where F: Fn(T) -> U, { self.zip_map(condition, |v, c| if c { Some(f(v)) } else { None }) } /// Creates a new `XY` by applying `f` to `x`, and carrying `y` over. pub fn map_x(self, f: F) -> Self where F: FnOnce(T) -> T, { XY::new(f(self.x), self.y) } /// Creates a new `XY` by applying `f` to `y`, and carrying `x` over. pub fn map_y(self, f: F) -> Self where F: FnOnce(T) -> T, { XY::new(self.x, f(self.y)) } /// Destructure self into a pair. pub fn pair(self) -> (T, T) { (self.x, self.y) } /// Return a `XY` with references to this one's values. pub fn as_ref(&self) -> XY<&T> { XY::new(&self.x, &self.y) } /// Creates an iterator that returns references to `x`, then `y`. pub fn iter(&self) -> iter::Chain, iter::Once<&T>> { iter::once(&self.x).chain(iter::once(&self.y)) } /// Returns a reference to the value on the given axis. pub fn get(&self, o: Orientation) -> &T { match o { Orientation::Horizontal => &self.x, Orientation::Vertical => &self.y, } } /// Returns a mutable reference to the value on the given axis. pub fn get_mut(&mut self, o: Orientation) -> &mut T { match o { Orientation::Horizontal => &mut self.x, Orientation::Vertical => &mut self.y, } } /// Returns a new `XY` of tuples made by zipping `self` and `other`. pub fn zip(self, other: XY) -> XY<(T, U)> { XY::new((self.x, other.x), (self.y, other.y)) } /// Returns a new `XY` of tuples made by zipping `self`, `a` and `b`. pub fn zip3(self, a: XY, b: XY) -> XY<(T, U, V)> { XY::new((self.x, a.x, b.x), (self.y, a.y, b.y)) } /// Returns a new `XY` of tuples made by zipping `self`, `a`, `b` and `c`. pub fn zip4( self, a: XY, b: XY, c: XY, ) -> XY<(T, U, V, W)> { XY::new((self.x, a.x, b.x, c.x), (self.y, a.y, b.y, c.y)) } /// Returns a new `XY` of tuples made by zipping `self`, `a`, `b`, `c` and `d`. pub fn zip5( self, a: XY, b: XY, c: XY, d: XY, ) -> XY<(T, U, V, W, Z)> { XY::new((self.x, a.x, b.x, c.x, d.x), (self.y, a.y, b.y, c.y, d.y)) } /// Returns a new `XY` by calling `f` on `self` and `other` for each axis. pub fn zip_map(self, other: XY, f: F) -> XY where F: Fn(T, U) -> V, { XY::new(f(self.x, other.x), f(self.y, other.y)) } /// For each axis, keep the element from `self` if `keep` is `true`. pub fn keep(self, keep: XY) -> XY> { keep.select(self) } } impl XY { /// Returns a new `XY` with the axis `o` set to `value`. pub fn with_axis(&self, o: Orientation, value: T) -> Self { let mut new = self.clone(); *o.get_ref(&mut new) = value; new } /// Returns a new `XY` with the axis `o` set to the value from `other`. pub fn with_axis_from(&self, o: Orientation, other: &Self) -> Self { let mut new = self.clone(); new.set_axis_from(o, other); new } /// Sets the axis `o` on `self` to the value from `other`. pub fn set_axis_from(&mut self, o: Orientation, other: &Self) { *o.get_ref(self) = o.get(other); } } impl XY> { /// Returns a new `XY` by calling `unwrap_or` on each axis. pub fn unwrap_or(self, other: XY) -> XY { self.zip_map(other, Option::unwrap_or) } } impl XY { /// Returns `true` if any of `x` or `y` is `true`. pub fn any(self) -> bool { use std::ops::BitOr; self.fold(BitOr::bitor) } /// Returns `true` if both `x` and `y` are `true`. pub fn both(self) -> bool { use std::ops::BitAnd; self.fold(BitAnd::bitand) } /// For each axis, keeps elements from `other` if `self` is `true`. pub fn select(self, other: XY) -> XY> { self.zip_map(other, |keep, o| if keep { Some(o) } else { None }) } /// For each axis, selects `if_true` if `self` is true, else `if_false`. pub fn select_or(self, if_true: XY, if_false: XY) -> XY { self.select(if_true).unwrap_or(if_false) } /// Returns a term-by-term AND operation. pub fn and(self, other: Self) -> Self { self.zip_map(other, |s, o| s && o) } /// Returns a term-by-term OR operation. pub fn or(self, other: Self) -> Self { self.zip_map(other, |s, o| s || o) } } impl XY { /// Creates a `XY` with both `x` and `y` set to `value`. pub fn both_from(value: T) -> Self { XY::new(value, value) } } impl From<(T, T)> for XY { fn from((x, y): (T, T)) -> Self { XY::new(x, y) } } impl From<(XY, XY)> for XY<(T, U)> { fn from((t, u): (XY, XY)) -> Self { t.zip(u) } }