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]) -> Uint256
pub const fn new(value: [u8; 32]) -> Uint256
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]) -> Uint256
pub const fn from_le_bytes(data: [u8; 32]) -> Uint256
Sourcepub const fn from_u128(num: u128) -> Uint256
pub const fn from_u128(num: u128) -> Uint256
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) -> Uint256
pub const fn from_uint128(num: Uint128) -> Uint256
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 fn pow(self, exp: u32) -> Uint256
Sourcepub fn multiply_ratio<A, B>(&self, numerator: A, denominator: B) -> Uint256
pub fn multiply_ratio<A, B>(&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, B>(
&self,
numerator: A,
denominator: B,
) -> Result<Uint256, CheckedMultiplyRatioError>
pub fn checked_multiply_ratio<A, B>( &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: Uint256) -> Result<Uint256, OverflowError>
pub fn checked_sub(self, other: Uint256) -> Result<Uint256, OverflowError>
pub fn checked_mul(self, other: Uint256) -> Result<Uint256, OverflowError>
pub fn checked_pow(self, exp: u32) -> Result<Uint256, OverflowError>
pub fn checked_div(self, other: Uint256) -> Result<Uint256, DivideByZeroError>
pub fn checked_div_euclid( self, other: Uint256, ) -> Result<Uint256, DivideByZeroError>
pub fn checked_rem(self, other: Uint256) -> Result<Uint256, DivideByZeroError>
pub fn checked_shr(self, other: u32) -> Result<Uint256, OverflowError>
pub fn checked_shl(self, other: u32) -> Result<Uint256, OverflowError>
pub fn wrapping_add(self, other: Uint256) -> Uint256
pub fn wrapping_sub(self, other: Uint256) -> Uint256
pub fn wrapping_mul(self, other: Uint256) -> Uint256
pub fn wrapping_pow(self, other: u32) -> Uint256
pub fn saturating_add(self, other: Uint256) -> Uint256
pub fn saturating_sub(self, other: Uint256) -> Uint256
pub fn saturating_mul(self, other: Uint256) -> Uint256
pub fn saturating_pow(self, exp: u32) -> Uint256
pub fn abs_diff(self, other: Uint256) -> Uint256
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<Uint256, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
fn deserialize<D>(
deserializer: D,
) -> Result<Uint256, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
Deserialized from an integer string using base 10
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: Uint256)
fn div_assign(&mut self, rhs: Uint256)
/=
operation. Read moreSource§impl FadromaDeserialize for Uint256
impl FadromaDeserialize for Uint256
Source§fn from_bytes<'a>(de: &mut Deserializer<'a>) -> Result<Self>
fn from_bytes<'a>(de: &mut Deserializer<'a>) -> Result<Self>
Deserializer
.Source§impl FadromaSerialize for Uint256
impl FadromaSerialize for Uint256
Source§fn size_hint(&self) -> usize
fn size_hint(&self) -> usize
Source§fn to_bytes(&self, ser: &mut Serializer) -> Result<()>
fn to_bytes(&self, ser: &mut Serializer) -> Result<()>
Serializer
.Source§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(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 Mul<Decimal256> for Uint256
Both du and ud with d: Decimal256 and u: Uint256 returns an Uint256. There is no
specific reason for this decision other than the initial use cases we have. If you
need a Decimal256 result for the same calculation, use Decimal256(du) or Decimal256(ud).
impl Mul<Decimal256> for Uint256
Both du and ud with d: Decimal256 and u: Uint256 returns an Uint256. There is no specific reason for this decision other than the initial use cases we have. If you need a Decimal256 result for the same calculation, use Decimal256(du) or Decimal256(ud).
Source§impl Mul<Uint256> for Decimal256
impl Mul<Uint256> for Decimal256
Source§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: Uint256)
fn mul_assign(&mut self, rhs: Uint256)
*=
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 as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
Serializes as an integer string using base 10
Source§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 more