Struct devela::num::Compare

source ·
#[repr(transparent)]
pub struct Compare<T>(pub T);
Expand description

Provides comparing methods for T, most of them const.

It provides the non-const methods pclamp, pmax, pmin for comparing PartialOrdered values.

It provides the following const methods for comparing primitives: clamp, max, min, eq, ne, lt, le, gt, ge.

In the case of floating-point primitives:

  • they use total ordering.
  • they implement aditional methods: is_positive, is_negative, is_finite, is_infinite, is_nan.
  • methods will only be const with the unsafe_const feature enabled.

Tuple Fields§

§0: T

Implementations§

source§

impl<T: PartialOrd> Compare<T>

source

pub fn pclamp(self, min: T, max: T) -> T

Compares and returns a PartialOrdered value clamped between min and max.

§Examples
assert_eq![0.4, Compare(1.0).pclamp(0.2, 0.4)];
assert_eq![0.2, Compare(0.0).pclamp(0.2, 0.4)];
source

pub fn pmax(self, other: T) -> T

Compares and returns the maximum of two PartialOrdered values.

Complements core::cmp::max which requires Ord

§Examples
assert_eq![0.4, Compare(0.2).pmax(0.4)];
source

pub fn pmin(self, other: T) -> T

Compares and returns the minimum of two PartialOrdered values.

Complements core::cmp::min which requires Ord

§Example
assert_eq![0.2, Compare(0.2).pmin(0.4)];
source§

impl Compare<u8>

source

pub const fn clamp(self, min: u8, max: u8) -> u8

Compares and returns self clamped between min and max.

source

pub const fn max(self, other: u8) -> u8

Compares and returns the maximum between self and other.

source

pub const fn min(self, other: u8) -> u8

Compares and returns the minimum between self and other.

source

pub const fn eq(self, other: u8) -> bool

Returns true if self == other.

source

pub const fn ne(self, other: u8) -> bool

Returns true if self != other.

source

pub const fn lt(self, other: u8) -> bool

Returns true if self < other.

source

pub const fn le(self, other: u8) -> bool

Returns true if self <= other.

source

pub const fn gt(self, other: u8) -> bool

Returns true if self > other.

source

pub const fn ge(self, other: u8) -> bool

Returns true if self >= other.

source§

impl Compare<u16>

source

pub const fn clamp(self, min: u16, max: u16) -> u16

Compares and returns self clamped between min and max.

source

pub const fn max(self, other: u16) -> u16

Compares and returns the maximum between self and other.

source

pub const fn min(self, other: u16) -> u16

Compares and returns the minimum between self and other.

source

pub const fn eq(self, other: u16) -> bool

Returns true if self == other.

source

pub const fn ne(self, other: u16) -> bool

Returns true if self != other.

source

pub const fn lt(self, other: u16) -> bool

Returns true if self < other.

source

pub const fn le(self, other: u16) -> bool

Returns true if self <= other.

source

pub const fn gt(self, other: u16) -> bool

Returns true if self > other.

source

pub const fn ge(self, other: u16) -> bool

Returns true if self >= other.

source§

impl Compare<u32>

source

pub const fn clamp(self, min: u32, max: u32) -> u32

Compares and returns self clamped between min and max.

source

pub const fn max(self, other: u32) -> u32

Compares and returns the maximum between self and other.

source

pub const fn min(self, other: u32) -> u32

Compares and returns the minimum between self and other.

source

pub const fn eq(self, other: u32) -> bool

Returns true if self == other.

source

pub const fn ne(self, other: u32) -> bool

Returns true if self != other.

source

pub const fn lt(self, other: u32) -> bool

Returns true if self < other.

source

pub const fn le(self, other: u32) -> bool

Returns true if self <= other.

source

pub const fn gt(self, other: u32) -> bool

Returns true if self > other.

source

pub const fn ge(self, other: u32) -> bool

Returns true if self >= other.

source§

impl Compare<u64>

source

pub const fn clamp(self, min: u64, max: u64) -> u64

Compares and returns self clamped between min and max.

source

pub const fn max(self, other: u64) -> u64

Compares and returns the maximum between self and other.

source

pub const fn min(self, other: u64) -> u64

Compares and returns the minimum between self and other.

source

