Skip to main content

NumberRef

Struct NumberRef 

Source
pub struct NumberRef<'a> { /* private fields */ }
Expand description

Borrows or copies a numeric value without depending on a runtime value enum.

Values are constructed through From implementations. The internal representation is intentionally private, so callers cannot create marker or otherwise non-numeric states.

§Type Parameters

  • 'a - Lifetime of a borrowed arbitrary-precision numeric value.

§Examples

use qubit_datatype::NumberRef;

let number = NumberRef::from(42_i64);
assert!(number.is_integer());
use std::cmp::Ordering;
use qubit_datatype::NumberRef;
use qubit_datatype::NumericComparisonPolicy;

let integer = NumberRef::from((1_u64 << 53) + 1);
let float = NumberRef::from((1_u64 << 53) as f64);
assert_eq!(integer.compare(float, NumericComparisonPolicy::Exact), Some(Ordering::Greater));
assert_eq!(integer.compare(float, NumericComparisonPolicy::Approximate), Some(Ordering::Equal));
assert_eq!(integer.compare(NumberRef::from(f64::NAN), NumericComparisonPolicy::Exact), None);
ⓘ
use qubit_datatype::NumberRef;

let _ = NumberRef::Int8(1);

Implementations§

Source§

impl<'a> NumberRef<'a>

Source

pub fn is_integer(self) -> bool

Reports whether this value is an integer representation.

§Returns

true for fixed-width integer values.

Source

pub fn is_float(self) -> bool

Reports whether this value is a primitive floating-point value.

§Returns

true for f32 and f64 values.

Source

pub fn is_big_integer(self) -> bool

Reports whether this value is an arbitrary-precision integer.

§Returns

true for BigInt values when the big-integer feature is enabled; otherwise, false.

Source

pub fn is_big_decimal(self) -> bool

Reports whether this value is an arbitrary-precision decimal.

§Returns

true for BigDecimal values when the big-decimal feature is enabled; otherwise, false.

Source

pub fn is_nan(self) -> bool

Reports whether this value is NaN.

§Returns

true only for a primitive NaN payload.

Source

pub fn is_infinite(self) -> bool

Reports whether this value is an infinite primitive float.

§Returns

true for positive or negative infinity; otherwise, false.

Source

pub fn is_finite(self) -> bool

Reports whether this value is finite.

§Returns

true for all integers, arbitrary-precision values, and finite primitive floats; otherwise, false.

Source

pub fn is_zero(self) -> bool

Reports whether this value is mathematically zero.

§Returns

true for integer zero and both positive and negative floating-point zero; otherwise, false.

Source

pub fn is_positive(self) -> bool

Reports whether this value is greater than zero.

§Returns

true for positive finite or infinite values; zero, negative values, and NaN return false.

Source

pub fn is_negative(self) -> bool

Reports whether this value is less than zero.

§Returns

true for negative finite or infinite values; zero, positive values, and NaN return false.

Source

