Enum infinitable::Infinitable

source ·
pub enum Infinitable<T> {
    Finite(T),
    Infinity,
    NegativeInfinity,
}
Expand description

An “infinitable” value, one that can be either finite or infinite

§Versioning

Available since 1.0.0. Variants are re-exported since 1.3.0.

Variants§

§

Finite(T)

A finite value T

§

Infinity

Positive infinity, which compares greater than all other values

§

NegativeInfinity

Negative infinity, which compares less than all other values

Implementations§

source§

impl<T> Infinitable<T>

source

pub fn is_finite(&self) -> bool

Returns true if the value is Finite.

§Examples
use infinitable::*;

let finite = Finite(5);
assert!(finite.is_finite());
let infinite: Infinitable<i32> = Infinity;
assert!(!infinite.is_finite());
§Versioning

Available since 1.0.0.

source

pub fn finite(self) -> Option<T>

Converts from an Infinitable<T> to an Option<T>.

Converts self into an Option<T> possibly containing a finite value, consuming self.

§Examples
use infinitable::*;

let finite = Finite(5);
assert_eq!(Some(5), finite.finite());
let infinite: Infinitable<i32> = Infinity;
assert_eq!(None, infinite.finite());
§Versioning

Available since 1.1.0.

source

pub fn finite_or_infinity(option: Option<T>) -> Infinitable<T>

Converts from Option<T> to Finite or Infinity.

Some(T) is converted to Finite(T), and None is converted to Infinity.

§Examples
use infinitable::*;

let finite = Finite(5);
assert_eq!(finite, Infinitable::finite_or_infinity(Some(5)));
let infinite: Infinitable<i32> = Infinity;
assert_eq!(infinite, Infinitable::finite_or_infinity(None));
§Versioning

Available since 1.3.0.

source

pub fn finite_or_negative_infinity(option: Option<T>) -> Infinitable<T>

Converts from Option<T> to Finite or NegativeInfinity.

Some(T) is converted to Finite(T), and None is converted to NegativeInfinity.

§Examples
use infinitable::*;

let finite = Finite(5);
assert_eq!(finite, Infinitable::finite_or_negative_infinity(Some(5)));
let infinite: Infinitable<i32> = NegativeInfinity;
assert_eq!(infinite, Infinitable::finite_or_negative_infinity(None));
§Versioning

Available since 1.3.0.

source

pub fn convert_into<U>(self) -> Infinitable<U>
where T: Into<U>,

Converts the value into a different type, based on the conversion of the underlying type T.

Infinite values are preserved as is, while finite values are converted using into on the underlying value.

§Examples
use infinitable::*;
let finite = Finite(5);
let finite64: Infinitable<i64> = finite.convert_into();
assert_eq!(Finite(5i64), finite64);
let infinite: Infinitable<i32> = Infinity;
let infinite64: Infinitable<i64> = infinite.convert_into();
assert_eq!(Infinity, infinite64);
§Versioning

Available since 1.6.0.

source

pub fn try_convert_into<U>(self) -> Result<Infinitable<U>, T::Error>
where T: TryInto<U>,

Converts the value into a different type, based on the conversion of the underlying type T.

Infinite values are preserved as is, while finite values are converted using try_into on the underlying value. Conversion of infinite values always succeeds, while conversion of finite values may fail.

§Examples
use infinitable::*;
use std::num::TryFromIntError;
let finite = Finite(1000);
let finite8: Result<Infinitable<i8>, TryFromIntError>
    = finite.try_convert_into();
assert!(finite8.is_err());
let infinite: Infinitable<i32> = Infinity;
let infinite8: Result<Infinitable<i8>, TryFromIntError>
    = infinite.try_convert_into();
assert_eq!(Ok(Infinity), infinite8);
§Versioning

Available since 1.6.0.

Trait Implementations§

source§

impl<T> Add for Infinitable<T>
where T: Add,

source§

fn add(self, rhs: Infinitable<T>) -> Infinitable<T::Output>

Adds two values.

The addition operation follows these rules:

selfrhsresult
FiniteFiniteFinite (add values)
FiniteInfinityInfinity
FiniteNegativeInfinityNegativeInfinity
InfinityFiniteInfinity
InfinityInfinityInfinity
InfinityNegativeInfinityUndefined (panic)
NegativeInfinityFiniteNegativeInfinity
NegativeInfinityInfinityUndefined (panic)
NegativeInfinityNegativeInfinityNegativeInfinity
§Examples
use infinitable::*;

assert_eq!(Finite(5), Finite(2) + Finite(3));
assert_eq!(Infinity, Finite(1) + Infinity);
assert_eq!(NegativeInfinity, NegativeInfinity + Finite(1));

The addition operation panics with Infinity and NegativeInfinity:

use infinitable::*;

let infinity: Infinitable<i32> = Infinity;
let negative_infinity: Infinitable<i32> = NegativeInfinity;
let _ = infinity + negative_infinity;
§Panics

