Skip to main content

Measurement

Struct Measurement 

Source
pub struct Measurement {
    pub minimum: usize,
    pub maximum: usize,
}
Expand description

The minimum and maximum width requirements of a renderable.

Fields§

§minimum: usize

Minimum width required (content won’t fit in less).

§maximum: usize

Maximum width the content would use if given unlimited space.

Implementations§

Source§

impl Measurement

Source

pub fn new(minimum: usize, maximum: usize) -> Self

Create a new measurement.

Source

pub fn exact(width: usize) -> Self

Create a measurement with both min and max set to the same value.

Source

pub fn span(&self) -> usize

Get the span (difference between max and min).

Source

pub fn normalize(&self) -> Self

Normalize the measurement ensuring minimum <= maximum and both >= 0.

Since we use usize, values are always >= 0, but this ensures the minimum does not exceed the maximum.

§Examples
use rich_rs::Measurement;

// Inverted measurement gets corrected
let m = Measurement::new(50, 10);
let normalized = m.normalize();
assert_eq!(normalized.minimum, 10);
assert_eq!(normalized.maximum, 10);
Source

pub fn with_maximum(&self, width: usize) -> Self

Get a measurement where both widths are <= the given width.

§Examples
use rich_rs::Measurement;

let m = Measurement::new(10, 50);
let constrained = m.with_maximum(30);
assert_eq!(constrained.minimum, 10);
assert_eq!(constrained.maximum, 30);

// When width is less than minimum, both get clamped
let m = Measurement::new(20, 50);
let constrained = m.with_maximum(15);
assert_eq!(constrained.minimum, 15);
assert_eq!(constrained.maximum, 15);
Source

pub fn with_minimum(&self, width: usize) -> Self

Get a measurement where both widths are >= the given width.

§Examples
use rich_rs::Measurement;

let m = Measurement::new(10, 50);
let constrained = m.with_minimum(20);
assert_eq!(constrained.minimum, 20);
assert_eq!(constrained.maximum, 50);

// When width is greater than maximum, both get raised
let m = Measurement::new(10, 30);
let constrained = m.with_minimum(40);
assert_eq!(constrained.minimum, 40);
assert_eq!(constrained.maximum, 40);
Source

pub fn clamp_bounds( &self, min_width: Option<usize>, max_width: Option<usize>, ) -> Self

Clamp the measurement within optional min and max bounds.

This clamps the measurement itself (both minimum and maximum fields), not a width value. Use clamp_width to clamp a width within measurement bounds.

§Examples
use rich_rs::Measurement;

let m = Measurement::new(10, 50);

// Clamp with both bounds
let clamped = m.clamp_bounds(Some(15), Some(40));
assert_eq!(clamped.minimum, 15);
assert_eq!(clamped.maximum, 40);

// Clamp with only max bound
let clamped = m.clamp_bounds(None, Some(30));
assert_eq!(clamped.minimum, 10);
assert_eq!(clamped.maximum, 30);

// Clamp with only min bound
let clamped = m.clamp_bounds(Some(20), None);
assert_eq!(clamped.minimum, 20);
assert_eq!(clamped.maximum, 50);
Source

pub fn clamp_width(&self, width: usize) -> usize

Clamp a width value to within the measurement bounds.

Returns a width that is >= minimum and <= maximum.

§Panics

Panics if the measurement invariant is violated (i.e., minimum > maximum). In debug builds, a debug_assert! provides a clearer error message. Use normalize to fix invalid measurements before calling this method.

§Examples
use rich_rs::Measurement;

let m = Measurement::new(10, 50);
assert_eq!(m.clamp_width(5), 10);   // Below minimum
assert_eq!(m.clamp_width(30), 30);  // Within bounds
assert_eq!(m.clamp_width(100), 50); // Above maximum
Source

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

Combine with another measurement, taking the max of mins and maxes.

Source

pub fn from_segments(segments: &Segments) -> Self

Create a measurement from rendered segments.

This is the default measurement strategy: render and measure the result. The minimum is the longest word, maximum is the total width.

Note: This method assumes single-line content. For multi-line content, the maximum will include the total width of all lines combined rather than the width of the widest line. Handle multi-line content by splitting into lines first and taking the union of per-line measurements.

Trait Implementations§

Source§

impl Clone for Measurement

Source§

fn clone(&self) -> Measurement

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 Measurement

Source§

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

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

impl Default for Measurement

Source§

fn default() -> Measurement

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

impl PartialEq for Measurement

Source§

fn eq(&self, other: &Measurement) -> 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 Measurement

Source§

impl Eq for Measurement

Source§

impl StructuralPartialEq for Measurement

Auto Trait Implementations§

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<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

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

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

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

Source§

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.