Skip to main content

OobScaleFpdec

Struct OobScaleFpdec 

Source
pub struct OobScaleFpdec<I>(/* private fields */);
Expand description

Out-of-band-scale fixed-point decimal.

I is the inner integer type. It could be signed i8, i16, i32, i64, i128, or unsigned u8, u16, u32, u64, u128, with about 2, 4, 9, 18/19 and 38 significant digits respectively.

For example, OobScaleFpdec<i64> means using i64 as the underlying integer. It’s your job to save the out-of-band scale somewhere else.

The scale can be positive for fraction precision or be negative for omitting the low-order digits of integer values.

Compared to ConstScaleFpdec, this OobScaleFpdec has more verbose APIs:

  • extra diff_scale argument for most operations such as * and /, but no need for + and -,
  • use try_from_str() to convert from string with scale set,
  • use (*, i32) tuple for converting from integers or floats,
  • use to_f32() or to_f64() to convert to floats,
  • use OobFmt for Display and FromStr,
  • no associate const SCALE,
  • and others.

See the module-level documentation for more information.

Implementations§

Source§

impl<I> OobScaleFpdec<I>
where I: FpdecInner,

Source

pub const ZERO: Self

The zero value.

Source

pub const MAX: Self

The largest value, (2b - 1) / 10S, where b is the bits of I.

Source

pub const MIN: Self

The smallest value, -(2b / 10S), where b is the bits of I.

Source

pub const EPSILON: Self

The smallest difference value, 10-S .

Source

pub const MAX_POWERS: Self

The largest powers of 10.

Source

pub const DIGITS: u32 = I::DIGITS

Approximate number of significant digits in base 10.

Source

pub fn checked_add(self, rhs: Self) -> Option<Self>

Checked addition. Computes self + rhs, returning None if overflow occurred.

If you make sure no overflow error, you can use + and += instead for convenience.

The right operand must have the same scale with self.

Source

pub fn checked_sub(self, rhs: Self) -> Option<Self>

Checked subtraction. Computes self - rhs, returning None if overflow occurred.

If you make sure no overflow error, you can use - and -= instead for convenience.

The right operand must have the same scale with self.

Source

pub fn checked_mul_int(self, n: impl Into<I>) -> Option<Self>

Checked multiplication with integer. Computes self * n, returning None if overflow occurred.

If you make sure no overflow error, you can use * and *= instead for convenience.

Source

pub fn checked_mul_ratio<R>(self, a: R, b: R) -> Option<Self>
where R: IntoRatioInt<I>,

Computes self * a/b, returning None if overflow occurred.

Equivalent to Self::checked_mul_ratio_ext with Rounding::Round.

Source

pub fn checked_mul_ratio_ext<R>( self, a: R, b: R, rounding: Rounding, ) -> Option<Self>
where R: IntoRatioInt<I>,

Computes self * a/b, returning None if overflow occurred.

The arguments a and b could be primitive integers, ConstScaleFpdec or OobScaleFpdec with same scale.

Compared to calling Self::checked_mul_int and Self::checked_div_int separately, this can avoid some potential overflow.

Examples:

// Here we use `ConstScaleFpdec` as example. It's same for `OobScaleFpdec`.
use primitive_fixed_point_decimal::{ConstScaleFpdec, fpdec};
type Balance = ConstScaleFpdec<i64, 2>;
type Quantity = ConstScaleFpdec<i32, 4>; // type for `a` and `b`

let margin: Balance = fpdec!(30000);
let deal: Quantity = fpdec!(0.2);
let total: Quantity = fpdec!(0.3);
assert_eq!(margin.checked_mul_ratio(deal, total).unwrap(), fpdec!(20000));

// integer
assert_eq!(margin.checked_mul_ratio(200, 300).unwrap(), fpdec!(20000));
Source

pub fn checked_div_int(self, n: impl Into<I>) -> Option<Self>

Checked division by integer, with Rounding::Round.

Computes self / n, returning None if n == 0 or overflow occurres.

If you make sure no overflow or 0-division error, you can use / and /= instead for convenience.

Source

pub fn checked_div_int_ext( self, n: impl Into<I>, rounding: Rounding, ) -> Option<Self>

Checked division by integer with rounding.

Computes self / n, returning None if n == 0 or overflow occurres.

Source

pub fn is_zero(&self) -> bool

Return if zero.

Source

pub const fn from_mantissa(i: I) -> Self

