Struct bigdecimal::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 sqrt_with_context(&self, ctx: &Context) -> Option<BigDecimal>

Take square root of this number

source

pub fn is_zero(&self) -> bool

Return if the referenced decimal is zero

Trait Implementations§

source§

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

§

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<'_>

§

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<'_>

§

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<'_>

§

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<'_>

§

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<'_>

§

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<'_>

§

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<'_>

§

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<'_>

§

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<'_>

§

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<'_>

§

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

§

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

§

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<'_>

§

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<'_>

§

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<'_>

§

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<'_>

§

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<'_>

§

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<'_>

§

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<'_>

§

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<'_>

§

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<'_>

§

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<'_>

§

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<'_>

§

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<'_>

§

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

impl Neg for BigDecimalRef<'_>

§

type Output = BigDecimalRef<'_>

The resulting type after applying the - operator.
source§

fn neg(self) -> Self::Output

Performs the unary - operation. Read more
source§

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

source§

fn eq(&self, rhs: &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 Sub<BigDecimal> for BigDecimalRef<'_>

§

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

§

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

§

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<'_>

§

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<'_>

§

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

impl<'a> Copy 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> 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,

§

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.