bigdecimal

Struct BigDecimalRef

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

Immutable big-decimal, referencing a borrowed buffer of digits

The non-digit information like scale and sign may be changed on these objects, which otherwise would require cloning the full digit buffer in the BigDecimal.

Built from full BigDecimal object using the to_ref() method. BigDecimal not implement AsRef, so we will reserve the method as_ref() for a later time.

May be transformed into full BigDecimal object using the to_owned() method. This clones the bigdecimal digits.

BigDecimalRef (or Into<BigDecimalRef>) should be preferred over using &BigDecimal for library functions that need an immutable reference to a bigdecimal, as it may be much more efficient.

NOTE: Using &BigDecimalRef is redundant, and not recommended.

§Examples

fn add_one<'a, N: Into<BigDecimalRef<'a>>>(n: N) -> BigDecimal {
    n.into() + 1
}

let n: BigDecimal = "123.456".parse().unwrap();

// call via "standard" reference (implements Into)
let m = add_one(&n);
assert_eq!(m, "124.456".parse().unwrap());

// call by negating the reference (fast: no-digit cloning involved)
let m = add_one(n.to_ref().neg());
assert_eq!(m, "-122.456".parse().unwrap());

Implementations§

Source§

impl BigDecimalRef<'_>

Source

pub fn to_owned(&self) -> BigDecimal

Clone digits to make this reference a full BigDecimal object

Source

pub fn to_owned_with_scale(&self, scale: i64) -> BigDecimal

Clone digits, returning BigDecimal with given scale


let n: BigDecimal = "123.45678".parse().unwrap();
let r = n.to_ref();
assert_eq!(r.to_owned_with_scale(5), n.clone());
assert_eq!(r.to_owned_with_scale(0), "123".parse().unwrap());
assert_eq!(r.to_owned_with_scale(-1), "12e1".parse().unwrap());

let x = r.to_owned_with_scale(8);
assert_eq!(&x, &n);
assert_eq!(x.fractional_digit_count(), 8);
Source

pub fn sign(&self) -> Sign

Sign of decimal

Source

pub fn fractional_digit_count(&self) -> i64

Return number of digits ‘right’ of the decimal point (including leading zeros)

Source

pub fn count_digits(&self) -> u64

Count total number of decimal digits

Source

pub fn abs(&self) -> Self

Take absolute value of the decimal (non-negative sign)

Source

pub fn round_with_context(&self, ctx: &Context) -> BigDecimal

Create BigDecimal from this reference, rounding to precision and with rounding-mode of the given context

Source

pub fn sqrt_with_context(&self, ctx: &Context) -> Option<BigDecimal>

Take square root of this number

Source

pub fn sqrt_abs_with_context(&self, ctx: &Context) -> BigDecimal

Take square root of absolute-value of the number

Source

pub fn sqrt_copysign_with_context(&self, ctx: &Context) -> BigDecimal

Take square root, copying sign of the initial decimal

Source

pub fn is_zero(&self) -> bool

Return if the referenced decimal is zero

Source

pub fn clone_into(&self, dest: &mut BigDecimal)

Clone this value into dest

Trait Implementations§

Source§

impl Add<&i128> for BigDecimalRef<'_>

Source§

type Output = BigDecimal

The resulting type after applying the + operator.
Source§

fn add(self, rhs: &i128) -> BigDecimal

Performs the + operation. Read more
Source§

impl Add<&i16> for BigDecimalRef<'_>

Source§

type Output = BigDecimal

The resulting type after applying the + operator.
Source§

fn add(self, rhs: &i16) -> BigDecimal

Performs the + operation. Read more
Source§

impl Add<&i32> for BigDecimalRef<'_>

Source§

type Output = BigDecimal

The resulting type after applying the + operator.
Source§

fn add(self, rhs: &i32) -> BigDecimal

Performs the + operation. Read more
Source§

impl Add<&i64> for BigDecimalRef<'_>

Source§

type Output = BigDecimal

The resulting type after applying the + operator.
Source§

fn add(self, rhs: &i64) -> BigDecimal

Performs the + operation. Read more
Source§

impl Add<&i8> for BigDecimalRef<'_>

Source§

type Output = BigDecimal

The resulting type after applying the + operator.
Source§

