pub struct XY<T> {
pub x: T,
pub y: T,
}
Expand description
A generic structure with a value for each axis.
Fields
x: T
X-axis value
y: T
Y-axis value
Implementations
sourceimpl XY<Offset>
impl XY<Offset>
sourcepub fn absolute<T>(offset: T) -> XY<Offset>where
T: Into<XY<usize>>,
pub fn absolute<T>(offset: T) -> XY<Offset>where
T: Into<XY<usize>>,
Returns a position absolute on both axis.
sourcepub fn parent<T>(offset: T) -> XY<Offset>where
T: Into<XY<isize>>,
pub fn parent<T>(offset: T) -> XY<Offset>where
T: Into<XY<isize>>,
Returns a position relative to the parent on both axis.
sourcepub fn compute_offset<S, A, P>(
&self,
size: S,
available: A,
parent: P
) -> XY<usize>where
S: Into<XY<usize>>,
A: Into<XY<usize>>,
P: Into<XY<usize>>,
pub fn compute_offset<S, A, P>(
&self,
size: S,
available: A,
parent: P
) -> XY<usize>where
S: Into<XY<usize>>,
A: Into<XY<usize>>,
P: Into<XY<usize>>,
Computes the offset required to draw a view.
When drawing a view with size
in a container with available
,
and a parent with the absolute coordinates parent
, drawing the
child with its top-left corner at the returned coordinates will
position him appropriately.
sourceimpl XY<usize>
impl XY<usize>
sourcepub fn max_value() -> XY<usize>
pub fn max_value() -> XY<usize>
Returns a Vec2
with usize::max_value()
in each axis.
Examples
assert!(Vec2::new(9999, 9999) < Vec2::max_value());
sourcepub fn saturating_sub<O>(&self, other: O) -> XY<usize>where
O: Into<XY<usize>>,
pub fn saturating_sub<O>(&self, other: O) -> XY<usize>where
O: Into<XY<usize>>,
Saturating subtraction. Computes self - other
, saturating at 0.
Never panics.
Examples
let u = Vec2::new(1, 2);
let v = Vec2::new(2, 1);
assert_eq!(u.saturating_sub(v), Vec2::new(0, 1));
sourcepub fn saturating_add<O>(&self, other: O) -> XY<usize>where
O: Into<XY<isize>>,
pub fn saturating_add<O>(&self, other: O) -> XY<usize>where
O: Into<XY<isize>>,
Saturating addition with a signed vec.
Any coordinates saturates to 0.
Examples
let u = Vec2::new(1, 2);
let v = XY::<isize>::new(-2, 1);
assert_eq!(u.saturating_add(v), Vec2::new(0, 3));
sourcepub fn checked_add<O>(&self, other: O) -> Option<XY<usize>>where
O: Into<XY<isize>>,
pub fn checked_add<O>(&self, other: O) -> Option<XY<usize>>where
O: Into<XY<isize>>,
Checked addition with a signed vec.
Will return None
if any coordinates exceeds bounds.
sourcepub fn div_up<O>(&self, other: O) -> XY<usize>where
O: Into<XY<usize>>,
pub fn div_up<O>(&self, other: O) -> XY<usize>where
O: Into<XY<usize>>,
Term-by-term integer division that rounds up.
Examples
let u = Vec2::new(1, 6);
let v = Vec2::new(2, 3);
assert_eq!(u.div_up(v), Vec2::new(1, 2));
sourcepub fn checked_sub<O>(&self, other: O) -> Option<XY<usize>>where
O: Into<XY<usize>>,
pub fn checked_sub<O>(&self, other: O) -> Option<XY<usize>>where
O: Into<XY<usize>>,
Checked subtraction. Computes self - other
if possible.
Returns None
if self.x < other.x || self.y < other.y
.
Never panics.
Examples
let xy = Vec2::new(1, 2);
assert_eq!(xy.checked_sub((1, 1)), Some(Vec2::new(0, 1)));
assert_eq!(xy.checked_sub((2, 2)), None);
sourceimpl<T> XY<T>where
T: Ord,
impl<T> XY<T>where
T: Ord,
sourcepub fn fits_in<O>(&self, other: O) -> boolwhere
O: Into<XY<T>>,
pub fn fits_in<O>(&self, other: O) -> boolwhere
O: Into<XY<T>>,
Returns true
if self
could fit inside other
.
Shortcut for self.x <= other.x && self.y <= other.y
.
If this returns true
, then other - self
will not underflow.
Examples
let v = Vec2::new(1, 2);
assert!(v.fits_in((1, 2)));
assert!(v.fits_in((3, 3)));
assert!(!v.fits_in((2, 1)));
sourcepub fn fits<O>(&self, other: O) -> boolwhere
O: Into<XY<T>>,
pub fn fits<O>(&self, other: O) -> boolwhere
O: Into<XY<T>>,
Returns true
if other
could fit inside self
.
Shortcut for self.x >= other.x && self.y >= other.y
.
If this returns true
, then self - other
will not underflow.
Examples
let v = Vec2::new(1, 2);
assert!(v.fits((1, 2)));
assert!(v.fits((0, 0)));
assert!(!v.fits((2, 1)));
sourcepub fn strictly_lt<O>(&self, other: O) -> boolwhere
O: Into<XY<T>>,
pub fn strictly_lt<O>(&self, other: O) -> boolwhere
O: Into<XY<T>>,
Returns true
if other
is strictly less than self
in each axis.
sourcepub fn strictly_gt<O>(&self, other: O) -> boolwhere
O: Into<XY<T>>,
pub fn strictly_gt<O>(&self, other: O) -> boolwhere
O: Into<XY<T>>,
Returns true
if other
is strictly greater than self
in each axis.
sourcepub fn max<A, B>(a: A, b: B) -> XY<T>where
A: Into<XY<T>>,
B: Into<XY<T>>,
pub fn max<A, B>(a: A, b: B) -> XY<T>where
A: Into<XY<T>>,
B: Into<XY<T>>,
Returns a new Vec2 that is a maximum per coordinate.
Examples
assert_eq!(Vec2::max((1, 2), (3, 1)), Vec2::new(3, 2));
sourcepub fn min<A, B>(a: A, b: B) -> XY<T>where
A: Into<XY<T>>,
B: Into<XY<T>>,
pub fn min<A, B>(a: A, b: B) -> XY<T>where
A: Into<XY<T>>,
B: Into<XY<T>>,
Returns a new Vec2 that is no larger than any input in both dimensions.
Examples
assert_eq!(Vec2::min((1, 2), (3, 1)), Vec2::new(1, 1));
sourceimpl<T> XY<T>where
T: Ord + Add<T, Output = T> + Clone,
impl<T> XY<T>where
T: Ord + Add<T, Output = T> + Clone,
sourcepub fn stack_vertical(&self, other: &XY<T>) -> XY<T>
pub fn stack_vertical(&self, other: &XY<T>) -> XY<T>
Returns (max(self.x,other.x), self.y+other.y)
sourcepub fn stack_horizontal(&self, other: &XY<T>) -> XY<T>
pub fn stack_horizontal(&self, other: &XY<T>) -> XY<T>
Returns (self.x+other.x, max(self.y,other.y))
sourceimpl<T> XY<T>where
T: Zero + Clone,
impl<T> XY<T>where
T: Zero + Clone,
sourcepub fn keep_x(&self) -> XY<T>
pub fn keep_x(&self) -> XY<T>
Returns a vector with the X component of self, and y=0.
Examples
let xy = XY::new(1, 2);
assert_eq!(xy.keep_x(), XY::new(1, 0));
sourceimpl<T> XY<T>
impl<T> XY<T>
sourcepub fn fold<U, F>(self, f: F) -> Uwhere
F: FnOnce(T, T) -> U,
pub fn fold<U, F>(self, f: F) -> Uwhere
F: FnOnce(T, T) -> U,
Returns f(self.x, self.y)
Examples
let xy = XY::new(1, 2);
assert_eq!(xy.fold(std::ops::Add::add), 3);
assert_eq!(xy.fold(std::ops::Mul::mul), 2);
assert_eq!(xy.fold(|x, y| x < y), true);
sourcepub fn map<U, F>(self, f: F) -> XY<U>where
F: Fn(T) -> U,
pub fn map<U, F>(self, f: F) -> XY<U>where
F: Fn(T) -> U,
Creates a new XY
by applying f
to x
and y
.
Examples
let xy = XY::new(1, 2);
assert_eq!(xy.map(|v| v * 2), XY::new(2, 4));
assert_eq!(xy.map(|v| v > 1), XY::new(false, true));
sourcepub fn map_if<F>(self, condition: XY<bool>, f: F) -> XY<T>where
F: Fn(T) -> T,
pub fn map_if<F>(self, condition: XY<bool>, f: F) -> XY<T>where
F: Fn(T) -> T,
Applies f
on axis where condition
is true.
Carries over self
otherwise.
Examples
let xy = XY::new(1, 2);
let cond = XY::new(true, false);
assert_eq!(xy.map_if(cond, |v| v * 3), XY::new(3, 2));
sourcepub fn run_if<F, U>(self, condition: XY<bool>, f: F) -> XY<Option<U>>where
F: FnMut(T) -> U,
pub fn run_if<F, U>(self, condition: XY<bool>, f: F) -> XY<Option<U>>where
F: FnMut(T) -> U,
Applies f
on axis where condition
is true.
Returns None
otherwise.
Examples
let xy = XY::new(1, 2);
let cond = XY::new(true, false);
assert_eq!(xy.run_if(cond, |v| v * 3), XY::new(Some(3), None));
sourcepub fn map_x<F>(self, f: F) -> XY<T>where
F: FnOnce(T) -> T,
pub fn map_x<F>(self, f: F) -> XY<T>where
F: FnOnce(T) -> T,
Creates a new XY
by applying f
to x
, and carrying y
over.
Examples
let xy = XY::new(1, 2);
assert_eq!(xy.map_x(|x| x * 10), XY::new(10, 2));
sourcepub fn map_y<F>(self, f: F) -> XY<T>where
F: FnOnce(T) -> T,
pub fn map_y<F>(self, f: F) -> XY<T>where
F: FnOnce(T) -> T,
Creates a new XY
by applying f
to y
, and carrying x
over.
Examples
let xy = XY::new(1, 2);
assert_eq!(xy.map_y(|y| y * 10), XY::new(1, 20));
sourcepub fn pair(self) -> (T, T)
pub fn pair(self) -> (T, T)
Destructure self into a pair.
Examples
let xy = XY::new(1, 2);
let (x, y) = xy.pair();
assert_eq!((x, y), (1, 2));
sourcepub fn as_ref(&self) -> XY<&T>
pub fn as_ref(&self) -> XY<&T>
Returns a XY
with references to this one’s values.
Examples
fn longer(xy: &XY<String>, l: usize) -> XY<bool> {
// `XY::map` takes ownership
// So we need to get a XY<&String> from a &XY<String>
let by_ref: XY<&String> = xy.as_ref();
by_ref.map(|s| s.len() > l)
}
let xy = XY::new(String::from("a"), String::from("bbb"));
assert_eq!(longer(&xy, 2), XY::new(false, true));
sourcepub fn as_ref_mut(&mut self) -> XY<&mut T>
pub fn as_ref_mut(&mut self) -> XY<&mut T>
Returns a XY
with mutable references to this one’s values.
sourcepub fn iter(&self) -> Chain<Once<&T>, Once<&T>>ⓘNotable traits for Chain<A, B>impl<A, B> Iterator for Chain<A, B>where
A: Iterator,
B: Iterator<Item = <A as Iterator>::Item>, type Item = <A as Iterator>::Item;
pub fn iter(&self) -> Chain<Once<&T>, Once<&T>>ⓘNotable traits for Chain<A, B>impl<A, B> Iterator for Chain<A, B>where
A: Iterator,
B: Iterator<Item = <A as Iterator>::Item>, type Item = <A as Iterator>::Item;
A: Iterator,
B: Iterator<Item = <A as Iterator>::Item>, type Item = <A as Iterator>::Item;
Creates an iterator that returns references to x
, then y
.
Examples
let xy = XY::new(1, 2);
let vec: Vec<bool> = xy.iter().map(|&i| i > 1).collect();
assert_eq!(vec, vec![false, true]);
sourcepub fn get(&self, o: Orientation) -> &T
pub fn get(&self, o: Orientation) -> &T
Returns a reference to the value on the given axis.
Examples
let xy = XY::new(1, 2);
assert_eq!(xy.get(Orientation::Horizontal), &1);
assert_eq!(xy.get(Orientation::Vertical), &2);
sourcepub fn get_mut(&mut self, o: Orientation) -> &mut T
pub fn get_mut(&mut self, o: Orientation) -> &mut T
Returns a mutable reference to the value on the given axis.
Examples
let mut xy = XY::new(1, 2);
*xy.get_mut(Orientation::Horizontal) = 10;
assert_eq!(xy, XY::new(10, 2));
sourcepub fn zip<U>(self, other: XY<U>) -> XY<(T, U)>
pub fn zip<U>(self, other: XY<U>) -> XY<(T, U)>
Returns a new XY
of tuples made by zipping self
and other
.
Examples
let a = XY::new(1, 2);
let b = XY::new(true, false);
assert_eq!(a.zip(b), XY::new((1, true), (2, false)));
sourcepub fn zip3<U, V>(self, a: XY<U>, b: XY<V>) -> XY<(T, U, V)>
pub fn zip3<U, V>(self, a: XY<U>, b: XY<V>) -> XY<(T, U, V)>
Returns a new XY
of tuples made by zipping self
, a
and b
.
Examples
let a = XY::new(1, 2);
let b = XY::new(true, false);
let c = XY::new("x", "y");
assert_eq!(a.zip3(b, c), XY::new((1, true, "x"), (2, false, "y")));
sourcepub fn zip4<U, V, W>(self, a: XY<U>, b: XY<V>, c: XY<W>) -> XY<(T, U, V, W)>
pub fn zip4<U, V, W>(self, a: XY<U>, b: XY<V>, c: XY<W>) -> XY<(T, U, V, W)>
Returns a new XY
of tuples made by zipping self
, a
, b
and c
.
Examples
let a = XY::new(1, 2);
let b = XY::new(true, false);
let c = XY::new("x", "y");
let d = XY::new(vec![1], vec![2, 3, 4]);
assert_eq!(
XY::zip4(a, b, c, d),
XY::new((1, true, "x", vec![1]), (2, false, "y", vec![2, 3, 4]))
);
sourcepub fn zip5<U, V, W, Z>(
self,
a: XY<U>,
b: XY<V>,
c: XY<W>,
d: XY<Z>
) -> XY<(T, U, V, W, Z)>
pub fn zip5<U, V, W, Z>(
self,
a: XY<U>,
b: XY<V>,
c: XY<W>,
d: XY<Z>
) -> XY<(T, U, V, W, Z)>
Returns a new XY
of tuples made by zipping self
, a
, b
, c
and d
.
Examples
let a = XY::new(1, 2);
let b = XY::new(true, false);
let c = XY::new("x", "y");
let d = XY::new(vec![1], vec![2, 3, 4]);
let e = XY::new('a', 'b');
let xy: XY<Option<char>> =
XY::zip5(a, b, c, d, e).map(|(a, b, c, d, e)| {
if b && d.contains(&a) {
Some(e)
} else {
c.chars().next()
}
});
assert_eq!(xy, XY::new(Some('a'), Some('y')));
sourcepub fn zip_map<U, V, F>(self, other: XY<U>, f: F) -> XY<V>where
F: FnMut(T, U) -> V,
pub fn zip_map<U, V, F>(self, other: XY<U>, f: F) -> XY<V>where
F: FnMut(T, U) -> V,
Returns a new XY
by calling f
on self
and other
for each axis.
Examples
let a = XY::new((1, 10), (2, 20));
let b = XY::new(true, false);
let xy = a.zip_map(b, |(a1, a2), b| if b { a1 } else { a2 });
assert_eq!(xy, XY::new(1, 20));
sourceimpl<T> XY<T>where
T: Clone,
impl<T> XY<T>where
T: Clone,
sourcepub fn with_axis(&self, o: Orientation, value: T) -> XY<T>
pub fn with_axis(&self, o: Orientation, value: T) -> XY<T>
Returns a new XY
with the axis o
set to value
.
Examples
let xy = XY::new(1, 2).with_axis(Orientation::Horizontal, 42);
assert_eq!(xy, XY::new(42, 2));
sourcepub fn with_axis_from(&self, o: Orientation, other: &XY<T>) -> XY<T>
pub fn with_axis_from(&self, o: Orientation, other: &XY<T>) -> XY<T>
Returns a new XY
with the axis o
set to the value from other
.
Examples
let other = XY::new(3, 4);
let xy = XY::new(1, 2).with_axis_from(Orientation::Horizontal, &other);
assert_eq!(xy, XY::new(3, 2));
sourcepub fn set_axis_from(&mut self, o: Orientation, other: &XY<T>)
pub fn set_axis_from(&mut self, o: Orientation, other: &XY<T>)
Sets the axis o
on self
to the value from other
.
Examples
let mut xy = XY::new(1, 2);
let other = XY::new(3, 4);
xy.set_axis_from(Orientation::Horizontal, &other);
assert_eq!(xy, XY::new(3, 2));
sourceimpl<T> XY<Option<T>>
impl<T> XY<Option<T>>
sourceimpl XY<bool>
impl XY<bool>
sourcepub fn any(self) -> bool
pub fn any(self) -> bool
Returns true
if any of x
or y
is true
.
Examples
assert_eq!(XY::new(true, false).any(), true);
assert_eq!(XY::new(false, false).any(), false);
assert_eq!(XY::new('a', 'b').map(|c| c == 'a').any(), true);
sourcepub fn both(self) -> bool
pub fn both(self) -> bool
Returns true
if both x
and y
are true
.
Examples
assert_eq!(XY::new(true, false).both(), false);
assert_eq!(XY::new(true, true).both(), true);
assert_eq!(XY::new("abc", "de").map(|s| s.len() > 2).both(), false);
sourcepub fn select<T>(self, other: XY<T>) -> XY<Option<T>>
pub fn select<T>(self, other: XY<T>) -> XY<Option<T>>
For each axis, keeps elements from other
if self
is true
.
Examples
let choice = XY::new(true, false);
let values = XY::new(1, 2);
let selection = choice.select(values);
assert_eq!(selection, XY::new(Some(1), None));
sourcepub fn select_or<T>(self, if_true: XY<T>, if_false: XY<T>) -> XY<T>
pub fn select_or<T>(self, if_true: XY<T>, if_false: XY<T>) -> XY<T>
For each axis, selects if_true
if self
is true, else if_false
.
Examples
let choice = XY::new(true, false);
let values = XY::new(1, 2);
let fallback = XY::new(3, 4);
let selection = choice.select_or(values, fallback);
assert_eq!(selection, XY::new(1, 4));
Trait Implementations
sourceimpl Index<XY<usize>> for ObservedScreen
impl Index<XY<usize>> for ObservedScreen
sourceimpl Index<XY<usize>> for dyn ObservedPieceInterface
impl Index<XY<usize>> for dyn ObservedPieceInterface
sourceimpl IndexMut<XY<usize>> for ObservedScreen
impl IndexMut<XY<usize>> for ObservedScreen
sourceimpl<T> IntoIterator for XY<T>
impl<T> IntoIterator for XY<T>
sourceimpl<T> PartialEq<XY<T>> for XY<T>where
T: PartialEq<T>,
impl<T> PartialEq<XY<T>> for XY<T>where
T: PartialEq<T>,
sourceimpl<T> PartialOrd<XY<T>> for XY<T>where
T: PartialOrd<T>,
impl<T> PartialOrd<XY<T>> for XY<T>where
T: PartialOrd<T>,
sourcefn partial_cmp(&self, other: &XY<T>) -> Option<Ordering>
fn partial_cmp(&self, other: &XY<T>) -> Option<Ordering>
a < b
<=> a.x < b.x && a.y < b.y
1.0.0 · sourcefn lt(&self, other: &Rhs) -> bool
fn lt(&self, other: &Rhs) -> bool
This method tests less than (for self
and other
) and is used by the <
operator. Read more
1.0.0 · sourcefn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
impl<T> Copy for XY<T>where
T: Copy,
impl<T> Eq for XY<T>where
T: Eq,
impl<T> StructuralEq for XY<T>
impl<T> StructuralPartialEq for XY<T>
Auto Trait Implementations
impl<T> RefUnwindSafe for XY<T>where
T: RefUnwindSafe,
impl<T> Send for XY<T>where
T: Send,
impl<T> Sync for XY<T>where
T: Sync,
impl<T> Unpin for XY<T>where
T: Unpin,
impl<T> UnwindSafe for XY<T>where
T: UnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more