Create a decimal from the underlying integer representation.

You must take care of the scale yourself.

This method is const, so it can be used to construct const values.

Examples:

// Here we use `ConstScaleFpdec` as example. It's same for `OobScaleFpdec`.
use primitive_fixed_point_decimal::{ConstScaleFpdec, fpdec};
type Dec = ConstScaleFpdec<i32, 4>; // scale is 4

assert_eq!(Dec::from_mantissa(123400), fpdec!(12.34));

// const values
const ONE: Dec = Dec::from_mantissa(1 * 10000);
const TEN: Dec = Dec::from_mantissa(10 * 10000);
const PI: Dec = Dec::from_mantissa(31400);
assert_eq!(ONE, fpdec!(1));
assert_eq!(TEN, fpdec!(10));
assert_eq!(PI, fpdec!(3.14));

// you can use `Dec::SCALE` for better consistency
const ONE_1: Dec = Dec::from_mantissa(10_i32.pow(Dec::SCALE as u32));
const TEN_1: Dec = Dec::from_mantissa(10_i32.pow(Dec::SCALE as u32 + 1));
const PI_1: Dec = Dec::from_mantissa(314 * 10_i32.pow(Dec::SCALE as u32 - 2));
assert_eq!(ONE_1, fpdec!(1));
assert_eq!(TEN_1, fpdec!(10));
assert_eq!(PI_1, fpdec!(3.14));
Source

pub const fn mantissa(self) -> I

Return the underlying integer representation.

You must take care of the scale yourself.

Examples:

// Here we use `ConstScaleFpdec` as example. It's same for `OobScaleFpdec`.
use primitive_fixed_point_decimal::{ConstScaleFpdec, fpdec};
type Dec = ConstScaleFpdec<i32, 4>; // scale is 4

let d: Dec = fpdec!(12.34);
assert_eq!(d.mantissa(), 123400);
Source

pub fn checked_mul<J>( self, rhs: OobScaleFpdec<J>, diff_scale: i32, ) -> Option<OobScaleFpdec<I>>
where J: FpdecInner,

Checked multiplication.

Equivalent to Self::checked_mul_ext with Rounding::Round.

Source

pub fn checked_mul_ext<J>( self, rhs: OobScaleFpdec<J>, diff_scale: i32, rounding: Rounding, ) -> Option<OobScaleFpdec<I>>
where J: FpdecInner,

Checked multiplication. Computes self * rhs, returning None if overflow occurred or argument diff_scale is out of range [-Self::DIGITS, Self::DIGITS].

The type of rhs can have different inner integer J, while the type of result must have the same I.

Argument: diff_scale = scale(self) + scale(rhs) - scale(result).

If the diff_scale < 0, then rounding operations are required and precision may be lost. You can specify the rounding type.

§Examples
use primitive_fixed_point_decimal::{OobScaleFpdec, Rounding, fpdec};
type Balance = OobScaleFpdec<i64>;
type FeeRate = OobScaleFpdec<i16>; // different types

let balance: Balance = fpdec!(12.30, 2); // scale=2
let rate: FeeRate = fpdec!(0.01, 4); // scale=4

let fee: Balance = balance.checked_mul(rate, 4).unwrap();
assert_eq!(fee, fpdec!(0.12, 2));

let fee: Balance = balance.checked_mul_ext(rate, 4, Rounding::Ceiling).unwrap();
assert_eq!(fee, fpdec!(0.13, 2));
Source

pub fn checked_mul_const_scale<J, const S: i32>( self, rhs: ConstScaleFpdec<J, S>, ) -> Option<OobScaleFpdec<I>>
where J: FpdecInner,

Checked multiplication with ConstScaleFpdec.

Equivalent to Self::checked_mul_const_scale_ext with Rounding::Round.

If you make sure no overflow error, you can use * and *= instead for convenience.

Source

pub fn checked_mul_const_scale_ext<J, const S: i32>( self, rhs: ConstScaleFpdec<J, S>, rounding: Rounding, ) -> Option<OobScaleFpdec<I>>
where J: FpdecInner,

Checked multiplication with ConstScaleFpdec. Computes self * rhs, returning None if overflow occurred.

Compared to Self::checked_mul_ext, the result of this method inherits the scale of self. So the diff_scale (which is equal to S) is no more need.