Panics if the operands consist of Infinity and NegativeInfinity.

§Versioning

Available since 1.5.0.

§

type Output = Infinitable<<T as Add>::Output>

The resulting type after applying the + operator.
source§

impl<T: Clone> Clone for Infinitable<T>

source§

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

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 Infinitable<T>

source§

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

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

impl<T> Display for Infinitable<T>
where T: Display,

source§

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

Formats the value, when the underlying type T supports formatting.

Infinity is formatted to "inf", NegativeInfinity is formatted to "-inf", and Finite is formatted based on the underlying value.

§Examples
use infinitable::*;

let finite = Finite(5);
assert_eq!("5", format!("{}", finite));
let infinity: Infinitable<i32> = Infinity;
assert_eq!("inf", format!("{}", infinity));
let negative_infinity: Infinitable<i32> = NegativeInfinity;
assert_eq!("-inf", format!("{}", negative_infinity));
§Versioning

Available since 1.2.0.

source§

impl<T> Div for Infinitable<T>
where T: Div + Zero + PartialOrd, <T as Div>::Output: Zero,

source§

fn div(self, rhs: Infinitable<T>) -> Infinitable<<T as Div>::Output>

Divides two values.

The division operation follows these rules:

selfrhsresult
Finite (> 0)Finite (~ 0)Infinity
Finite (~ 0)Finite (~ 0)Undefined (panic)
Finite (< 0)Finite (~ 0)NegativeInfinity
FiniteFinite (<> 0)Finite (divide values)
FiniteInfinityZero
FiniteNegativeInfinityZero
InfinityFinite (> 0)Infinity
InfinityFinite (~ 0)Infinity
InfinityFinite (< 0)NegativeInfinity
InfinityInfinityUndefined (panic)
InfinityNegativeInfinityUndefined (panic)
NegativeInfinityFinite (> 0)NegativeInfinity
NegativeInfinityFinite (~ 0)NegativeInfinity
NegativeInfinityFinite (< 0)Infinity
NegativeInfinityInfinityUndefined (panic)
NegativeInfinityNegativeInfinityUndefined (panic)

(In the table, “~ 0” refers to a value that is either equal to or unordered with zero, and “<> 0” refers to a value that is either greater than or less than zero.)

§Examples
use infinitable::*;

assert_eq!(Finite(3), Finite(6) / Finite(2));
assert_eq!(Infinity, Infinity / Finite(1));
assert_eq!(Infinity, NegativeInfinity / Finite(-2));
assert_eq!(NegativeInfinity, NegativeInfinity / Finite(1));
assert_eq!(NegativeInfinity, Infinity / Finite(-2));
assert_eq!(Finite(0), Finite(1) / Infinity);
assert_eq!(Finite(0), Finite(1) / NegativeInfinity);

The division operation panics when an infinite value is divided by another infinite value, or when zero is divided by itself:

use infinitable::*;

let infinity: Infinitable<i32> = Infinity;
let _ = infinity / infinity;
use infinitable::*;

let _ = Finite(0) / Finite(0);
§Panics

Panics if both operands are either Infinity or NegativeInfinity, or both operands are Finite with an underlying value equal to or unordered with zero.

§Versioning

Available since 1.5.0.

§

type Output = Infinitable<<T as Div>::Output>

The resulting type after applying the / operator.
source§

impl<T> From<T> for Infinitable<T>

source§

fn from(value: T) -> Infinitable<T>

Converts from a value T to Finite containing the underlying value.

Note that there is no special handling for pre-existing infinite values. Consider using from_f32 or from_f64 for floating-point numbers.

§Examples
use infinitable::*;

let finite = Infinitable::from(5);
assert_eq!(Finite(5), finite);

// Warning: There is no special handling for pre-existing infinite values
let fp_infinity = Infinitable::from(f32::INFINITY);
assert_eq!(Finite(f32::INFINITY), fp_infinity);
assert_ne!(Infinity, fp_infinity);
§Versioning

Available since 1.2.0.

source§

impl<T: Hash> Hash for Infinitable<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> Mul for Infinitable<T>
where T: Mul + Zero + PartialOrd,

source§

fn mul(self, rhs: Infinitable<T>) -> Infinitable<<T as Mul>::Output>

Multiplies two values.

The multiplication operation follows these rules:

selfrhsresult
FiniteFiniteFinite (multiply values)
Finite (> 0)InfinityInfinity
Finite (~ 0)InfinityUndefined (panic)
Finite (< 0)InfinityNegativeInfinity
Finite (> 0)NegativeInfinityNegativeInfinity
Finite (~ 0)NegativeInfinityUndefined (panic)
Finite (< 0)NegativeInfinityInfinity
InfinityFinite (> 0)Infinity
InfinityFinite (~ 0)Undefined (panic)
InfinityFinite (< 0)NegativeInfinity
InfinityInfinityInfinity
InfinityNegativeInfinityNegativeInfinity
NegativeInfinityFinite (> 0)NegativeInfinity
NegativeInfinityFinite (~ 0)Undefined (panic)
NegativeInfinityFinite (< 0)Infinity
NegativeInfinityInfinityNegativeInfinity
NegativeInfinityNegativeInfinityInfinity

