pub struct Uint256(/* private fields */);
Expand description
An implementation of u256 that is using strings for JSON encoding/decoding, such that the full u256 range can be used for clients that convert JSON numbers to floats, like JavaScript and jq.
§Examples
Use from
to create instances out of primitive uint types or new
to provide big
endian bytes:
let a = Uint256::from(258u128);
let b = Uint256::new([
0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8,
0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8,
0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8,
0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 1u8, 2u8,
]);
assert_eq!(a, b);
Implementations§
Source§impl Uint256
impl Uint256
pub const MAX: Uint256
pub const MIN: Uint256
Sourcepub const fn new(value: [u8; 32]) -> Self
pub const fn new(value: [u8; 32]) -> Self
Creates a Uint256(value) from a big endian representation. It’s just an alias for
Uint256::from_be_bytes
.
This method is less flexible than from
but can be called in a const context.
pub const fn from_be_bytes(data: [u8; 32]) -> Self
pub const fn from_le_bytes(data: [u8; 32]) -> Self
Sourcepub const fn from_u128(num: u128) -> Self
pub const fn from_u128(num: u128) -> Self
A conversion from u128
that, unlike the one provided by the From
trait,
can be used in a const
context.
Sourcepub const fn from_uint128(num: Uint128) -> Self
pub const fn from_uint128(num: Uint128) -> Self
A conversion from Uint128
that, unlike the one provided by the From
trait,
can be used in a const
context.
Sourcepub const fn to_be_bytes(self) -> [u8; 32]
pub const fn to_be_bytes(self) -> [u8; 32]
Returns a copy of the number as big endian bytes.
Sourcepub const fn to_le_bytes(self) -> [u8; 32]
pub const fn to_le_bytes(self) -> [u8; 32]
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<Uint256>, B: Into<Uint256>>(
&self,
numerator: A,
denominator: B,
) -> Uint256
pub fn multiply_ratio<A: Into<Uint256>, B: Into<Uint256>>( &self, numerator: A, denominator: B, ) -> Uint256
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<Uint256>, B: Into<Uint256>>(
&self,
numerator: A,
denominator: B,
) -> Result<Uint256, CheckedMultiplyRatioError>
pub fn checked_multiply_ratio<A: Into<Uint256>, B: Into<Uint256>>( &self, numerator: A, denominator: B, ) -> Result<Uint256, 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 Uint256::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 Uint256::sub
but const.
pub const fn abs_diff(self, other: Self) -> Self
Source§impl Uint256
impl Uint256
Sourcepub fn checked_mul_floor<F: Fraction<T>, T: Into<Uint256>>(
self,
rhs: F,
) -> Result<Self, CheckedMultiplyFractionError>
pub fn checked_mul_floor<F: Fraction<T>, T: Into<Uint256>>( 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<Uint256>>(self, rhs: F) -> Self
pub fn mul_floor<F: Fraction<T>, T: Into<Uint256>>(self, rhs: F) -> Self
Same operation as checked_mul_floor
except unwrapped
Sourcepub fn checked_mul_ceil<F: Fraction<T>, T: Into<Uint256>>(
self,
rhs: F,
) -> Result<Self, CheckedMultiplyFractionError>
pub fn checked_mul_ceil<F: Fraction<T>, T: Into<Uint256>>( 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<Uint256>>(self, rhs: F) -> Self
pub fn mul_ceil<F: Fraction<T>, T: Into<Uint256>>(self, rhs: F) -> Self
Same operation as checked_mul_ceil
except unwrapped
Sourcepub fn checked_div_floor<F: Fraction<T>, T: Into<Uint256>>(
self,
rhs: F,
) -> Result<Self, CheckedMultiplyFractionError>where
Self: Sized,
pub fn checked_div_floor<F: Fraction<T>, T: Into<Uint256>>(
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<Uint256>>(self, rhs: F) -> Selfwhere
Self: Sized,
pub fn div_floor<F: Fraction<T>, T: Into<Uint256>>(self, rhs: F) -> Selfwhere
Self: Sized,
Same operation as checked_div_floor
except unwrapped
Sourcepub fn checked_div_ceil<F: Fraction<T>, T: Into<Uint256>>(
self,
rhs: F,
) -> Result<Self, CheckedMultiplyFractionError>where
Self: Sized,
pub fn checked_div_ceil<F: Fraction<T>, T: Into<Uint256>>(
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 Uint256> for Uint256
impl<'a> AddAssign<&'a Uint256> for Uint256
Source§fn add_assign(&mut self, rhs: &'a Uint256)
fn add_assign(&mut self, rhs: &'a Uint256)
+=
operation. Read moreSource§impl AddAssign for Uint256
impl AddAssign for Uint256
Source§fn add_assign(&mut self, rhs: Uint256)
fn add_assign(&mut self, rhs: Uint256)
+=
operation. Read moreSource§impl<'de> Deserialize<'de> for Uint256
impl<'de> Deserialize<'de> for Uint256
Source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
Deserializes from an integer string using base 10.
See the Serialize
documentation for a few more words
on the encoding of the UintXXX
/IntYYY
family.
Source§impl Div<Uint256> for Decimal256
impl Div<Uint256> for Decimal256
Source§impl<'a> DivAssign<&'a Uint256> for Uint256
impl<'a> DivAssign<&'a Uint256> for Uint256
Source§fn div_assign(&mut self, rhs: &'a Uint256)
fn div_assign(&mut self, rhs: &'a Uint256)
/=
operation. Read moreSource§impl DivAssign<Uint256> for Decimal256
impl DivAssign<Uint256> for Decimal256
Source§fn div_assign(&mut self, rhs: Uint256)
fn div_assign(&mut self, rhs: Uint256)
/=
operation. Read moreSource§impl DivAssign for Uint256
impl DivAssign for Uint256
Source§fn div_assign(&mut self, rhs: Self)
fn div_assign(&mut self, rhs: Self)
/=
operation. Read moreSource§impl Fraction<Uint256> for Decimal256
impl Fraction<Uint256> for Decimal256
Source§impl JsonSchema for Uint256
impl JsonSchema for Uint256
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(generator: &mut SchemaGenerator) -> Schema
fn json_schema(generator: &mut SchemaGenerator) -> Schema
Source§fn is_referenceable() -> bool
fn is_referenceable() -> bool
$ref
keyword. Read moreSource§impl MulAssign<&Uint256> for Uint256
impl MulAssign<&Uint256> for Uint256
Source§fn mul_assign(&mut self, other: &Uint256)
fn mul_assign(&mut self, other: &Uint256)
*=
operation. Read moreSource§impl MulAssign for Uint256
impl MulAssign for Uint256
Source§fn mul_assign(&mut self, rhs: Self)
fn mul_assign(&mut self, rhs: Self)
*=
operation. Read moreSource§impl Ord for Uint256
impl Ord for Uint256
Source§impl PartialOrd for Uint256
impl PartialOrd for Uint256
Source§impl RemAssign<&Uint256> for Uint256
impl RemAssign<&Uint256> for Uint256
Source§fn rem_assign(&mut self, other: &Uint256)
fn rem_assign(&mut self, other: &Uint256)
%=
operation. Read moreSource§impl RemAssign for Uint256
impl RemAssign for Uint256
Source§fn rem_assign(&mut self, rhs: Uint256)
fn rem_assign(&mut self, rhs: Uint256)
%=
operation. Read moreSource§impl Serialize for Uint256
impl Serialize for Uint256
Source§fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>where
S: Serializer,
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>where
S: Serializer,
Serializes as an integer string using base 10.
We consistently serialize all UintXXX
and IntYYY
types as strings in JSON
to ensure the best possible compatibility with clients. E.g. JavaScript and jq
only support up to ~53bit numbers without losing precision, making it hard to use
serialized u64
s on other systems than Rust or Go. Uint64
/Int64
ensure the full
64 bit range is supported. For larger integers, the use of strings is pretty much the
only reasonable way to store them in JSON.
For binary encodings (notably MessagePack) strings are used too. The reason is that
in MessagePack integers are limited to 64 bit and we strive for consistent encoding
within the UintXXX
/IntYYY
family. Also for small to mid sized values, decimal strings
are often more compact than a fixed-length binary encoding.
§Examples
Serialize to JSON:
let value = Uint64::new(17);
let serialized = to_json_vec(&value).unwrap();
assert_eq!(serialized, b"\"17\"");
Serialize to MessagePack:
let value = Uint64::new(17);
let serialized = to_msgpack_vec(&value).unwrap();
assert_eq!(serialized, [0b10100000 ^ 2, b'1', b'7']); // string of lengths 2 with value "17"
Source§impl<'a> ShlAssign<&'a u32> for Uint256
impl<'a> ShlAssign<&'a u32> for Uint256
Source§fn shl_assign(&mut self, rhs: &'a u32)
fn shl_assign(&mut self, rhs: &'a u32)
<<=
operation. Read moreSource§impl ShlAssign<u32> for Uint256
impl ShlAssign<u32> for Uint256
Source§fn shl_assign(&mut self, rhs: u32)
fn shl_assign(&mut self, rhs: u32)
<<=
operation. Read moreSource§impl<'a> ShrAssign<&'a u32> for Uint256
impl<'a> ShrAssign<&'a u32> for Uint256
Source§fn shr_assign(&mut self, rhs: &'a u32)
fn shr_assign(&mut self, rhs: &'a u32)
>>=
operation. Read moreSource§impl ShrAssign<u32> for Uint256
impl ShrAssign<u32> for Uint256
Source§fn shr_assign(&mut self, rhs: u32)
fn shr_assign(&mut self, rhs: u32)
>>=
operation. Read moreSource§impl SubAssign<&Uint256> for Uint256
impl SubAssign<&Uint256> for Uint256
Source§fn sub_assign(&mut self, other: &Uint256)
fn sub_assign(&mut self, other: &Uint256)
-=
operation. Read moreSource§impl SubAssign for Uint256
impl SubAssign for Uint256
Source§fn sub_assign(&mut self, rhs: Uint256)
fn sub_assign(&mut self, rhs: Uint256)
-=
operation. Read moreimpl Copy for Uint256
impl Eq for Uint256
impl StructuralPartialEq for Uint256
Auto Trait Implementations§
impl Freeze for Uint256
impl RefUnwindSafe for Uint256
impl Send for Uint256
impl Sync for Uint256
impl Unpin for Uint256
impl UnwindSafe for Uint256
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§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