§Examples
use primitive_fixed_point_decimal::{ConstScaleFpdec, OobScaleFpdec, Rounding, fpdec};
type Balance = OobScaleFpdec<i64>;
type FeeRate = ConstScaleFpdec<i16, 4>;

let balance: Balance = fpdec!(12.30, 2); // scale=2
let rate: FeeRate = fpdec!(0.01);

let fee: Balance = balance.checked_mul_const_scale(rate).unwrap();
assert_eq!(fee, fpdec!(0.12, 2));

let fee: Balance = balance.checked_mul_const_scale_ext(rate, Rounding::Ceiling).unwrap();
assert_eq!(fee, fpdec!(0.13, 2));
Source

pub fn checked_div<J>( self, rhs: OobScaleFpdec<J>, diff_scale: i32, ) -> Option<OobScaleFpdec<I>>
where J: FpdecInner,

Checked division.

Equivalent to Self::checked_div_ext with Rounding::Round.

Source

pub fn checked_div_ext<J>( self, rhs: OobScaleFpdec<J>, diff_scale: i32, rounding: Rounding, ) -> Option<OobScaleFpdec<I>>
where J: FpdecInner,

Checked division. Computes self / rhs, returning None if division by 0, or overflow occurred, or argument diff_scale is out of range [-Self::DIGITS, Self::DIGITS].

The type of rhs can have different inner integer J, while the type of result must have the same I.

Argument: diff_scale = scale(self) - scale(rhs) - scale(result).

You can specify the rounding type.

§Examples
use primitive_fixed_point_decimal::{OobScaleFpdec, Rounding, fpdec};
type Balance = OobScaleFpdec<i64>;
type FeeRate = OobScaleFpdec<i16>; // different types

let fee: Balance = fpdec!(0.13, 2); // scale=2
let rate: FeeRate = fpdec!(0.03, 4); // scale=4

let balance: Balance = fee.checked_div_ext(rate, -4, Rounding::Ceiling).unwrap();
assert_eq!(balance, fpdec!(4.34, 2));
Source

pub fn checked_div_const_scale<J, const S: i32>( self, rhs: ConstScaleFpdec<J, S>, ) -> Option<OobScaleFpdec<I>>
where J: FpdecInner,

Checked division with ConstScaleFpdec.

Equivalent to Self::checked_div_const_scale_ext with Rounding::Round.

If you make sure no overflow or 0-division error, you can use / and /= instead for convenience.

Source

pub fn checked_div_const_scale_ext<J, const S: i32>( self, rhs: ConstScaleFpdec<J, S>, rounding: Rounding, ) -> Option<OobScaleFpdec<I>>
where J: FpdecInner,

Checked division with ConstScaleFpdec. Computes self / rhs, returning None if division by 0 or overflow occurred.

Compared to Self::checked_div_ext, the result of this method inherits the scale of self. So the diff_scale (which is equal to -S) is no more need.

§Examples
use primitive_fixed_point_decimal::{ConstScaleFpdec, OobScaleFpdec, Rounding, fpdec};
type Balance = OobScaleFpdec<i64>;
type FeeRate = ConstScaleFpdec<i16, 4>;

let fee: Balance = fpdec!(0.12, 2); // scale=2
let rate: FeeRate = fpdec!(0.01);

let balance: Balance = fee.checked_div_const_scale(rate).unwrap();
assert_eq!(balance, fpdec!(12.0, 2));
Source

pub fn round_diff(self, diff_scale: i32) -> Self

Round the decimal.

Equivalent to Self::round_diff_ext with Rounding::Round.

Source

pub fn round_diff_ext(self, diff_scale: i32, rounding: Rounding) -> Self

Round the decimal with rounding type.

The argument diff_scale is original_scale - round_scale. Return the original decimal if diff_scale <= 0.

Examples:

use primitive_fixed_point_decimal::{OobScaleFpdec, Rounding, fpdec};
type Price = OobScaleFpdec<i64>;

let price: Price = fpdec!(12.12345678, 8); // scale=8

assert_eq!(price.round_diff(8 - 6), // reduce 2 scale
    fpdec!(12.123457, 8)); // `Rounding::Round` as default

assert_eq!(price.round_diff_ext(8 - 6, Rounding::Floor),
    fpdec!(12.123456, 8));
Source

pub fn try_from_str(s: &str, scale: i32) -> Result<Self, ParseError>
where I: Num<FromStrRadixErr = ParseIntError>,

Read decimal from string.

