Skip to main content

ZeroOne

Struct ZeroOne 

Source
pub struct ZeroOne<T>(/* private fields */);
Expand description

A floating-point number which is within the range +0 to +1 (inclusive).

This may be used for alpha blending, reflectance, lerps, and anything else where values outside the range 0 to 1 is meaningless. It is closed under multiplication.

Because NaN and negative zero are excluded, this type implements Eq straightforwardly, and can reliably be used as a map key for caching, interning, etc.

Implementations§

Source§

impl<T: FloatCore> ZeroOne<T>

Source

pub const unsafe fn new_unchecked(value: T) -> Self

Construct ZeroOne without checking the value.

§Safety

value must be +0, 1, or some value in between those two. Note that value >= 0. && value <= 1. is not a sufficient condition, because it does not exclude negative zero.

Source

pub const fn into_inner(self) -> T

Unwraps the value without modifying it.

Source

pub fn complement(self) -> Self

Returns 1.0 - self.

The result cannot be out of range, so this operation is always successful.

Source§

impl<T: FloatCore + ConstZero + ConstOne> ZeroOne<T>

Source

pub const ZERO: Self

The number zero, as a constant.

Source

pub const ONE: Self

The number one, as a constant.

Source§

impl ZeroOne<f32>

Source

pub const fn new_strict(value: f32) -> Self

Wraps the given value in ZeroOne.

  • If value is in range, returns wrapped value.
  • If value is zero of either sign, returns wrapped positive zero. This is lossy, but corresponds to the IEEE 754 idea that -0.0 == +0.0.
  • If value is out of range or NaN, panics.
Source

pub const fn new_clamped(value: f32) -> Self

Wraps the given value in ZeroOne.

  • If the value is NaN, panics.
  • If the value is out of range, replaces it with the nearest in-range value (0 or 1).
Source

pub const fn is_zero(self) -> bool

Const version of self == ZeroOne::ZERO

Source

pub const fn is_one(self) -> bool

Const version of self == ZeroOne::ONE

Source§

impl ZeroOne<f64>

Source

pub const fn new_strict(value: f64) -> Self

Wraps the given value in ZeroOne.

  • If value is in range, returns wrapped value.
  • If value is zero of either sign, returns wrapped positive zero. This is lossy, but corresponds to the IEEE 754 idea that -0.0 == +0.0.
  • If value is out of range or NaN, panics.
Source

pub const fn new_clamped(value: f64) -> Self

Wraps the given value in ZeroOne.

  • If the value is NaN, panics.
  • If the value is out of range, replaces it with the nearest in-range value (0 or 1).
Source

pub const fn is_zero(self) -> bool

Const version of self == ZeroOne::ZERO

Source

pub const fn is_one(self) -> bool

Const version of self == ZeroOne::ONE

Trait Implementations§

Source§

impl<T> AsRef<T> for ZeroOne<T>

Source§

fn as_ref(&self) -> &T

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<T: Clone> Clone for ZeroOne<T>

Source§

fn clone(&self) -> ZeroOne<T>

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<T: FloatCore + ConstOne> ConstOne for ZeroOne<T>

Source§

const ONE: Self

The multiplicative identity element of Self, 1.
Source§

impl<T: Copy> Copy for ZeroOne<T>

Source§

impl<T: Debug> Debug for ZeroOne<T>

Source§

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

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

impl<T: FloatCore + Zero> Default for ZeroOne<T>

Source§

fn default() -> Self

The default is zero, regardless of what T::default() is.

Source§

impl<T: Display> Display for ZeroOne<T>

Source§

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

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

impl<T: PartialEq> Eq for ZeroOne<T>

Source§

impl From<ZeroOne<f32>> for NotNan<f32>

Source§

fn from(value: ZeroOne<f32>) -> Self

Converts to this type from the input type.
Source§

impl From<ZeroOne<f32>> for f32

Source§

fn from(value: ZeroOne<f32>) -> Self

Converts to this type from the input type.
Source§

impl From<ZeroOne<f32>> for PositiveSign<f32>

Source§

fn from(value: ZeroOne<f32>) -> Self

Converts to this type from the input type.
Source§

impl From<ZeroOne<f64>> for NotNan<f64>

Source§

fn from(value: ZeroOne<f64>) -> Self

Converts to this type from the input type.
Source§

impl From<ZeroOne<f64>> for f64

Source§

fn from(value: ZeroOne<f64>) -> Self

Converts to this type from the input type.
Source§

impl From<ZeroOne<f64>> for PositiveSign<f64>

Source§

fn from(value: ZeroOne<f64>) -> Self

Converts to this type from the input type.
Source§

impl<T: FloatCore> From<bool> for ZeroOne<T>

Source§

fn from(value: bool) -> Self

Converts to this type from the input type.
Source§

impl<T: PrimitiveFloat> Hash for ZeroOne<T>

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<T: FloatCore + Mul<Output = T>> Mul for ZeroOne<T>

Source§

type Output = ZeroOne<T>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Self) -> Self::Output

Performs the * operation. Read more
Source§

impl<T: FloatCore> Mul<PositiveSign<T>> for ZeroOne<T>
where PositiveSign<T>: From<ZeroOne<T>>,

Source§

type Output = PositiveSign<T>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: PositiveSign<T>) -> Self::Output

Performs the * operation. Read more
Source§