(In the table, “~ 0” refers to a value that is either equal to or unordered with zero.)

§Examples
use infinitable::*;

assert_eq!(Finite(6), Finite(2) * Finite(3));
assert_eq!(Infinity, Infinity * Finite(2));
assert_eq!(Infinity, Finite(-1) * NegativeInfinity);
assert_eq!(NegativeInfinity, NegativeInfinity * Finite(2));
assert_eq!(NegativeInfinity, Finite(-1) * Infinity);

The multiplication operation panics when an infinite value is multiplied with zero:

use infinitable::*;

let infinity: Infinitable<i32> = Infinity;
let _ = infinity * Finite(0);
§Panics

Panics if one of the operands is Infinity or NegativeInfinity and the other is a Finite value with an underlying value equal to or unordered with zero.

§Versioning

Available since 1.5.0.

§

type Output = Infinitable<<T as Mul>::Output>

The resulting type after applying the * operator.
source§

impl<T> Neg for Infinitable<T>
where T: Neg,

source§

fn neg(self) -> Infinitable<T::Output>

Negates the value, when the underlying type T supports negation.

Infinity is negated to NegativeInfinity (and vice versa), and Finite is negated based on the underlying value.

§Examples
use infinitable::*;

let finite = Finite(5);
assert_eq!(Finite(-5), -finite);
let infinity: Infinitable<i32> = Infinity;
assert_eq!(NegativeInfinity, -infinity);
let negative_infinity: Infinitable<i32> = NegativeInfinity;
assert_eq!(Infinity, -negative_infinity);
§Versioning

Available since 1.3.0.

§

type Output = Infinitable<<T as Neg>::Output>

The resulting type after applying the - operator.
source§

impl<T> Ord for Infinitable<T>
where T: Ord,

Total order, when the underlying type T implements a total order.

NegativeInfinity compares less than all other values, and Infinity compares greater than all other values.

§Examples

use infinitable::*;
use std::cmp::Ordering;

let finite = Finite(5);
let infinity = Infinity;
let negative_infinity = NegativeInfinity;

assert_eq!(Ordering::Less, finite.cmp(&infinity));
assert_eq!(Ordering::Greater, finite.cmp(&negative_infinity));

§Versioning

Available since 1.0.0.

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 Infinitable<T>

source§

fn eq(&self, other: &Infinitable<T>) -> 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 for Infinitable<T>
where T: PartialOrd,

Partial order, when the underlying type T implements a partial order.

NegativeInfinity compares less than all other values, and Infinity compares greater than all other values.

§Examples

use infinitable::*;
use std::cmp::Ordering;

let finite = Finite(5);
let infinity = Infinity;
let negative_infinity = NegativeInfinity;

assert_eq!(Some(Ordering::Less), finite.partial_cmp(&infinity));
assert_eq!(Some(Ordering::Greater), finite.partial_cmp(&negative_infinity));

§Versioning

Available since 1.0.0.

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> Sub for Infinitable<T>
where T: Sub,

source§

fn sub(self, rhs: Infinitable<T>) -> Infinitable<T::Output>

Subtracts two values.

The subtraction operation follows these rules:

selfrhsresult
FiniteFiniteFinite (subtract values)
FiniteInfinityNegativeInfinity
FiniteNegativeInfinityInfinity
InfinityFiniteInfinity
InfinityInfinityUndefined (panic)
InfinityNegativeInfinityInfinity
NegativeInfinityFiniteNegativeInfinity
NegativeInfinityInfinityNegativeInfinity
NegativeInfinityNegativeInfinityUndefined (panic)
§Examples
use infinitable::*;

assert_eq!(Finite(3), Finite(5) - Finite(2));
assert_eq!(Infinity, Infinity - Finite(1));
assert_eq!(Infinity, Finite(1) - NegativeInfinity);
assert_eq!(NegativeInfinity, NegativeInfinity - Finite(1));
assert_eq!(NegativeInfinity, Finite(1) - Infinity);

The subraction operation panics when an infinite value is subtracted from itself:

use infinitable::*;

let infinity: Infinitable<i32> = Infinity;
let _ = infinity - infinity;
§Panics

Panics if the operands are both Infinity or both NegativeInfinity.

§Versioning

Available since 1.5.0.

§

type Output = Infinitable<<T as Sub>::Output>

The resulting type after applying the - operator.
source§

impl<T: Copy> Copy for Infinitable<T>

source§

impl<T: Eq> Eq for Infinitable<T>

source§

impl<T> StructuralPartialEq for Infinitable<T>

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

§

impl<T> UnwindSafe for Infinitable<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> 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> From<!> for T

source§

fn from(t: !) -> T

Converts to this type from the input type.
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, 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.