This method has 2 limitations:

  1. Support decimal format only but not scientific notation;
  2. Return ParseError::Precision if the string has more precision.

If you want to skip these limitations, you can parse the string to float number first and then convert the number to this decimal.

Examples:

use primitive_fixed_point_decimal::{OobScaleFpdec, ParseError, fpdec};
type Decimal = OobScaleFpdec<i16>;

assert_eq!(Decimal::try_from_str("1.23", 4).unwrap(), fpdec!(1.23, 4));
assert_eq!(Decimal::try_from_str("9999", 4), Err(ParseError::Overflow));
assert_eq!(Decimal::try_from_str("1.23456", 4), Err(ParseError::Precision));
Source

pub fn to_f32(self, scale: i32) -> f32

Convert into f32.

Examples:

use primitive_fixed_point_decimal::{OobScaleFpdec, fpdec};
type Decimal = OobScaleFpdec<i32>;

let dec: Decimal = fpdec!(1.234, 4);
assert_eq!(dec.to_f32(4), 1.234);

let dec: Decimal = fpdec!(1234000, -3);
assert_eq!(dec.to_f32(-3), 1234000.0);
Source

pub fn to_f64(self, scale: i32) -> f64

Convert into f64.

Examples:

use primitive_fixed_point_decimal::{OobScaleFpdec, fpdec};
type Decimal = OobScaleFpdec<i32>;

let dec: Decimal = fpdec!(1.234, 4);
assert_eq!(dec.to_f64(4), 1.234);

let dec: Decimal = fpdec!(1234000, -3);
assert_eq!(dec.to_f64(-3), 1234000.0);
Source§

impl<I> OobScaleFpdec<I>
where I: FpdecInner + Signed,

Source

pub fn abs(self) -> Self

Computes the absolute value of self.

§Overflow behavior

The absolute value of MIN cannot be represented as this type, and attempting to calculate it will cause an overflow. This means that code in debug mode will trigger a panic on this case and optimized code will return MIN without a panic.

Source

pub fn checked_abs(self) -> Option<Self>

Checked absolute value. Computes self.abs(), returning None if self == MIN.

Source

pub fn is_neg(&self) -> bool

Return if negative.

Source

pub fn is_pos(&self) -> bool

Return if positive.

Trait Implementations§

Source§

impl<I> Add for OobScaleFpdec<I>
where I: FpdecInner,

Source§

type Output = OobScaleFpdec<I>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Self) -> Self::Output

Performs the + operation. Read more
Source§

impl<I> AddAssign for OobScaleFpdec<I>
where I: FpdecInner,

Source§

fn add_assign(&mut self, rhs: Self)

Performs the += operation. Read more
Source§

impl<I: Clone> Clone for OobScaleFpdec<I>

Source§

fn clone(&self) -> OobScaleFpdec<I>

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
Source§

impl<I: Debug> Debug for OobScaleFpdec<I>

Source§

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

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

impl<I: Default> Default for OobScaleFpdec<I>

Source§

fn default() -> OobScaleFpdec<I>

Returns the “default value” for a type. Read more
Source§

impl<I, J, const S: i32> Div<ConstScaleFpdec<J, S>> for OobScaleFpdec<I>
where I: FpdecInner, J: FpdecInner,

Performs the / operation with ConstScaleFpdec.

§Panics

If Self::checked_div_const_scale returns None.

Source§

type Output = OobScaleFpdec<I>

The resulting type after applying the / operator.
Source§

fn div(self, rhs: ConstScaleFpdec<J, S>) -> Self::Output

Performs the / operation. Read more
Source§

impl<I, J> Div<J> for OobScaleFpdec<I>
where I: FpdecInner, J: Into<I> + Num,

Performs the / operation with an integer.

§Panics

If Self::checked_div_int returns None.

Source§

type Output = OobScaleFpdec<I>

The resulting type after applying the / operator.
Source§

fn div(self, rhs: J) -> Self::Output

Performs the / operation. Read more
Source§

impl<I, J, const S: i32> DivAssign<ConstScaleFpdec<J, S>> for OobScaleFpdec<I>
where I: FpdecInner, J: FpdecInner,

Source§

fn div_assign(&mut self, rhs: ConstScaleFpdec<J, S>)

Performs the /= operation. Read more
Source§

impl<I, J> DivAssign<J> for OobScaleFpdec<I>
where I: FpdecInner, J: Into<I> + Num,

Source§