impl<T: FloatCore> Mul<ZeroOne<T>> for PositiveSign<T>
where PositiveSign<T>: From<ZeroOne<T>>,

Source§

type Output = PositiveSign<T>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: ZeroOne<T>) -> Self::Output

Performs the * operation. Read more
Source§

impl Mul<ZeroOne<f32>> for Rgb

Source§

fn mul(self, scalar: ZeroOne<f32>) -> Self

Multiplies this color value by a scalar.

Source§

type Output = Rgb

The resulting type after applying the * operator.
Source§

impl Mul<ZeroOne<f32>> for Rgb01

Source§

fn mul(self, scalar: ZeroOne<f32>) -> Self

Multiplies this color value by a scalar.

Source§

type Output = Rgb01

The resulting type after applying the * operator.
Source§

impl<T: FloatCore + Mul<Output = T>> MulAssign for ZeroOne<T>

Source§

fn mul_assign(&mut self, rhs: Self)

Performs the *= operation. Read more
Source§

impl<T: FloatCore + One> One for ZeroOne<T>

Source§

fn one() -> Self

Returns the multiplicative identity element of Self, 1. Read more
Source§

fn set_one(&mut self)

Sets self to the multiplicative identity element of Self, 1.
Source§

fn is_one(&self) -> bool
where Self: PartialEq,

Returns true if self is equal to the multiplicative identity. Read more
Source§

impl<T: FloatCore + PartialOrd> Ord for ZeroOne<T>

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<T: PartialEq> PartialEq for ZeroOne<T>

Source§

fn eq(&self, other: &ZeroOne<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · 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<T: PartialEq> PartialEq<T> for ZeroOne<T>

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · 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<T: FloatCore + PartialOrd> PartialOrd for ZeroOne<T>

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · Source§

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

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

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

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

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

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<T: PartialOrd> PartialOrd<T> for ZeroOne<T>

Source§

fn partial_cmp(&self, other: &T) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · Source§

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

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

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

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

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

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<T> StructuralPartialEq for ZeroOne<T>

Source§

impl TryFrom<f32> for ZeroOne<f32>

Source§

fn try_from(value: f32) -> Result<Self, Self::Error>

Checks that value is within the range 0 to 1.

  • If value > 0 and value ≤ 1, returns wrapped value.
  • If value is zero of either sign, returns wrapped positive zero. This is lossy, but corresponds to the IEEE 754 idea that -0.0 == +0.0.
  • If value is out of range or NaN, returns an error.
Source§

type Error = NotZeroOne<f32>

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

impl TryFrom<f64> for ZeroOne<f64>

Source§

fn try_from(value: f64) -> Result<Self, Self::Error>

Checks that value is within the range 0 to 1.

  • If value > 0 and value ≤ 1, returns wrapped value.
  • If value is zero of either sign, returns wrapped positive zero. This is lossy, but corresponds to the IEEE 754 idea that -0.0 == +0.0.
  • If value is out of range or NaN, returns an error.
Source§

type Error = NotZeroOne<f64>

The type returned in the event of a conversion error.

Auto Trait Implementations§

§

impl<T> Freeze for ZeroOne<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for ZeroOne<T>
where T: RefUnwindSafe,

§

impl<T> Send for ZeroOne<T>
where T: Send,

§

impl<T> Sync for ZeroOne<T>
where T: Sync,

§

impl<T> Unpin for ZeroOne<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for ZeroOne<T>
where T: UnsafeUnpin,

§

impl<T> UnwindSafe for ZeroOne<T>
where T: UnwindSafe,

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

Source§

fn az<Dst>(self) -> Dst
where T: Cast<Dst>,

Casts the value.
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<Src, Dst> CastFrom<Src> for Dst
where Src: Cast<Dst>,

Source§

fn cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> CheckedAs for T

Source§

fn checked_as<Dst>(self) -> Option<Dst>
where T: CheckedCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> CheckedCastFrom<Src> for Dst
where Src: CheckedCast<Dst>,

Source§

fn checked_cast_from(src: Src) -> Option<Dst>

Casts the value.
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> Comparable<K> for Q
where Q: Ord + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn compare(&self, key: &K) -> Ordering

Compare self to key and return their ordering.
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> One for T
where T: One,

Source§

fn one() -> T

Source§

impl<T> OverflowingAs for T

Source§

fn overflowing_as<Dst>(self) -> (Dst, bool)
where T: OverflowingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> OverflowingCastFrom<Src> for Dst
where Src: OverflowingCast<Dst>,

Source§

fn overflowing_cast_from(src: Src) -> (Dst, bool)

Casts the value.
Source§

impl<T> SaturatingAs for T

Source§

fn saturating_as<Dst>(self) -> Dst
where T: SaturatingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> SaturatingCastFrom<Src> for Dst
where Src: SaturatingCast<Dst>,

Source§

fn saturating_cast_from(src: Src) -> Dst

Casts the value.
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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.
Source§

impl<T> UnwrappedAs for T

Source§

fn unwrapped_as<Dst>(self) -> Dst
where T: UnwrappedCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> UnwrappedCastFrom<Src> for Dst
where Src: UnwrappedCast<Dst>,

Source§

fn unwrapped_cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> WrappingAs for T

Source§

fn wrapping_as<Dst>(self) -> Dst
where T: WrappingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> WrappingCastFrom<Src> for Dst
where Src: WrappingCast<Dst>,

Source§

fn wrapping_cast_from(src: Src) -> Dst

Casts the value.