pub const fn eq(self, other: u64) -> bool

Returns true if self == other.

source

pub const fn ne(self, other: u64) -> bool

Returns true if self != other.

source

pub const fn lt(self, other: u64) -> bool

Returns true if self < other.

source

pub const fn le(self, other: u64) -> bool

Returns true if self <= other.

source

pub const fn gt(self, other: u64) -> bool

Returns true if self > other.

source

pub const fn ge(self, other: u64) -> bool

Returns true if self >= other.

source§

impl Compare<u128>

source

pub const fn clamp(self, min: u128, max: u128) -> u128

Compares and returns self clamped between min and max.

source

pub const fn max(self, other: u128) -> u128

Compares and returns the maximum between self and other.

source

pub const fn min(self, other: u128) -> u128

Compares and returns the minimum between self and other.

source

pub const fn eq(self, other: u128) -> bool

Returns true if self == other.

source

pub const fn ne(self, other: u128) -> bool

Returns true if self != other.

source

pub const fn lt(self, other: u128) -> bool

Returns true if self < other.

source

pub const fn le(self, other: u128) -> bool

Returns true if self <= other.

source

pub const fn gt(self, other: u128) -> bool

Returns true if self > other.

source

pub const fn ge(self, other: u128) -> bool

Returns true if self >= other.

source§

impl Compare<usize>

source

pub const fn clamp(self, min: usize, max: usize) -> usize

Compares and returns self clamped between min and max.

source

pub const fn max(self, other: usize) -> usize

Compares and returns the maximum between self and other.

source

pub const fn min(self, other: usize) -> usize

Compares and returns the minimum between self and other.

source

pub const fn eq(self, other: usize) -> bool

Returns true if self == other.

source

pub const fn ne(self, other: usize) -> bool

Returns true if self != other.

source

pub const fn lt(self, other: usize) -> bool

Returns true if self < other.

source

pub const fn le(self, other: usize) -> bool

Returns true if self <= other.

source

pub const fn gt(self, other: usize) -> bool

Returns true if self > other.

source

pub const fn ge(self, other: usize) -> bool

Returns true if self >= other.

source§

impl Compare<i8>

source

pub const fn clamp(self, min: i8, max: i8) -> i8

Compares and returns self clamped between min and max.

source

pub const fn max(self, other: i8) -> i8

Compares and returns the maximum between self and other.

source

pub const fn min(self, other: i8) -> i8

Compares and returns the minimum between self and other.

source

pub const fn eq(self, other: i8) -> bool

Returns true if self == other.

source

pub const fn ne(self, other: i8) -> bool

Returns true if self != other.

source

pub const fn lt(self, other: i8) -> bool

Returns true if self < other.

source

pub const fn le(self, other: i8) -> bool

Returns true if self <= other.

source

pub const fn gt(self, other: i8) -> bool

Returns true if self > other.

source

pub const fn ge(self, other: i8) -> bool

Returns true if self >= other.

source§

impl Compare<i16>

source

pub const fn clamp(self, min: i16, max: i16) -> i16

Compares and returns self clamped between min and max.

source

pub const fn max(self, other: i16) -> i16

Compares and returns the maximum between self and other.

source

pub const fn min(self, other: i16) -> i16

Compares and returns the minimum between self and other.

source

pub const fn eq(self, other: i16) -> bool

Returns true if self == other.

source

pub const fn ne(self, other: i16) -> bool

Returns true if self != other.

source

pub const fn lt(self, other: i16) -> bool

Returns true if self < other.

source

pub const fn le(self, other: i16) -> bool

Returns true if self <= other.

source

pub const fn gt(self, other: i16) -> bool

Returns true if self > other.

source

pub const fn ge(self, other: i16) -> bool

Returns true if self >= other.

source§

impl Compare<i32>

source

pub const fn clamp(self, min: i32, max: i32) -> i32

Compares and returns self clamped between min and max.

source

pub const fn max(self, other: i32) -> i32

Compares and returns the maximum between self and other.

source

pub const fn min(self, other: i32) -> i32

Compares and returns the minimum between self and other.

source

pub const fn eq(self, other: i32) -> bool

Returns true if self == other.

source

pub const fn ne(self, other: i32) -> bool

Returns true if self != other.

source

pub const fn lt(self, other: i32) -> bool