fn div_assign(&mut self, rhs: J)

Performs the /= operation. Read more
Source§

impl<I, const S: i32> From<ConstScaleFpdec<I, S>> for OobScaleFpdec<I>
where I: FpdecInner,

Source§

fn from(sd: ConstScaleFpdec<I, S>) -> Self

Convert from ConstScaleFpdec to OobScaleFpdec with scale S.

Examples:

use primitive_fixed_point_decimal::{ConstScaleFpdec, OobScaleFpdec, fpdec};
type ConstDec = ConstScaleFpdec<i32, 6>;
type OobDec = OobScaleFpdec<i32>; // the OOB scale is 6 too

let sd: ConstDec = fpdec!(123.45);
let od: OobDec = sd.into(); // `od` has the same scale=6
assert_eq!(od, fpdec!(123.45, 6));
Source§

impl<I, const S: i32> From<OobScaleFpdec<I>> for ConstScaleFpdec<I, S>
where I: FpdecInner,

Source§

fn from(od: OobScaleFpdec<I>) -> Self

Convert from OobScaleFpdec with scale S to ConstScaleFpdec.

Examples:

use primitive_fixed_point_decimal::{ConstScaleFpdec, OobScaleFpdec, fpdec};
type ConstDec = ConstScaleFpdec<i32, 6>;
type OobDec = OobScaleFpdec<i32>; // the OOB scale is 6 too

let od: OobDec = fpdec!(123.45, 6); // make sure that `od` has the same scale=6
let sd: ConstDec = od.into();
assert_eq!(sd, fpdec!(123.45));
Source§

impl<I: Hash> Hash for OobScaleFpdec<I>

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<I, J> IntoRatioInt<J> for OobScaleFpdec<I>
where I: FpdecInner + Into<J>,

Source§

fn to_int(self) -> J

Source§

impl<I, J, const S: i32> Mul<ConstScaleFpdec<J, S>> for OobScaleFpdec<I>
where I: FpdecInner, J: FpdecInner,

Performs the * operation with ConstScaleFpdec.

§Panics

If Self::checked_mul_const_scale returns None.

Source§

type Output = OobScaleFpdec<I>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: ConstScaleFpdec<J, S>) -> Self::Output

Performs the * operation. Read more
Source§

impl<I, J> Mul<J> for OobScaleFpdec<I>
where I: FpdecInner, J: Into<I> + Num,

Performs the * operation with an integer.

§Panics

If Self::checked_mul_int returns None.

Source§

type Output = OobScaleFpdec<I>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: J) -> Self::Output

Performs the * operation. Read more
Source§

impl<I, J, const S: i32> MulAssign<ConstScaleFpdec<J, S>> for OobScaleFpdec<I>
where I: FpdecInner, J: FpdecInner,

Source§

fn mul_assign(&mut self, rhs: ConstScaleFpdec<J, S>)

Performs the *= operation. Read more
Source§

impl<I, J> MulAssign<J> for OobScaleFpdec<I>
where I: FpdecInner, J: Into<I> + Num,

Source§

fn mul_assign(&mut self, rhs: J)

Performs the *= operation. Read more
Source§

impl<I> Neg for OobScaleFpdec<I>
where I: FpdecInner + Signed,

Source§

type Output = OobScaleFpdec<I>

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self::Output

Performs the unary - operation. Read more
Source§

impl<I: Ord> Ord for OobScaleFpdec<I>

Source§

fn cmp(&self, other: &OobScaleFpdec<I>) -> 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
Source§

impl<I: PartialEq> PartialEq for OobScaleFpdec<I>

Source§

fn eq(&self, other: &OobScaleFpdec<I>) -> 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<I: PartialOrd> PartialOrd for OobScaleFpdec<I>

Source§

fn partial_cmp(&self, other: &OobScaleFpdec<I>) -> 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<I> Sub for OobScaleFpdec<I>
where I: FpdecInner,

Source§

type Output = OobScaleFpdec<I>

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Self) -> Self::Output

Performs the - operation. Read more
Source§

impl<I> SubAssign for OobScaleFpdec<I>
where I: FpdecInner,

Source§

fn sub_assign(&mut self, rhs: Self)

Performs the -= operation. Read more
Source§

impl<I> TryFrom<(f32, i32)> for OobScaleFpdec<I>

Source§

fn try_from(t: (f32, i32)) -> Result<Self, Self::Error>