pub fn compare( self, right: NumberRef<'_>, policy: NumericComparisonPolicy, ) -> Option<Ordering>

Compares this value with right under an explicit numeric policy.

Fixed-width comparisons do not require heap allocation. Some paths involving BigInt or BigDecimal may clone coefficients or construct exact rational or decimal values, with cost depending on the operand representations. Direct comparisons between some large-number representations avoid that work. Conversion limits and session budgets do not apply to this independent comparison; constrain untrusted number representations at the input boundary when their size must be bounded.

See the type-level examples for exact, approximate, and NaN comparisons. Approximate comparison is not transitive across mixed representations; do not use it for sorting or ordered keys.

§Parameters
  • right - Right numeric operand.
  • policy - Exact or approximate comparison policy.
§Returns

The numeric ordering, or None when either operand is NaN.

Trait Implementations§

Source§

impl<'a> Clone for NumberRef<'a>

Source§

fn clone(&self) -> Self

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<'a> Copy for NumberRef<'a>

Source§

impl<'a> Debug for NumberRef<'a>

Source§

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

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

impl<'a> From<&'a BigDecimal> for NumberRef<'a>

Source§

fn from(value: &'a BigDecimal) -> Self

Creates a numeric view borrowing an arbitrary-precision value.

§Parameters
  • value - Numeric value to borrow.
§Returns

A numeric view borrowing the source for its lifetime.

Source§

impl<'a> From<&'a BigInt> for NumberRef<'a>

Source§

fn from(value: &'a BigInt) -> Self

Creates a numeric view borrowing an arbitrary-precision value.

§Parameters
  • value - Numeric value to borrow.
§Returns

A numeric view borrowing the source for its lifetime.

Source§

impl From<f32> for NumberRef<'_>

Source§

fn from(value: f32) -> Self

Creates a numeric view by copying a fixed-width value.

§Parameters
  • value - Numeric value to copy.
§Returns

A numeric view owning the copied scalar representation.

Source§

impl From<f64> for NumberRef<'_>

Source§

fn from(value: f64) -> Self

Creates a numeric view by copying a fixed-width value.

§Parameters
  • value - Numeric value to copy.
§Returns

A numeric view owning the copied scalar representation.

Source§

impl From<i8> for NumberRef<'_>

Source§

fn from(value: i8) -> Self

Creates a numeric view by copying a fixed-width value.

§Parameters
  • value - Numeric value to copy.
§Returns

A numeric view owning the copied scalar representation.

Source§

impl From<i16> for NumberRef<'_>

Source§

fn from(value: i16) -> Self

Creates a numeric view by copying a fixed-width value.

§Parameters
  • value - Numeric value to copy.
§Returns

A numeric view owning the copied scalar representation.

Source§

impl From<i32> for NumberRef<'_>

Source§

fn from(value: i32) -> Self

Creates a numeric view by copying a fixed-width value.

§Parameters
  • value - Numeric value to copy.
§Returns

A numeric view owning the copied scalar representation.

Source§

impl From<i64> for NumberRef<'_>

Source§

fn from(value: i64) -> Self

Creates a numeric view by copying a fixed-width value.

§Parameters
  • value - Numeric value to copy.
§Returns

A numeric view owning the copied scalar representation.

Source§

impl From<i128> for NumberRef<'_>

Source§

fn from(value: i128) -> Self

Creates a numeric view by copying a fixed-width value.

§Parameters
  • value - Numeric value to copy.
§Returns

A numeric view owning the copied scalar representation.

Source§

impl From<u8> for NumberRef<'_>

Source§

fn from(value: u8) -> Self

Creates a numeric view by copying a fixed-width value.

§Parameters
  • value - Numeric value to copy.
§Returns

A numeric view owning the copied scalar representation.

Source§

impl From<u16> for NumberRef<'_>

Source§

fn from(value: u16) -> Self

Creates a numeric view by copying a fixed-width value.

§Parameters
  • value - Numeric value to copy.
§Returns

A numeric view owning the copied scalar representation.

Source§

impl From<u32> for NumberRef<'_>

Source§

fn from(value: u32) -> Self

Creates a numeric view by copying a fixed-width value.

§Parameters
  • value - Numeric value to copy.
§Returns

A numeric view owning the copied scalar representation.

Source§

impl From<u64> for NumberRef<'_>

Source§

fn from(value: u64) -> Self

Creates a numeric view by copying a fixed-width value.

§Parameters
  • value - Numeric value to copy.
§Returns

A numeric view owning the copied scalar representation.

Source§

impl From<u128> for NumberRef<'_>

Source§

fn from(value: u128) -> Self

Creates a numeric view by copying a fixed-width value.

§Parameters
  • value - Numeric value to copy.
§Returns

A numeric view owning the copied scalar representation.

Auto Trait Implementations§

§

impl<'a> Forget for NumberRef<'a>

§

impl<'a> Freeze for NumberRef<'a>

§

impl<'a> Move for NumberRef<'a>

§

impl<'a> RefUnwindSafe for NumberRef<'a>

§

impl<'a> Send for NumberRef<'a>

§

impl<'a> Sync for NumberRef<'a>

§

impl<'a> Unpin for NumberRef<'a>

§

impl<'a> UnsafeUnpin for NumberRef<'a>

§

impl<'a> UnwindSafe for NumberRef<'a>

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> 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 = !

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

fn try_from(value: U) -> Result<T, !>

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.