pub struct Rect {
pub a: Point,
pub b: Point,
}Expand description
A rectangle defined by its top-left corner a (inclusive) and bottom-right
corner b (exclusive).
Geometry primitives: Point and Rect.
Coordinates are i32. Signed is required: view origins go negative when
scrolled offscreen, and the translate/inflate operations take negative deltas.
Conversion to the unsigned buffer index space happens at the screen boundary.
§Keyword-collision renames
Two Rect methods would collide with Rust keywords, so they use raw
identifiers: Rect::r#move (translate) and Rect::r#union (bounding box).
Call sites read rect.r#move(1, 2) / rect.r#union(&other).
The half-open convention ([a, b)) means b is one cell past the last
occupied column/row — the same model used by Rust ranges and ratatui.
The width is b.x - a.x; the height is b.y - a.y.
The mutating methods take &mut self and return &mut Self, so they chain
and operate in place:
let mut r = Rect::new(0, 0, 10, 10);
r.grow(-1, -1).r#move(1, 1);
assert_eq!(r, Rect::new(2, 2, 10, 10));§Turbo Vision heritage
Ports TPoint/TRect (objects.h). The mutating methods return &mut Self
to mirror the original TRect& chaining idiom.
Ports TRect from objects.h. The two raw-identifier methods
r#move and r#union use raw identifiers
to avoid collisions with the Rust keywords move and union.
Fields§
§a: PointTop-left corner of the rectangle (inclusive).
a is the first cell that belongs to the rectangle. Both a.x and
a.y are included in contains tests. You may read
or write this field directly; if you set a > b the rectangle becomes
empty (see is_empty) but is otherwise valid.
b: PointBottom-right corner of the rectangle (exclusive).
b is one cell past the last occupied column (b.x) and the last
occupied row (b.y). The rectangle width is b.x - a.x; height is
b.y - a.y. You may read or write this field directly.
Implementations§
Source§impl Rect
impl Rect
Sourcepub const fn new(ax: i32, ay: i32, bx: i32, by: i32) -> Self
pub const fn new(ax: i32, ay: i32, bx: i32, by: i32) -> Self
Construct a rectangle from the four corner coordinates (ax, ay)–(bx, by).
Prefer this constructor when you have individual x/y values at hand.
Use from_points when you already hold Point
values. The convention is a = (ax, ay) (top-left, inclusive) and
b = (bx, by) (bottom-right, exclusive).
Sourcepub const fn from_points(p1: Point, p2: Point) -> Self
pub const fn from_points(p1: Point, p2: Point) -> Self
Sourcepub fn move(&mut self, dx: i32, dy: i32) -> &mut Self
pub fn move(&mut self, dx: i32, dy: i32) -> &mut Self
Translate the rectangle by (dx, dy), shifting both corners equally.
Use this to reposition a rectangle without changing its size. Negative
deltas move it left/up; positive deltas move it right/down. Because move
is a Rust keyword, call this method as rect.r#move(dx, dy).
Sourcepub fn grow(&mut self, dx: i32, dy: i32) -> &mut Self
pub fn grow(&mut self, dx: i32, dy: i32) -> &mut Self
Inflate the rectangle symmetrically by (dx, dy).
Expands each edge outward: a moves left/up by (dx, dy) and b moves
right/down by the same amount, so the total width grows by 2*dx and
height by 2*dy. Pass negative values to deflate (shrink) the rectangle;
deflating past zero produces an empty (inverted) rectangle.
Sourcepub fn intersect(&mut self, r: &Rect) -> &mut Self
pub fn intersect(&mut self, r: &Rect) -> &mut Self
Clip the rectangle to the intersection with r.
Replaces self with the largest rectangle that fits inside both self
and r. If the two rectangles do not overlap, the result is empty
(is_empty() returns true). Use this to constrain a child view’s
bounds to its parent’s visible area.
Sourcepub fn union(&mut self, r: &Rect) -> &mut Self
pub fn union(&mut self, r: &Rect) -> &mut Self
Expand the rectangle to the bounding box of self and r.
Replaces self with the smallest rectangle that contains both self
and r. Use this to compute a dirty region that covers multiple views.
Because union is a Rust keyword, call this method as
rect.r#union(&other).
Sourcepub fn contains(&self, p: Point) -> bool
pub fn contains(&self, p: Point) -> bool
Returns true if p lies inside the rectangle.
Uses half-open semantics matching the [a, b) convention: the
left/top edges are included (p.x >= a.x, p.y >= a.y) but the
right/bottom edges are excluded (p.x < b.x, p.y < b.y).
This is the correct test for hit-testing a character-cell grid, where
a point exactly on the right or bottom edge belongs to the next cell.
Trait Implementations§
impl Copy for Rect
impl Eq for Rect
impl StructuralPartialEq for Rect
Auto Trait Implementations§
impl Freeze for Rect
impl RefUnwindSafe for Rect
impl Send for Rect
impl Sync for Rect
impl Unpin for Rect
impl UnsafeUnpin for Rect
impl UnwindSafe for Rect
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can
then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be
further downcast into Rc<ConcreteType> where ConcreteType implements Trait.§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.§impl<T> DowncastSync for T
impl<T> DowncastSync for T
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.