Convert from float and scale. Returning error if overflow occurred.

Since it’s hard for the float types to represent decimal fraction exactly, so this method always rounds the float number into OobScaleFpdec.

Examples:

use core::str::FromStr;
use primitive_fixed_point_decimal::{OobScaleFpdec, ParseError};
type Decimal = OobScaleFpdec<i32>;

assert_eq!(Decimal::try_from((1.23, 4)).unwrap(), Decimal::try_from_str("1.23", 4).unwrap());
assert_eq!(Decimal::try_from((1.23456789, 4)).unwrap(), Decimal::try_from_str("1.2346", 4).unwrap());
Source§

type Error = ParseError

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

impl<I> TryFrom<(f64, i32)> for OobScaleFpdec<I>

Source§

fn try_from(t: (f64, i32)) -> Result<Self, Self::Error>

Convert from float and scale. Returning error if overflow occurred.

Since it’s hard for the float types to represent decimal fraction exactly, so this method always rounds the float number into OobScaleFpdec.

Examples:

use core::str::FromStr;
use primitive_fixed_point_decimal::{OobScaleFpdec, ParseError};
type Decimal = OobScaleFpdec<i32>;

assert_eq!(Decimal::try_from((1.23, 4)).unwrap(), Decimal::try_from_str("1.23", 4).unwrap());
assert_eq!(Decimal::try_from((1.23456789, 4)).unwrap(), Decimal::try_from_str("1.2346", 4).unwrap());
Source§

type Error = ParseError

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

impl<I> TryFrom<(i128, i32)> for OobScaleFpdec<I>
where I: FpdecInner,

Source§

fn try_from(i: (i128, i32)) -> Result<Self, Self::Error>

Convert from integer with scale. Returning error if overflow occurred or lossing precision under scale < 0.

Examples:

use core::str::FromStr;
use primitive_fixed_point_decimal::{OobScaleFpdec, ParseError};
type Decimal = OobScaleFpdec<i32>;

assert_eq!(Decimal::try_from((123, 4)).unwrap(), Decimal::try_from_str("123", 4).unwrap());
assert_eq!(Decimal::try_from((123_i8, 4)).unwrap(), Decimal::try_from_str("123", 4).unwrap());
assert_eq!(Decimal::try_from((120000000000_i64, -10)).unwrap(), Decimal::try_from_str("120000000000", -10).unwrap());
assert_eq!(Decimal::try_from((9999999, 4)), Err(ParseError::Overflow));
assert_eq!(Decimal::try_from((123, -4)), Err(ParseError::Precision));
Source§

type Error = ParseError

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

impl<I> TryFrom<(i16, i32)> for OobScaleFpdec<I>
where I: FpdecInner,

Source§

fn try_from(i: (i16, i32)) -> Result<Self, Self::Error>

Convert from integer with scale. Returning error if overflow occurred or lossing precision under scale < 0.

Examples:

use core::str::FromStr;
use primitive_fixed_point_decimal::{OobScaleFpdec, ParseError};
type Decimal = OobScaleFpdec<i32>;

assert_eq!(Decimal::try_from((123, 4)).unwrap(), Decimal::try_from_str("123", 4).unwrap());
assert_eq!(Decimal::try_from((123_i8, 4)).unwrap(), Decimal::try_from_str("123", 4).unwrap());
assert_eq!(Decimal::try_from((120000000000_i64, -10)).unwrap(), Decimal::try_from_str("120000000000", -10).unwrap());
assert_eq!(Decimal::try_from((9999999, 4)), Err(ParseError::Overflow));
assert_eq!(Decimal::try_from((123, -4)), Err(ParseError::Precision));
Source§

type Error = ParseError

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

impl<I> TryFrom<(i32, i32)> for OobScaleFpdec<I>
where I: FpdecInner,

Source§

fn try_from(i: (i32, i32)) -> Result<Self, Self::Error>

Convert from integer with scale. Returning error if overflow occurred or lossing precision under scale < 0.

Examples:

use core::str::FromStr;
use primitive_fixed_point_decimal::{OobScaleFpdec, ParseError};
type Decimal = OobScaleFpdec<i32>;

