pub struct BigUint {
/// The underlying limbs representing the `BigUint` type.
limbs: Vec<u64>,
}
Expand description
The BigUint
type.
Additional Information
The BigUint
type is unsigned. The minimum value is zero and the maximum value is infinity.
Fields
limbs: Vec
The underlying limbs representing the BigUint
type.
Implementations
pub fn new() -> Self
pub fn new() -> Self
Create a new instance of a BigUint
.
Returns
- [BigUint] - A newly instantiated instance of a
BigUint
.
Examples
use sway_libs::bigint::BigUint;
fn foo() {
let new_big_uint = BigUint::new();
assert(new_big_uint.is_zero());
}
pub fn equal_limb_size(self, other: BigUint) -> bool
pub fn equal_limb_size(self, other: BigUint) -> bool
Returns true if two BigUint
numbers have the same number of limbs.
Arguments
other
: [BigUint] - The otherBigUint
to compare with.
Returns
- [bool] -
true
if bothBigUint
values have the same limbs, otherwise false.
Examples
use sway_libs::bigint::BigUint;
fn foo() {
let new_big_uint = BigUint::new();
let u64_big_uint = BigUint::from(u64::max());
let u256_big_uint = BigUint::from(u256::max());
assert(new_big_uint.equal_limb_size(u64_big_uint));
assert(!new_big_uint.equal_limb_size(u256_big_uint));
assert(!u64_big_uint.equal_limb_size(u256_big_uint));
}
pub fn number_of_limbs(self) -> u64
pub fn number_of_limbs(self) -> u64
Returns the number of limbs the BigUint
has.
Returns
- [u64] - The number of limbs the
BigUint
has.
Examples
use sway_libs::bigint::BigUint;
fn foo() {
let new_big_uint = BigUint::new();
let u64_big_uint = BigUint::from(u64::max());
let u256_big_uint = BigUint::from(u256::max());
assert(new_big_uint.number_of_limbs() == 1);
assert(u64_big_uint.number_of_limbs() == 1);
assert(u256_big_uint.number_of_limbs() == 4);
}
pub fn limbs(self) -> Vec<u64>
pub fn limbs(self) -> Vec<u64>
Returns a copy of the BigUint
’s limbs.
Returns
- [Vec] - A clone of the
BigUint
’s limbs.
Examples
use sway_libs::bigint::BigUint;
fn foo() {
let new_big_uint = BigUint::new();
let new_limbs: Vec<u64> = new_big_uint.limbs();
assert(new_limbs.len() == 1);
assert(new_limbs.get(0).unwrap() == 0);
let u64_big_uint = BigUint::from(u64::max());
let u64_limbs: Vec<u64> = u64_big_uint.limbs();
assert(u64_limbs.len() == 1);
assert(u64_limbs.get(0).unwrap() == u64::max());
let u256_big_uint = BigUint::from(u256::max());
let u256_limbs: Vec<u64> = u256_big_uint.limbs();
assert(u256_limbs.len() == 4);
assert(u256_limbs.get(0).unwrap() == u64::max());
assert(u256_limbs.get(1).unwrap() == u64::max());
assert(u256_limbs.get(2).unwrap() == u64::max());
assert(u256_limbs.get(3).unwrap() == u64::max());
}
pub fn get_limb(self, index: u64) -> Option<u64>
pub fn get_limb(self, index: u64) -> Option<u64>
Returns the BigUint
’s limb at a specific index.
Arguments
index
: [u64] - The index at which to fetch the limb.
Returns
Examples
use sway_libs::bigint::BigUint;
fn foo() {
let new_big_uint = BigUint::new();
assert(new_big_uint.get_limb(0).unwrap() == 0);
assert(new_big_uint.get_limb(1).is_none());
let u64_big_uint = BigUint::from(u64::max());
assert(u64_big_uint.get_limb(0).unwrap() == u64::max());
assert(u64_big_uint.get_limb(1).is_none());
let u256_big_uint = BigUint::from(u256::max());
assert(u256_big_uint.get_limb(0).unwrap() == u64::max());
assert(u256_big_uint.get_limb(1).unwrap() == u64::max());
assert(u256_big_uint.get_limb(2).unwrap() == u64::max());
assert(u256_big_uint.get_limb(3).unwrap() == u64::max());
assert(u256_big_uint.get_limb(4).is_none());
}
pub fn zero() -> Self
pub fn zero() -> Self
A zeroed instance of a BigUint
.
Returns
- [BigUint] - A newly created zeroed
BigUint
.
Examples
use sway_libs::bigint::BigUint;
fn foo() {
let zero_big_uint = BigUint::zero();
assert(zero_big_uint.is_zero());
}
pub fn is_zero(self) -> bool
pub fn is_zero(self) -> bool
Returns whether the BigUint
is zero.
Returns
- [bool] -
true
if theBigUint
is zero, otherwisefalse
.
Examples
use sway_libs::bigint::BigUint;
fn foo() {
let zero_big_uint = BigUint::zero();
assert(zero_big_uint.is_zero());
}
Trait Implementations
impl AbiEncode for BigUint
impl AbiEncode for BigUint
pub fn abi_encode(self, buffer: Buffer) -> Buffer
impl AbiDecode for BigUint
impl AbiDecode for BigUint
pub fn abi_decode(refmut buffer: BufferReader) -> Self
impl Eq for BigUint
impl Eq for BigUint
pub fn eq(self, other: Self) -> bool
pub fn neq(self, other: Self) -> bool
pub fn neq(self, other: Self) -> bool
Evaluates if two values of the same type are not equal.
Additional Information
This function is inherited when eq()
is implemented.
Arguments
other
: [Self] - The value of the same type.
Returns
- [bool] -
true
if the two values are not equal, otherwisefalse
.
Examples
struct MyStruct {
val: u64,
}
impl Eq for MyStruct {
fn eq(self, other: Self) -> bool {
self.val == other.val
}
}
fn foo() {
let struct1 = MyStruct { val: 10 };
let struct2 = MyStruct { val: 2 };
let result = struct1 != struct2;
assert(result);
}
impl OrdEq for BigUint
impl OrdEq for BigUint
pub fn ge(self, other: Self) -> bool
pub fn ge(self, other: Self) -> bool
Evaluates if one value of the same type is greater or equal to than another.
Additional Information
This trait requires that the Ord
and Eq
traits are implemented.
Arguments
other
: [Self] - The value of the same type.
Returns
- [bool] -
true
ifself
is greater than or equal toother
, otherwisefalse
.
Examples
struct MyStruct {
val: u64,
}
impl Eq for MyStruct {
fn eq(self, other: Self) -> bool {
self.val == other.val
}
}
impl Ord for MyStruct {
fn gt(self, other: Self) -> bool {
self.val > other.val
}
}
impl OrdEq for MyStruct {}
fn foo() {
let struct1 = MyStruct { val: 10 };
let struct2 = MyStruct { val: 10 };
let result = struct1 >= struct2;
assert(result);
}
pub fn le(self, other: Self) -> bool
pub fn le(self, other: Self) -> bool
Evaluates if one value of the same type is less or equal to than another.
Additional Information
This trait requires that the Ord
and Eq
traits are implemented.
Arguments
other
: [Self] - The value of the same type.
Returns
- [bool] -
true
ifself
is less than or equal toother
, otherwisefalse
.
Examples
struct MyStruct {
val: u64,
}
impl Eq for MyStruct {
fn eq(self, other: Self) -> bool {
self.val == other.val
}
}
impl Ord for MyStruct {
fn lt(self, other: Self) -> bool {
self.val < other.val
}
}
impl OrdEq for MyStruct {}
fn foo() {
let struct1 = MyStruct { val: 10 };
let struct2 = MyStruct { val: 10 };
let result = struct1 <= struct2;
assert(result);
}