Struct cosmwasm_std::Uint128
source · pub struct Uint128(/* private fields */);
Expand description
A thin wrapper around u128 that is using strings for JSON encoding/decoding, such that the full u128 range can be used for clients that convert JSON numbers to floats, like JavaScript and jq.
§Examples
Use from
to create instances of this and u128
to get the value out:
let a = Uint128::from(123u128);
assert_eq!(a.u128(), 123);
let b = Uint128::from(42u64);
assert_eq!(b.u128(), 42);
let c = Uint128::from(70u32);
assert_eq!(c.u128(), 70);
Implementations§
source§impl Uint128
impl Uint128
pub const MAX: Self = _
pub const MIN: Self = _
sourcepub const fn new(value: u128) -> Self
pub const fn new(value: u128) -> Self
Creates a Uint128(value).
This method is less flexible than from
but can be called in a const context.
sourcepub const fn to_be_bytes(self) -> [u8; 16]
pub const fn to_be_bytes(self) -> [u8; 16]
Returns a copy of the number as big endian bytes.
sourcepub const fn to_le_bytes(self) -> [u8; 16]
pub const fn to_le_bytes(self) -> [u8; 16]
Returns a copy of the number as little endian bytes.
pub const fn is_zero(&self) -> bool
pub const fn pow(self, exp: u32) -> Self
sourcepub fn ilog2(self) -> u32
pub fn ilog2(self) -> u32
Returns the base 2 logarithm of the number, rounded down.
§Panics
This function will panic if self
is zero.
sourcepub fn multiply_ratio<A: Into<u128>, B: Into<u128>>(
&self,
numerator: A,
denominator: B,
) -> Uint128
pub fn multiply_ratio<A: Into<u128>, B: Into<u128>>( &self, numerator: A, denominator: B, ) -> Uint128
Returns self * numerator / denominator
.
Due to the nature of the integer division involved, the result is always floored. E.g. 5 * 99/100 = 4.
sourcepub fn checked_multiply_ratio<A: Into<u128>, B: Into<u128>>(
&self,
numerator: A,
denominator: B,
) -> Result<Uint128, CheckedMultiplyRatioError>
pub fn checked_multiply_ratio<A: Into<u128>, B: Into<u128>>( &self, numerator: A, denominator: B, ) -> Result<Uint128, CheckedMultiplyRatioError>
Returns self * numerator / denominator
.
Due to the nature of the integer division involved, the result is always floored. E.g. 5 * 99/100 = 4.
pub fn checked_add(self, other: Self) -> Result<Self, OverflowError>
pub fn checked_sub(self, other: Self) -> Result<Self, OverflowError>
pub fn checked_mul(self, other: Self) -> Result<Self, OverflowError>
pub fn checked_pow(self, exp: u32) -> Result<Self, OverflowError>
pub fn checked_div(self, other: Self) -> Result<Self, DivideByZeroError>
pub fn checked_div_euclid(self, other: Self) -> Result<Self, DivideByZeroError>
pub fn checked_rem(self, other: Self) -> Result<Self, DivideByZeroError>
pub fn checked_shr(self, other: u32) -> Result<Self, OverflowError>
pub fn checked_shl(self, other: u32) -> Result<Self, OverflowError>
pub fn wrapping_add(self, other: Self) -> Self
pub fn wrapping_sub(self, other: Self) -> Self
pub fn wrapping_mul(self, other: Self) -> Self
pub fn wrapping_pow(self, other: u32) -> Self
pub fn saturating_add(self, other: Self) -> Self
pub fn saturating_sub(self, other: Self) -> Self
pub fn saturating_mul(self, other: Self) -> Self
pub fn saturating_pow(self, exp: u32) -> Self
sourcepub const fn strict_add(self, rhs: Self) -> Self
pub const fn strict_add(self, rhs: Self) -> Self
Strict integer addition. Computes self + rhs
, panicking if overflow occurred.
This is the same as Uint128::add
but const.
sourcepub const fn strict_sub(self, other: Self) -> Self
pub const fn strict_sub(self, other: Self) -> Self
Strict integer subtraction. Computes self - rhs
, panicking if overflow occurred.
This is the same as Uint128::sub
but const.
pub const fn abs_diff(self, other: Self) -> Self
source§impl Uint128
impl Uint128
sourcepub fn checked_mul_floor<F: Fraction<T>, T: Into<Uint128>>(
self,
rhs: F,
) -> Result<Self, CheckedMultiplyFractionError>
pub fn checked_mul_floor<F: Fraction<T>, T: Into<Uint128>>( self, rhs: F, ) -> Result<Self, CheckedMultiplyFractionError>
Multiply self
with a struct implementing Fraction
(e.g. crate::Decimal
).
Result is rounded down.
§Examples
use cosmwasm_std::Uint128;
let fraction = (8u128, 21u128);
let res = Uint128::new(123456).checked_mul_floor(fraction).unwrap();
assert_eq!(Uint128::new(47030), res); // 47030.8571 rounds down
sourcepub fn mul_floor<F: Fraction<T>, T: Into<Uint128>>(self, rhs: F) -> Self
pub fn mul_floor<F: Fraction<T>, T: Into<Uint128>>(self, rhs: F) -> Self
Same operation as checked_mul_floor
except unwrapped
sourcepub fn checked_mul_ceil<F: Fraction<T>, T: Into<Uint128>>(
self,
rhs: F,
) -> Result<Self, CheckedMultiplyFractionError>
pub fn checked_mul_ceil<F: Fraction<T>, T: Into<Uint128>>( self, rhs: F, ) -> Result<Self, CheckedMultiplyFractionError>
Multiply self
with a struct implementing Fraction
(e.g. crate::Decimal
).
Result is rounded up.
§Examples
use cosmwasm_std::Uint128;
let fraction = (8u128, 21u128);
let res = Uint128::new(123456).checked_mul_ceil(fraction).unwrap();
assert_eq!(Uint128::new(47031), res); // 47030.8571 rounds up
sourcepub fn mul_ceil<F: Fraction<T>, T: Into<Uint128>>(self, rhs: F) -> Self
pub fn mul_ceil<F: Fraction<T>, T: Into<Uint128>>(self, rhs: F) -> Self
Same operation as checked_mul_ceil
except unwrapped
sourcepub fn checked_div_floor<F: Fraction<T>, T: Into<Uint128>>(
self,
rhs: F,
) -> Result<Self, CheckedMultiplyFractionError>where
Self: Sized,
pub fn checked_div_floor<F: Fraction<T>, T: Into<Uint128>>(
self,
rhs: F,
) -> Result<Self, CheckedMultiplyFractionError>where
Self: Sized,
Divide self
with a struct implementing Fraction
(e.g. crate::Decimal
).
Result is rounded down.
§Examples
use cosmwasm_std::Uint128;
let fraction = (4u128, 5u128);
let res = Uint128::new(789).checked_div_floor(fraction).unwrap();
assert_eq!(Uint128::new(986), res); // 986.25 rounds down
sourcepub fn div_floor<F: Fraction<T>, T: Into<Uint128>>(self, rhs: F) -> Selfwhere
Self: Sized,
pub fn div_floor<F: Fraction<T>, T: Into<Uint128>>(self, rhs: F) -> Selfwhere
Self: Sized,
Same operation as checked_div_floor
except unwrapped
sourcepub fn checked_div_ceil<F: Fraction<T>, T: Into<Uint128>>(
self,
rhs: F,
) -> Result<Self, CheckedMultiplyFractionError>where
Self: Sized,
pub fn checked_div_ceil<F: Fraction<T>, T: Into<Uint128>>(
self,
rhs: F,
) -> Result<Self, CheckedMultiplyFractionError>where
Self: Sized,
Divide self
with a struct implementing Fraction
(e.g. crate::Decimal
).
Result is rounded up.
§Examples
use cosmwasm_std::Uint128;
let fraction = (4u128, 5u128);
let res = Uint128::new(789).checked_div_ceil(fraction).unwrap();
assert_eq!(Uint128::new(987), res); // 986.25 rounds up
Trait Implementations§
source§impl<'a> AddAssign<&'a Uint128> for Uint128
impl<'a> AddAssign<&'a Uint128> for Uint128
source§fn add_assign(&mut self, rhs: &'a Uint128)
fn add_assign(&mut self, rhs: &'a Uint128)
+=
operation. Read moresource§impl AddAssign for Uint128
impl AddAssign for Uint128
source§fn add_assign(&mut self, rhs: Uint128)
fn add_assign(&mut self, rhs: Uint128)
+=
operation. Read moresource§impl<'de> Deserialize<'de> for Uint128
impl<'de> Deserialize<'de> for Uint128
source§fn deserialize<D>(deserializer: D) -> Result<Uint128, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Uint128, D::Error>where
D: Deserializer<'de>,
Deserialized from an integer string using base 10
source§impl<'a> DivAssign<&'a Uint128> for Uint128
impl<'a> DivAssign<&'a Uint128> for Uint128
source§fn div_assign(&mut self, rhs: &'a Uint128)
fn div_assign(&mut self, rhs: &'a Uint128)
/=
operation. Read moresource§impl DivAssign<Uint128> for Decimal
impl DivAssign<Uint128> for Decimal
source§fn div_assign(&mut self, rhs: Uint128)
fn div_assign(&mut self, rhs: Uint128)
/=
operation. Read moresource§impl DivAssign for Uint128
impl DivAssign for Uint128
source§fn div_assign(&mut self, rhs: Self)
fn div_assign(&mut self, rhs: Self)
/=
operation. Read moresource§impl JsonSchema for Uint128
impl JsonSchema for Uint128
source§fn schema_name() -> String
fn schema_name() -> String
source§fn schema_id() -> Cow<'static, str>
fn schema_id() -> Cow<'static, str>
source§fn json_schema(gen: &mut SchemaGenerator) -> Schema
fn json_schema(gen: &mut SchemaGenerator) -> Schema
source§fn is_referenceable() -> bool
fn is_referenceable() -> bool
$ref
keyword. Read moresource§impl MulAssign<&Uint128> for Uint128
impl MulAssign<&Uint128> for Uint128
source§fn mul_assign(&mut self, other: &Uint128)
fn mul_assign(&mut self, other: &Uint128)
*=
operation. Read moresource§impl MulAssign for Uint128
impl MulAssign for Uint128
source§fn mul_assign(&mut self, rhs: Self)
fn mul_assign(&mut self, rhs: Self)
*=
operation. Read moresource§impl Ord for Uint128
impl Ord for Uint128
source§impl PartialOrd for Uint128
impl PartialOrd for Uint128
source§impl RemAssign<&Uint128> for Uint128
impl RemAssign<&Uint128> for Uint128
source§fn rem_assign(&mut self, other: &Uint128)
fn rem_assign(&mut self, other: &Uint128)
%=
operation. Read moresource§impl RemAssign for Uint128
impl RemAssign for Uint128
source§fn rem_assign(&mut self, rhs: Uint128)
fn rem_assign(&mut self, rhs: Uint128)
%=
operation. Read moresource§impl<'a> ShlAssign<&'a u32> for Uint128
impl<'a> ShlAssign<&'a u32> for Uint128
source§fn shl_assign(&mut self, rhs: &'a u32)
fn shl_assign(&mut self, rhs: &'a u32)
<<=
operation. Read moresource§impl ShlAssign<u32> for Uint128
impl ShlAssign<u32> for Uint128
source§fn shl_assign(&mut self, rhs: u32)
fn shl_assign(&mut self, rhs: u32)
<<=
operation. Read moresource§impl<'a> ShrAssign<&'a u32> for Uint128
impl<'a> ShrAssign<&'a u32> for Uint128
source§fn shr_assign(&mut self, rhs: &'a u32)
fn shr_assign(&mut self, rhs: &'a u32)
>>=
operation. Read moresource§impl ShrAssign<u32> for Uint128
impl ShrAssign<u32> for Uint128
source§fn shr_assign(&mut self, rhs: u32)
fn shr_assign(&mut self, rhs: u32)
>>=
operation. Read moresource§impl SubAssign<&Uint128> for Uint128
impl SubAssign<&Uint128> for Uint128
source§fn sub_assign(&mut self, other: &Uint128)
fn sub_assign(&mut self, other: &Uint128)
-=
operation. Read moresource§impl SubAssign for Uint128
impl SubAssign for Uint128
source§fn sub_assign(&mut self, rhs: Uint128)
fn sub_assign(&mut self, rhs: Uint128)
-=
operation. Read moreimpl Copy for Uint128
impl Eq for Uint128
impl StructuralPartialEq for Uint128
Auto Trait Implementations§
impl Freeze for Uint128
impl RefUnwindSafe for Uint128
impl Send for Uint128
impl Sync for Uint128
impl Unpin for Uint128
impl UnwindSafe for Uint128
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more