assert_eq!(Decimal::try_from((123, 4)).unwrap(), Decimal::try_from_str("123", 4).unwrap());
assert_eq!(Decimal::try_from((123_i8, 4)).unwrap(), Decimal::try_from_str("123", 4).unwrap());
assert_eq!(Decimal::try_from((120000000000_i64, -10)).unwrap(), Decimal::try_from_str("120000000000", -10).unwrap());
assert_eq!(Decimal::try_from((9999999, 4)), Err(ParseError::Overflow));
assert_eq!(Decimal::try_from((123, -4)), Err(ParseError::Precision));
Source§

type Error = ParseError

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

impl<I> TryFrom<(i64, i32)> for OobScaleFpdec<I>
where I: FpdecInner,

Source§

fn try_from(i: (i64, i32)) -> Result<Self, Self::Error>

Convert from integer with scale. Returning error if overflow occurred or lossing precision under scale < 0.

Examples:

use core::str::FromStr;
use primitive_fixed_point_decimal::{OobScaleFpdec, ParseError};
type Decimal = OobScaleFpdec<i32>;

assert_eq!(Decimal::try_from((123, 4)).unwrap(), Decimal::try_from_str("123", 4).unwrap());
assert_eq!(Decimal::try_from((123_i8, 4)).unwrap(), Decimal::try_from_str("123", 4).unwrap());
assert_eq!(Decimal::try_from((120000000000_i64, -10)).unwrap(), Decimal::try_from_str("120000000000", -10).unwrap());
assert_eq!(Decimal::try_from((9999999, 4)), Err(ParseError::Overflow));
assert_eq!(Decimal::try_from((123, -4)), Err(ParseError::Precision));
Source§

type Error = ParseError

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

impl<I> TryFrom<(i8, i32)> for OobScaleFpdec<I>
where I: FpdecInner,

Source§

fn try_from(i: (i8, i32)) -> Result<Self, Self::Error>

Convert from integer with scale. Returning error if overflow occurred or lossing precision under scale < 0.

Examples:

use core::str::FromStr;
use primitive_fixed_point_decimal::{OobScaleFpdec, ParseError};
type Decimal = OobScaleFpdec<i32>;

assert_eq!(Decimal::try_from((123, 4)).unwrap(), Decimal::try_from_str("123", 4).unwrap());
assert_eq!(Decimal::try_from((123_i8, 4)).unwrap(), Decimal::try_from_str("123", 4).unwrap());
assert_eq!(Decimal::try_from((120000000000_i64, -10)).unwrap(), Decimal::try_from_str("120000000000", -10).unwrap());
assert_eq!(Decimal::try_from((9999999, 4)), Err(ParseError::Overflow));
assert_eq!(Decimal::try_from((123, -4)), Err(ParseError::Precision));
Source§

type Error = ParseError

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

impl<I> TryFrom<(u128, i32)> for OobScaleFpdec<I>
where I: FpdecInner,

Source§

fn try_from(i: (u128, i32)) -> Result<Self, Self::Error>

Convert from integer with scale. Returning error if overflow occurred or lossing precision under scale < 0.

Examples:

use core::str::FromStr;
use primitive_fixed_point_decimal::{OobScaleFpdec, ParseError};
type Decimal = OobScaleFpdec<i32>;

assert_eq!(Decimal::try_from((123, 4)).unwrap(), Decimal::try_from_str("123", 4).unwrap());
assert_eq!(Decimal::try_from((123_i8, 4)).unwrap(), Decimal::try_from_str("123", 4).unwrap());
assert_eq!(Decimal::try_from((120000000000_i64, -10)).unwrap(), Decimal::try_from_str("120000000000", -10).unwrap());
assert_eq!(Decimal::try_from((9999999, 4)), Err(ParseError::Overflow));
assert_eq!(Decimal::try_from((123, -4)), Err(ParseError::Precision));
Source§

type Error = ParseError

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

impl<I> TryFrom<(u16, i32)> for OobScaleFpdec<I>
where I: FpdecInner,

Source§

fn try_from(i: (u16, i32)) -> Result<Self, Self::Error>

Convert from integer with scale. Returning error if overflow occurred or lossing precision under scale < 0.

Examples:

use core::str::FromStr;
use primitive_fixed_point_decimal::{OobScaleFpdec, ParseError};
type Decimal = OobScaleFpdec<i32>;

