Struct ChartRange

Source
pub struct ChartRange {
    pub min: f32,
    pub max: f32,
}
Expand description

A numeric domain used to map chart values.

Self::new and Self::sanitized replace non-finite input bounds and order them. They attempt to widen a degenerate domain by adding 1.0 to its maximum, but f32 rounding can leave very large equal bounds unchanged. A span between finite bounds can also overflow to infinity.

§Examples

use ailloli_ui_core::ChartRange;

let range = ChartRange::new(10.0, 30.0);
assert_eq!(range.fraction_for_value(20.0), 0.5);
assert_eq!(range.value_for_fraction(0.25), 15.0);

Fields§

§min: f32

Inclusive lower domain bound.

§max: f32

Inclusive upper domain bound.

Implementations§

Source§

impl ChartRange

Source

pub fn new(min: f32, max: f32) -> Self

Creates a sanitized range from two candidate bounds.

Non-finite min and max become 0.0 and 1.0, respectively; reversed bounds are swapped; and a range no wider than f32::EPSILON is expanded upward by adding 1.0 to its maximum. At magnitudes where that increment is not representable, the bounds can remain equal.

§Examples
use ailloli_ui_core::ChartRange;
assert_eq!(ChartRange::new(4.0, 2.0), ChartRange { min: 2.0, max: 4.0 });
Source

pub fn sanitized(self) -> Self

Returns this range with finite, ascending bounds and attempts to make it non-degenerate.

This applies the same normalization as Self::new.

§Examples
use ailloli_ui_core::ChartRange;
assert_eq!(ChartRange { min: 3.0, max: 3.0 }.sanitized().max, 4.0);
Source

pub fn fraction_for_value(self, value: f32) -> f32

Maps value into the inclusive normalized interval 0.0..=1.0.

Values outside the range are clamped. A non-finite value maps to 0.0. Extreme finite bounds can produce an infinite or zero intermediate span; in that case IEEE-754 arithmetic can produce NaN instead of a fraction.

§Examples
use ailloli_ui_core::ChartRange;
assert_eq!(ChartRange::new(10.0, 30.0).fraction_for_value(20.0), 0.5);
Source

pub fn value_for_fraction(self, fraction: f32) -> f32

Maps a normalized fraction back into this range.

Fractions are clamped to 0.0..=1.0; a non-finite fraction selects Self::min after sanitization. Extreme finite bounds can overflow the intermediate span, so the returned value can be non-finite.

§Examples
use ailloli_ui_core::ChartRange;
assert_eq!(ChartRange::new(10.0, 30.0).value_for_fraction(0.25), 15.0);

Trait Implementations§

Source§

impl Clone for ChartRange

Source§

fn clone(&self) -> ChartRange

Returns a copy 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 ChartRange

Source§

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

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

impl Default for ChartRange

Source§

fn default() -> Self

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

impl PartialEq for ChartRange

Source§

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

Source§

impl StructuralPartialEq for ChartRange

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<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> Same for T

Source§

type Output = T

Should always be Self
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.