fn add(self, rhs: &i8) -> BigDecimal

Performs the + operation. Read more
Source§

impl Add<&u128> for BigDecimalRef<'_>

Source§

type Output = BigDecimal

The resulting type after applying the + operator.
Source§

fn add(self, rhs: &u128) -> BigDecimal

Performs the + operation. Read more
Source§

impl Add<&u16> for BigDecimalRef<'_>

Source§

type Output = BigDecimal

The resulting type after applying the + operator.
Source§

fn add(self, rhs: &u16) -> BigDecimal

Performs the + operation. Read more
Source§

impl Add<&u32> for BigDecimalRef<'_>

Source§

type Output = BigDecimal

The resulting type after applying the + operator.
Source§

fn add(self, rhs: &u32) -> BigDecimal

Performs the + operation. Read more
Source§

impl Add<&u64> for BigDecimalRef<'_>

Source§

type Output = BigDecimal

The resulting type after applying the + operator.
Source§

fn add(self, rhs: &u64) -> BigDecimal

Performs the + operation. Read more
Source§

impl Add<&u8> for BigDecimalRef<'_>

Source§

type Output = BigDecimal

The resulting type after applying the + operator.
Source§

fn add(self, rhs: &u8) -> BigDecimal

Performs the + operation. Read more
Source§

impl Add<BigDecimal> for BigDecimalRef<'_>

Source§

type Output = BigDecimal

The resulting type after applying the + operator.
Source§

fn add(self, rhs: BigDecimal) -> BigDecimal

Performs the + operation. Read more
Source§

impl<'a> Add<BigDecimalRef<'a>> for &BigInt

Source§

type Output = BigDecimal

The resulting type after applying the + operator.
Source§