Returns true if self < other.

source

pub const fn le(self, other: i32) -> bool

Returns true if self <= other.

source

pub const fn gt(self, other: i32) -> bool

Returns true if self > other.

source

pub const fn ge(self, other: i32) -> bool

Returns true if self >= other.

source§

impl Compare<i64>

source

pub const fn clamp(self, min: i64, max: i64) -> i64

Compares and returns self clamped between min and max.

source

pub const fn max(self, other: i64) -> i64

Compares and returns the maximum between self and other.

source

pub const fn min(self, other: i64) -> i64

Compares and returns the minimum between self and other.

source

pub const fn eq(self, other: i64) -> bool

Returns true if self == other.

source

pub const fn ne(self, other: i64) -> bool

Returns true if self != other.

source

pub const fn lt(self, other: i64) -> bool

Returns true if self < other.

source

pub const fn le(self, other: i64) -> bool

Returns true if self <= other.

source

pub const fn gt(self, other: i64) -> bool

Returns true if self > other.

source

pub const fn ge(self, other: i64) -> bool

Returns true if self >= other.

source§

impl Compare<i128>

source

pub const fn clamp(self, min: i128, max: i128) -> i128

Compares and returns self clamped between min and max.

source

pub const fn max(self, other: i128) -> i128

Compares and returns the maximum between self and other.

source

pub const fn min(self, other: i128) -> i128

Compares and returns the minimum between self and other.

source

pub const fn eq(self, other: i128) -> bool

Returns true if self == other.

source

pub const fn ne(self, other: i128) -> bool

Returns true if self != other.

source

pub const fn lt(self, other: i128) -> bool

Returns true if self < other.

source

pub const fn le(self, other: i128) -> bool

Returns true if self <= other.

source

pub const fn gt(self, other: i128) -> bool

Returns true if self > other.

source

pub const fn ge(self, other: i128) -> bool

Returns true if self >= other.

source§

impl Compare<isize>

source

pub const fn clamp(self, min: isize, max: isize) -> isize

Compares and returns self clamped between min and max.

source

pub const fn max(self, other: isize) -> isize

Compares and returns the maximum between self and other.

source

pub const fn min(self, other: isize) -> isize

Compares and returns the minimum between self and other.

source

pub const fn eq(self, other: isize) -> bool

Returns true if self == other.

source

pub const fn ne(self, other: isize) -> bool

Returns true if self != other.

source

pub const fn lt(self, other: isize) -> bool

Returns true if self < other.

source

pub const fn le(self, other: isize) -> bool

Returns true if self <= other.

source

pub const fn gt(self, other: isize) -> bool

Returns true if self > other.

source

pub const fn ge(self, other: isize) -> bool

Returns true if self >= other.

source§

impl Compare<f32>

source

pub const fn total_cmp(self, other: f32) -> Ordering

A (const) port of f32::total_cmp.

§Features

This function will only be const with the unsafe_const feature enabled.

source

pub const fn clamp(self, min: f32, max: f32) -> f32

Compares and returns a clamped total ordered self between min and max.

§Features

This function will only be const with the unsafe_const feature enabled.

§Examples
assert_eq![2.0, Compare(5.0f32).clamp(-1.0, 2.0)];
assert_eq![-1.0, Compare(-5.0f32).clamp(-1.0, 2.0)];
source

pub const fn max(self, other: f32) -> f32

Compares and returns the total ordered maximum between self and other.

§Features

This function will only be const with the unsafe_const feature enabled.

§Examples
assert_eq![2.0, Compare(2.0f32).max(-1.0)];
assert_eq![2.0, Compare(1.0f32).max(2.0)];
assert_eq![0.0, Compare(-0.0f32).max(0.0)];
assert_eq![f32::INFINITY, Compare(f32::INFINITY).max(f32::NEG_INFINITY)];
source

pub const fn min(self, other: f32) -> f32

Compares and returns the total ordered minimum between self and other.

§Features

This function will only be const with the unsafe_const feature enabled.

§Examples
assert_eq![-1.0, Compare(2.0f32).min(-1.0)];
assert_eq![1.0, Compare(1.0f32).min(2.0)];
assert_eq![-0.0, Compare(-0.0f32).min(0.0)];
assert_eq![f32::NEG_INFINITY, Compare(f32::INFINITY).min(f32::NEG_INFINITY)];
source

