glamour/traits/mod.rs
1//! Common convenience traits.
2
3pub mod marker;
4mod transparent;
5
6pub use transparent::*;
7
8/// General trait for the `contains()` method.
9///
10/// Coordinates exactly on the upper bound are considered to be contained.
11///
12/// See also: [`Intersection`], which is different in this regard.
13pub trait Contains<T> {
14 /// Returns `true` if `thing` is inside `self`.
15 fn contains(&self, thing: &T) -> bool;
16}
17
18/// The `intersection()` method.
19///
20/// Coordinates exactly on the upper bound are _not_ considered to be
21/// intersecting.
22///
23/// For example, each adjacent tile of a grid of [`crate::Rect`]s or
24/// [`crate::Box2`]s will not be considered intersecting, while
25/// [`Contains::contains()`] returns true for both tiles with coordinates that
26/// fall exactly on the upper bound of one tile.
27///
28/// See also: [`Contains`], which is different in this regard.
29pub trait Intersection<T = Self> {
30 /// The type of intersection.
31 ///
32 /// For example, a rect/rect intersection is another rect, while a box/box
33 /// intersection is a box, and a rect/box intersection is a rect, but a
34 /// box/rect intersection is a box.
35 type Intersection;
36
37 /// True if `thing` intersects with `self`.
38 fn intersects(&self, thing: &T) -> bool;
39
40 /// If `thing` intersects with `self`, return the intersection. Otherwise,
41 /// returns `None`.
42 fn intersection(&self, thing: &T) -> Option<Self::Intersection>;
43}
44
45/// The `union()` operation.
46pub trait Union<T = Self> {
47 /// The type of the union.
48 type Union;
49
50 /// Compute the union of two things.
51 fn union(self, other: T) -> Self::Union;
52}