Guide API
Skip to main content

Rect

Struct Rect 

Source
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: Point

Top-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: Point

Bottom-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

Source

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).

Source

pub const fn from_points(p1: Point, p2: Point) -> Self

Construct a rectangle from two Point corners.

Equivalent to new but more ergonomic when you already hold Point values. p1 becomes a (top-left, inclusive) and p2 becomes b (bottom-right, exclusive); no sorting is applied, so passing them in the wrong order yields an empty rectangle.

Source

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).

Source

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.

Source

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.

Source

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).

Source

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.

Source

pub fn is_empty(&self) -> bool

Returns true if the rectangle has zero or negative area.

This is the case when a.x >= b.x or a.y >= b.y — either the width or height is zero. An inverted rectangle (where b < a, e.g. produced by deflating past zero with grow) is also considered empty.

Trait Implementations§

Source§

impl Clone for Rect

Source§

fn clone(&self) -> Rect

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for Rect

Source§

impl Debug for Rect

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Rect

Source§

fn default() -> Rect

Returns the “default value” for a type. Read more
Source§

impl Eq for Rect

Source§

impl Hash for Rect

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for Rect

Source§

fn eq(&self, other: &Rect) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> CustomValue for T
where T: Any + Debug,

Source§

fn as_any_rc(self: Rc<T>) -> Rc<dyn Any>

Upcast Rc<Self> to Rc<dyn Any> so FieldValue::as_custom / FieldValue::value_as can downcast.
§

impl<T> Downcast for T
where T: Any,

§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert 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>

Convert 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)

Convert &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)

Convert &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
where T: Any + Send + Sync,

§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Send + Sync> ⓘ

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.