pub const fn eq(self, other: f32) -> bool

Returns true if self == other using total order.

source

pub const fn ne(self, other: f32) -> bool

Returns true if self != other using total order.

source

pub const fn lt(self, other: f32) -> bool

Returns true if self < other using total order.

source

pub const fn le(self, other: f32) -> bool

Returns true if self <= other using total order.

source

pub const fn gt(self, other: f32) -> bool

Returns true if self > other using total order.

source

pub const fn ge(self, other: f32) -> bool

Returns true if self >= other using total order.

source

pub const fn is_positive(self) -> bool

Returns true if self is sign positive.

source

pub const fn is_negative(self) -> bool

Returns true if self is sign negative.

source

pub const fn is_infinite(self) -> bool

Returns true if self is infinite (either negative or positive).

source

pub const fn is_finite(self) -> bool

Returns true if self is nether infinite nor NaN.

source

pub const fn is_nan(self) -> bool

Returns true if self is NaN.

source

pub const fn is_subnormal(self) -> bool

Returns true if self is subnormal.

source

pub const fn is_normal(self) -> bool

Returns true if self is neither zero, infinite, subnormal, or NaN.

source§

impl Compare<f64>

source

pub const fn total_cmp(self, other: f64) -> Ordering

A (const) port of f64::total_cmp.

§Features

This function will only be const with the unsafe_const feature enabled.

source

pub const fn clamp(self, min: f64, max: f64) -> f64

Compares and returns a clamped total ordered self between min and max.

§Features

This function will only be const with the unsafe_const feature enabled.

§Examples
assert_eq![2.0, Compare(5.0f64).clamp(-1.0, 2.0)];
assert_eq![-1.0, Compare(-5.0f64).clamp(-1.0, 2.0)];
source

pub const fn max(self, other: f64) -> f64

Compares and returns the total ordered maximum between self and other.

§Features

This function will only be const with the unsafe_const feature enabled.

§Examples
assert_eq![2.0, Compare(2.0f64).max(-1.0)];
assert_eq![2.0, Compare(1.0f64).max(2.0)];
assert_eq![0.0, Compare(-0.0f64).max(0.0)];
assert_eq![f64::INFINITY, Compare(f64::INFINITY).max(f64::NEG_INFINITY)];
source

pub const fn min(self, other: f64) -> f64

Compares and returns the total ordered minimum between self and other.

§Features

This function will only be const with the unsafe_const feature enabled.

§Examples
assert_eq![-1.0, Compare(2.0f64).min(-1.0)];
assert_eq![1.0, Compare(1.0f64).min(2.0)];
assert_eq![-0.0, Compare(-0.0f64).min(0.0)];
assert_eq![f64::NEG_INFINITY, Compare(f64::INFINITY).min(f64::NEG_INFINITY)];
source

pub const fn eq(self, other: f64) -> bool

Returns true if self == other using total order.

source

pub const fn ne(self, other: f64) -> bool

Returns true if self != other using total order.

source

pub const fn lt(self, other: f64) -> bool

Returns true if self < other using total order.

source

pub const fn le(self, other: f64) -> bool

Returns true if self <= other using total order.

source

pub const fn gt(self, other: f64) -> bool

Returns true if self > other using total order.

source

pub const fn ge(self, other: f64) -> bool

Returns true if self >= other using total order.

source

pub const fn is_positive(self) -> bool

Returns true if self is sign positive.

source

pub const fn is_negative(self) -> bool

Returns true if self is sign negative.

source

pub const fn is_infinite(self) -> bool

Returns true if self is infinite (either negative or positive).

source

pub const fn is_finite(self) -> bool

Returns true if self is nether infinite nor NaN.

source

pub const fn is_nan(self) -> bool

Returns true if self is NaN.

source

pub const fn is_subnormal(self) -> bool

Returns true if self is subnormal.

source

pub const fn is_normal(self) -> bool

Returns true if self is neither zero, infinite, subnormal, or NaN.

Trait Implementations§

source§

impl<T: Clone> Clone for Compare<T>

source§

fn clone(&self) -> Self

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<T: Debug> Debug for Compare<T>