assert_eq!(Decimal::try_from((123, 4)).unwrap(), Decimal::try_from_str("123", 4).unwrap());
assert_eq!(Decimal::try_from((123_i8, 4)).unwrap(), Decimal::try_from_str("123", 4).unwrap());
assert_eq!(Decimal::try_from((120000000000_i64, -10)).unwrap(), Decimal::try_from_str("120000000000", -10).unwrap());
assert_eq!(Decimal::try_from((9999999, 4)), Err(ParseError::Overflow));
assert_eq!(Decimal::try_from((123, -4)), Err(ParseError::Precision));
Source§

type Error = ParseError

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

impl<I> TryFrom<(u32, i32)> for OobScaleFpdec<I>
where I: FpdecInner,

Source§

fn try_from(i: (u32, i32)) -> Result<Self, Self::Error>

Convert from integer with scale. Returning error if overflow occurred or lossing precision under scale < 0.

Examples:

use core::str::FromStr;
use primitive_fixed_point_decimal::{OobScaleFpdec, ParseError};
type Decimal = OobScaleFpdec<i32>;

assert_eq!(Decimal::try_from((123, 4)).unwrap(), Decimal::try_from_str("123", 4).unwrap());
assert_eq!(Decimal::try_from((123_i8, 4)).unwrap(), Decimal::try_from_str("123", 4).unwrap());
assert_eq!(Decimal::try_from((120000000000_i64, -10)).unwrap(), Decimal::try_from_str("120000000000", -10).unwrap());
assert_eq!(Decimal::try_from((9999999, 4)), Err(ParseError::Overflow));
assert_eq!(Decimal::try_from((123, -4)), Err(ParseError::Precision));
Source§

type Error = ParseError

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

impl<I> TryFrom<(u64, i32)> for OobScaleFpdec<I>
where I: FpdecInner,

Source§

fn try_from(i: (u64, i32)) -> Result<Self, Self::Error>

Convert from integer with scale. Returning error if overflow occurred or lossing precision under scale < 0.

Examples:

use core::str::FromStr;
use primitive_fixed_point_decimal::{OobScaleFpdec, ParseError};
type Decimal = OobScaleFpdec<i32>;

assert_eq!(Decimal::try_from((123, 4)).unwrap(), Decimal::try_from_str("123", 4).unwrap());
assert_eq!(Decimal::try_from((123_i8, 4)).unwrap(), Decimal::try_from_str("123", 4).unwrap());
assert_eq!(Decimal::try_from((120000000000_i64, -10)).unwrap(), Decimal::try_from_str("120000000000", -10).unwrap());
assert_eq!(Decimal::try_from((9999999, 4)), Err(ParseError::Overflow));
assert_eq!(Decimal::try_from((123, -4)), Err(ParseError::Precision));
Source§

type Error = ParseError

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

impl<I> TryFrom<(u8, i32)> for OobScaleFpdec<I>
where I: FpdecInner,

Source§

fn try_from(i: (u8, i32)) -> Result<Self, Self::Error>

Convert from integer with scale. Returning error if overflow occurred or lossing precision under scale < 0.

Examples:

use core::str::FromStr;
use primitive_fixed_point_decimal::{OobScaleFpdec, ParseError};
type Decimal = OobScaleFpdec<i32>;

assert_eq!(Decimal::try_from((123, 4)).unwrap(), Decimal::try_from_str("123", 4).unwrap());
assert_eq!(Decimal::try_from((123_i8, 4)).unwrap(), Decimal::try_from_str("123", 4).unwrap());
assert_eq!(Decimal::try_from((120000000000_i64, -10)).unwrap(), Decimal::try_from_str("120000000000", -10).unwrap());
assert_eq!(Decimal::try_from((9999999, 4)), Err(ParseError::Overflow));
assert_eq!(Decimal::try_from((123, -4)), Err(ParseError::Precision));
Source§

type Error = ParseError

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

impl<I: Copy> Copy for OobScaleFpdec<I>

Source§

impl<I: Eq> Eq for OobScaleFpdec<I>

Source§

impl<I> StructuralPartialEq for OobScaleFpdec<I>

Auto Trait Implementations§

§

impl<I> Freeze for OobScaleFpdec<I>
where I: Freeze,

§

impl<I> RefUnwindSafe for OobScaleFpdec<I>
where I: RefUnwindSafe,

§

impl<I> Send for OobScaleFpdec<I>
where I: Send,

§

impl<I> Sync for OobScaleFpdec<I>
where I: Sync,

§

impl<I> Unpin for OobScaleFpdec<I>
where I: Unpin,

§

impl<I> UnwindSafe for OobScaleFpdec<I>
where I: 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> 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, 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.