Skip to main content

Number

Enum Number 

pub enum Number {
    Integer(i64),
    Float(f64),
}
Expand description

Represents a YAML number.

Variants§

§

Integer(i64)

A signed integer.

§

Float(f64)

A floating-point number.

Implementations§

§

impl Number

pub fn as_i64(&self) -> Option<i64>

Returns the number as an i64 if it is an integer.

Floats return None even when their value happens to be a whole number; the type tag is part of the test.

§Examples
use noyalib::Number;
assert_eq!(Number::Integer(42).as_i64(), Some(42));
assert_eq!(Number::Float(1.0).as_i64(), None);

pub fn as_u64(&self) -> Option<u64>

Returns the number as a u64 if it is a non-negative integer.

Negative integers and floats return None.

§Examples
use noyalib::Number;
assert_eq!(Number::Integer(42).as_u64(), Some(42));
assert_eq!(Number::Integer(-1).as_u64(), None);
assert_eq!(Number::Float(1.0).as_u64(), None);

pub fn as_f64(&self) -> f64

Returns the number as an f64.

Always succeeds — integers are widened to f64 (with the usual i64 → f64 precision loss for magnitudes above 2^53), floats pass through unchanged.

§Examples
use noyalib::Number;
assert_eq!(Number::Integer(42).as_f64(), 42.0);
assert_eq!(Number::Float(0.5).as_f64(), 0.5);

pub fn is_integer(&self) -> bool

Returns true if the number is an integer.

§Examples
use noyalib::Number;
assert!(Number::Integer(42).is_integer());
assert!(!Number::Float(1.0).is_integer());

pub fn is_float(&self) -> bool

Returns true if the number is a float.

§Examples
use noyalib::Number;
assert!(Number::Float(1.0).is_float());
assert!(!Number::Integer(42).is_float());

pub fn is_i64(&self) -> bool

Returns true if the number can be represented as an i64.

True for all integer values, false for floats.

§Examples
use noyalib::Number;
assert!(Number::Integer(42).is_i64());
assert!(!Number::Float(42.0).is_i64());

pub fn is_u64(&self) -> bool

Returns true if the number can be represented as a u64.

True for non-negative integers, false otherwise.

§Examples
use noyalib::Number;
assert!(Number::Integer(42).is_u64());
assert!(!Number::Integer(-1).is_u64());
assert!(!Number::Float(1.0).is_u64());

pub fn is_f64(&self) -> bool

Returns true if the number can be represented as an f64.

Always true — both integers and floats convert to f64 (with the usual precision caveats for very large integers).

§Examples
use noyalib::Number;
assert!(Number::Integer(42).is_f64());
assert!(Number::Float(1.0).is_f64());

pub fn is_nan(&self) -> bool

Returns true if the number is NaN (Not a Number).

Integers are never NaN — only floats with the IEEE 754 NaN bit pattern.

§Examples
use noyalib::Number;
assert!(Number::Float(f64::NAN).is_nan());
assert!(!Number::Float(0.0).is_nan());
assert!(!Number::Integer(0).is_nan());

pub fn is_infinite(&self) -> bool

Returns true if the number is positive or negative infinity.

Integers are always finite — only Number::Float(±inf) returns true.

§Examples
use noyalib::Number;
assert!(Number::Float(f64::INFINITY).is_infinite());
assert!(Number::Float(f64::NEG_INFINITY).is_infinite());
assert!(!Number::Integer(i64::MAX).is_infinite());

pub fn is_finite(&self) -> bool

Returns true if the number is neither infinite nor NaN.

Integers are always finite; floats are finite when neither ±∞ nor NaN.

§Examples
use noyalib::Number;
assert!(Number::Integer(0).is_finite());
assert!(Number::Float(0.5).is_finite());
assert!(!Number::Float(f64::NAN).is_finite());
assert!(!Number::Float(f64::INFINITY).is_finite());

Trait Implementations§

§

impl Clone for Number

§

fn clone(&self) -> Number

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
§

impl Debug for Number

§

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

Formats the value using the given formatter. Read more
§

impl Display for Number

§

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

Formats the value using the given formatter. Read more
§

impl From<Number> for Value

§

fn from(v: Number) -> Value

Converts to this type from the input type.
§

impl From<f32> for Number

§

fn from(v: f32) -> Number

Converts to this type from the input type.
§

impl From<f64> for Number

§

fn from(v: f64) -> Number

Converts to this type from the input type.
§

impl From<i16> for Number

§

fn from(v: i16) -> Number

Converts to this type from the input type.
§

impl From<i32> for Number

§

fn from(v: i32) -> Number

Converts to this type from the input type.
§

impl From<i64> for Number

§

fn from(v: i64) -> Number

Converts to this type from the input type.
§

impl From<i8> for Number

§

fn from(v: i8) -> Number

Converts to this type from the input type.
§

impl From<isize> for Number

§

fn from(v: isize) -> Number

Converts to this type from the input type.
§

impl From<u16> for Number

§

fn from(v: u16) -> Number

Converts to this type from the input type.
§

impl From<u32> for Number

§

fn from(v: u32) -> Number

Converts to this type from the input type.
§

impl From<u64> for Number

§

fn from(v: u64) -> Number

Converts to this type from the input type.
§

impl From<u8> for Number

§

fn from(v: u8) -> Number

Converts to this type from the input type.
§

impl From<usize> for Number

§

fn from(v: usize) -> Number

Converts to this type from the input type.
§

impl FromStr for Number

§

type Err = ParseNumberError

The associated error which can be returned from parsing.
§

fn from_str(s: &str) -> Result<Number, <Number as FromStr>::Err>

Parses a string s to return a value of this type. Read more
§

impl Hash for Number

§

fn hash<H>(&self, state: &mut H)
where H: Hasher,

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
§

impl Ord for Number

§

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

Restrict a value to a certain interval. Read more
§

impl PartialEq for Number

§

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

impl PartialOrd for Number

§

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

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

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

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

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

impl Copy for Number

§

impl Eq for Number

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
§

impl<Q, K> Comparable<K> for Q
where Q: Ord + ?Sized, K: Borrow<Q> + ?Sized,

§

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

Compare self to key and return their ordering.
§

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

§

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

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

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

§

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