2016-07-13 04:01:11 +00:00
|
|
|
use std::iter;
|
|
|
|
|
|
|
|
/// A generic structure with a value for each axis.
|
|
|
|
#[derive(Debug,Clone,Copy,PartialEq)]
|
|
|
|
pub struct XY<T> {
|
|
|
|
/// X-axis value
|
|
|
|
pub x: T,
|
|
|
|
/// Y-axis value
|
|
|
|
pub y: T,
|
|
|
|
}
|
|
|
|
|
2016-07-13 04:33:50 +00:00
|
|
|
impl<T> XY<T> {
|
2016-07-13 04:01:11 +00:00
|
|
|
/// Creates a new `XY` from the given values.
|
|
|
|
pub fn new(x: T, y: T) -> Self {
|
2016-07-13 04:33:50 +00:00
|
|
|
XY { x: x, y: y }
|
2016-07-13 04:01:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// 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<&T>> {
|
|
|
|
iter::once(&self.x).chain(iter::once(&self.y))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-13 04:33:50 +00:00
|
|
|
impl<T: Copy> XY<T> {
|
2016-07-13 04:01:11 +00:00
|
|
|
/// Creates a `XY` with both `x` and `y` set to `value`.
|
|
|
|
pub fn both(value: T) -> Self {
|
|
|
|
XY::new(value, value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-13 04:33:50 +00:00
|
|
|
impl<T> From<(T, T)> for XY<T> {
|
2016-07-13 04:01:11 +00:00
|
|
|
fn from((x, y): (T, T)) -> Self {
|
|
|
|
XY::new(x, y)
|
|
|
|
}
|
|
|
|
}
|