source§

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

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

impl<T: Display> Display for Compare<T>

source§

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

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

impl<T: Ord> Ord for Compare<T>

source§

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

This method returns an Ordering between self and other. Read more
1.21.0 · source§

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

Compares and returns the maximum of two values. Read more
1.21.0 · source§

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

Compares and returns the minimum of two values. Read more
1.50.0 · source§

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

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

impl<T: PartialEq> PartialEq for Compare<T>

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<T: PartialOrd> PartialOrd for Compare<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 · source§

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

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

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

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

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

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

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

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl<T: Copy> Copy for Compare<T>

source§

impl<T: Eq> Eq for Compare<T>

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

§

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

Blanket Implementations§

source§

impl<T> Also for T

source§

fn also_mut<F: FnOnce(&mut Self)>(self, f: F) -> Self

Applies a function which takes the parameter by exclusive reference, and then returns the (possibly) modified owned value. Read more
source§

fn also_ref<F: FnOnce(&Self)>(self, f: F) -> Self

Applies a function which takes the parameter by shared reference, and then returns the (possibly) modified owned value. Read more
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, Res> Apply<Res> for T
where T: ?Sized,

source§

fn apply<F: FnOnce(Self) -> Res>(self, f: F) -> Res
where Self: Sized,

Apply a function which takes the parameter by value.
source§

fn apply_ref<F: FnOnce(&Self) -> Res>(&self, f: F) -> Res

Apply a function which takes the parameter by shared reference.
source§

fn apply_mut<F: FnOnce(&mut Self) -> Res>(&mut self, f: F) -> Res

Apply a function which takes the parameter by exclusive reference.
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> ByteSize for T

source§

const BYTE_ALIGN: usize = _

The alignment of this type in bytes.
source§

const BYTE_SIZE: usize = _

The size of this type in bytes.
source§

const PTR_SIZE: usize = 8usize

The size of a pointer in bytes, for the current platform.
source§

fn byte_align(&self) -> usize

Returns the alignment of this type in bytes.
source§

fn byte_size(&self) -> usize

Returns the size of this type in bytes. Read more
source§

fn ptr_size_ratio(&self) -> [usize; 2]

Returns the size ratio between PTR_SIZE and BYTE_SIZE. 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<T> ExtAny for T
where T: Any,

source§

fn type_of(&self) -> TypeId

Returns the TypeId of self. Read more
source§

fn type_name(&self) -> &'static str

Returns the type name of self. Read more
source§

fn type_is<T: 'static>(&self) -> bool

Returns true if Self is of type T. Read more
source§

fn as_any_ref(&self) -> &dyn Any
where Self: Sized,

Upcasts &self as &dyn Any. Read more
source§

fn as_any_mut(&mut self) -> &mut dyn Any
where Self: Sized,

Upcasts &mut self as &mut dyn Any. Read more
source§

fn as_any_box(self: Box<Self>) -> Box<dyn Any>
where Self: Sized,

Upcasts Box<self> as Box<dyn Any>. 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> Mem for T
where T: ?Sized,

source§

const NEEDS_DROP: bool = _

Whether dropping values of this type matters.
source§

fn mem_needs_drop(&self) -> bool

Returns true if dropping values of this type matters.
source§

fn mem_drop(self)
where Self: Sized,

Drops self by running its destructor.
source§

fn mem_forget(self)
where Self: Sized,

Forgets about self without running its destructor.
source§

fn mem_replace(&mut self, other: Self) -> Self
where Self: Sized,

Replaces self with other, returning the previous value of self.
source§

fn mem_take(&mut self) -> Self
where Self: Default,

Replaces self with its default value, returning the previous value of self.
source§

fn mem_swap(&mut self, other: &mut Self)
where Self: Sized,

Swaps the value of self and other without deinitializing either one.
source§

fn mem_as_bytes(&self) -> &[u8]
where Self: Sync + Unpin,

Available on crate feature unsafe_slice only.
View a Sync + Unpin self as &[u8]. Read more
source§

fn mem_as_bytes_mut(&mut self) -> &mut [u8]
where Self: Sync + Unpin,

Available on crate feature unsafe_slice only.
View a Sync + Unpin self as &mut [u8].
source§

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

§

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§

default 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>,

§

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

§

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.