Skip to main content

Rect

Struct Rect 

Source
pub struct Rect {
    pub x: u32,
    pub y: u32,
    pub width: u32,
    pub height: u32,
}
Expand description

An axis-aligned rectangle with u32 coordinates.

Uses u32 rather than u16 to avoid overflow bugs that affect other TUI libraries on large terminals. All coordinates are in terminal columns and rows, with (0, 0) at the top-left.

Note: Rect::right and Rect::bottom return exclusive bounds (one past the last column/row), consistent with Rust range conventions.

Fields§

§x: u32

Left edge column, inclusive.

§y: u32

Top edge row, inclusive.

§width: u32

Width in terminal columns.

§height: u32

Height in terminal rows.

Implementations§

Source§

impl Rect

Source

pub const fn new(x: u32, y: u32, width: u32, height: u32) -> Self

Create a new rectangle from position and size.

Source

pub const fn area(&self) -> u32

Total area in cells (width * height).

Source

pub const fn right(&self) -> u32

Exclusive right edge (x + width).

This is one column past the last column in the rectangle.

Source

pub const fn bottom(&self) -> u32

Exclusive bottom edge (y + height).

This is one row past the last row in the rectangle.

Source

pub const fn is_empty(&self) -> bool

Returns true if the rectangle has zero area (width or height is zero).

Source

pub fn centered(&self, inner_w: u32, inner_h: u32) -> Rect

Returns a smaller Rect centered within self.

If the inner dimensions exceed self’s dimensions, they are clamped to self’s size. The returned rectangle is positioned such that it is centered both horizontally and vertically within self.

§Example
use slt::Rect;
let outer = Rect::new(0, 0, 10, 10);
let inner = outer.centered(4, 4);
assert_eq!(inner, Rect::new(3, 3, 4, 4));
Source

pub fn union(&self, other: Rect) -> Rect

Returns the smallest Rect containing both self and other.

The union encompasses all cells in both rectangles. If either rectangle is empty, the result may have unexpected dimensions; use is_empty() to check.

§Example
use slt::Rect;
let r1 = Rect::new(0, 0, 5, 5);
let r2 = Rect::new(3, 3, 5, 5);
let union = r1.union(r2);
assert_eq!(union, Rect::new(0, 0, 8, 8));
Source

pub fn intersection(&self, other: Rect) -> Option<Rect>

Returns the overlapping region between self and other, or None if they don’t overlap.

Two rectangles overlap if they share at least one cell. Adjacent rectangles (touching at an edge but not overlapping) return None.

§Example
use slt::Rect;
let r1 = Rect::new(0, 0, 5, 5);
let r2 = Rect::new(3, 3, 5, 5);
let overlap = r1.intersection(r2);
assert_eq!(overlap, Some(Rect::new(3, 3, 2, 2)));
Source

pub fn contains(&self, x: u32, y: u32) -> bool

Returns true if the point (x, y) is inside the rectangle.

A point is considered inside if it is within the inclusive left/top bounds and exclusive right/bottom bounds (consistent with Rust range conventions).

§Example
use slt::Rect;
let r = Rect::new(5, 5, 10, 10);
assert!(r.contains(5, 5));   // top-left corner
assert!(r.contains(14, 14)); // inside
assert!(!r.contains(15, 15)); // outside (exclusive right/bottom)
Source

pub fn rows(&self) -> impl Iterator<Item = u32>

Returns an iterator over row y-coordinates in this rectangle.

Yields values from self.y to self.bottom() - 1 (inclusive).

§Example
use slt::Rect;
let r = Rect::new(0, 2, 5, 3);
let rows: Vec<u32> = r.rows().collect();
assert_eq!(rows, vec![2, 3, 4]);
Source

pub fn positions(&self) -> impl Iterator<Item = (u32, u32)>

Returns an iterator over all (x, y) positions in this rectangle, row by row.

Iterates from top-left to bottom-right, filling each row left-to-right before moving to the next row. Total count is width * height.

§Example
use slt::Rect;
let r = Rect::new(0, 0, 2, 2);
let positions: Vec<(u32, u32)> = r.positions().collect();
assert_eq!(positions, vec![(0, 0), (1, 0), (0, 1), (1, 1)]);

Trait Implementations§

Source§

impl Clone for Rect

Source§

fn clone(&self) -> Rect

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
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 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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Copy for Rect

Source§

impl Eq for Rect

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