fn add(self, rhs: BigDecimalRef<'_>) -> BigDecimal

Performs the + operation. Read more
Source§

impl<'a> Add<BigDecimalRef<'a>> for BigInt

Source§

type Output = BigDecimal

The resulting type after applying the + operator.
Source§

fn add(self, rhs: BigDecimalRef<'_>) -> BigDecimal

Performs the + operation. Read more
Source§

impl Add<BigInt> for BigDecimalRef<'_>

Source§

type Output = BigDecimal

The resulting type after applying the + operator.
Source§

fn add(self, rhs: BigInt) -> BigDecimal

Performs the + operation. Read more
Source§

impl<'a, T: Into<BigDecimalRef<'a>>> Add<T> for BigDecimalRef<'_>

Source§

type Output = BigDecimal

The resulting type after applying the + operator.
Source§

fn add(self, rhs: T) -> BigDecimal

Performs the + operation. Read more
Source§

impl Add<i128> for BigDecimalRef<'_>

Source§

type Output = BigDecimal

The resulting type after applying the + operator.
Source§

fn add(self, rhs: i128) -> BigDecimal

Performs the + operation. Read more
Source§

impl Add<i16> for BigDecimalRef<'_>

Source§

type Output = BigDecimal

The resulting type after applying the + operator.
Source§

fn add(self, rhs: i16) -> BigDecimal

Performs the + operation. Read more
Source§

impl Add<i32> for BigDecimalRef<'_>

Source§

type Output = BigDecimal

The resulting type after applying the + operator.
Source§

fn add(self, rhs: i32) -> BigDecimal

Performs the + operation. Read more
Source§

impl Add<i64> for BigDecimalRef<'_>

Source§

type Output = BigDecimal

The resulting type after applying the + operator.
Source§

fn add(self, rhs: i64) -> BigDecimal

Performs the + operation. Read more
Source§

impl Add<i8> for BigDecimalRef<'_>

Source§

type Output = BigDecimal

The resulting type after applying the + operator.
Source§

fn add(self, rhs: i8) -> BigDecimal

Performs the + operation. Read more
Source§

impl Add<u128> for BigDecimalRef<'_>

Source§

type Output = BigDecimal

The resulting type after applying the + operator.
Source§

fn add(self, rhs: u128) -> BigDecimal

Performs the + operation. Read more
Source§

impl Add<u16> for BigDecimalRef<'_>

Source§

type Output = BigDecimal

The resulting type after applying the + operator.
Source§

fn add(self, rhs: u16) -> BigDecimal

Performs the + operation. Read more
Source§

impl Add<u32> for BigDecimalRef<'_>

Source§

type Output = BigDecimal

The resulting type after applying the + operator.
Source§

fn add(self, rhs: u32) -> BigDecimal

Performs the + operation. Read more
Source§

impl Add<u64> for BigDecimalRef<'_>

Source§

type Output = BigDecimal

The resulting type after applying the + operator.
Source§

fn add(self, rhs: u64) -> BigDecimal

Performs the + operation. Read more
Source§

impl Add<u8> for BigDecimalRef<'_>

Source§

type Output = BigDecimal

The resulting type after applying the + operator.
Source§

fn add(self, rhs: u8) -> BigDecimal

Performs the + operation. Read more
Source§

impl<'a> Clone for BigDecimalRef<'a>

Source§

fn clone(&self) -> BigDecimalRef<'a>

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<'a> Debug for BigDecimalRef<'a>

Source§

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

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

impl Display for BigDecimalRef<'_>

Source§

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

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

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

Source§

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

Converts to this type from the input type.
Source§

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

Source§

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

Converts to this type from the input type.
Source§

impl LowerExp for BigDecimalRef<'_>

Source§

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

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

impl Neg for BigDecimalRef<'_>

Source§

type Output = BigDecimalRef<'_>

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self::Output

Performs the unary - operation. Read more
Source§

impl Ord for BigDecimalRef<'_>

Source§

fn cmp(&self, other: &BigDecimalRef<'_>) -> Ordering

Complete ordering implementation for BigDecimal

§Example
use std::str::FromStr;

let a = bigdecimal::BigDecimal::from_str("-1").unwrap();
let b = bigdecimal::BigDecimal::from_str("1").unwrap();
assert!(a < b);
assert!(b > a);
let c = bigdecimal::BigDecimal::from_str("1").unwrap();
assert!(b >= c);
assert!(c >= b);
let d = bigdecimal::BigDecimal::from_str("10.0").unwrap();
assert!(d > c);
let e = bigdecimal::BigDecimal::from_str(".5").unwrap();
assert!(e < c);
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
Source§

impl<'rhs, T> PartialEq<T> for BigDecimalRef<'_>
where T: Into<BigDecimalRef<'rhs>> + Copy,

Source§

fn eq(&self, rhs: &T) -> 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.
Source§

impl PartialOrd for BigDecimalRef<'_>

Source§

fn partial_cmp(&self, other: &BigDecimalRef<'_>) -> 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
Source§

impl Sub<BigDecimal> for BigDecimalRef<'_>

Source§

type Output = BigDecimal

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: BigDecimal) -> BigDecimal

Performs the - operation. Read more
Source§

impl<'a> Sub<BigDecimalRef<'a>> for &BigInt

Source§

type Output = BigDecimal

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: BigDecimalRef<'a>) -> BigDecimal

Performs the - operation. Read more
Source§

impl<'a> Sub<BigDecimalRef<'a>> for BigInt

Source§

type Output = BigDecimal

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: BigDecimalRef<'a>) -> BigDecimal

Performs the - operation. Read more
Source§

impl Sub<BigInt> for BigDecimalRef<'_>

Source§

type Output = BigDecimal

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: BigInt) -> BigDecimal

Performs the - operation. Read more
Source§

impl<'a, T: Into<BigDecimalRef<'a>>> Sub<T> for BigDecimalRef<'_>

Source§

type Output = BigDecimal

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: T) -> BigDecimal

Performs the - operation. Read more
Source§

impl UpperExp for BigDecimalRef<'_>

Source§

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

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

impl<'a> Copy for BigDecimalRef<'a>

Source§

impl<'a> Eq for BigDecimalRef<'a>

Auto Trait Implementations§

§

impl<'a> Freeze for BigDecimalRef<'a>

§

impl<'a> RefUnwindSafe for BigDecimalRef<'a>

§

impl<'a> Send for BigDecimalRef<'a>

§

impl<'a> Sync for BigDecimalRef<'a>

§

impl<'a> Unpin for BigDecimalRef<'a>

§

impl<'a> UnwindSafe for BigDecimalRef<'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, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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> 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>,

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.