Struct sway_libs::signed_integers::i32::I32
pub struct I32 {
/// The underlying u32 type that represent a I32.
underlying: u32,
}
Expand description
The 32-bit signed integer type.
Additional Information
Represented as an underlying u32 value.
Actual value is underlying value minus 2 ^ 31
Max value is 2 ^ 31 - 1, min value is - 2 ^ 31
Fields
underlying: u32
The underlying u32 type that represent a I32.
Implementations
pub fn indent() -> u32
pub fn indent() -> u32
The underlying value that corresponds to zero value.
Returns
- [u32] - The unsigned integer value representing a zero value.
Examples
use sway_libs::signed_integers::i32::I32;
fn foo() {
let zero = I32::indent();
assert(zero == 2147483648u32);
}
pub fn bits() -> u64
pub fn bits() -> u64
The size of this type in bits.
Returns
[u64] - The defined size of the I32
type.
Examples
``sway
use sway_libs::signed_integers::i32::I32;
fn foo() {
let bits = I32::bits();
assert(bits == 32);
}
pub fn from_uint(underlying: u32) -> Self
pub fn from_uint(underlying: u32) -> Self
Helper function to get a signed number from with an underlying.
Arguments
underlying
: [u32] - The unsigned number to become the underlying value for theI32
.
Returns
- [I32] - The newly created
I32
struct.
Examples
use sway_libs::signed_integers::i32::I32;
fn foo() {
let underlying = 1u32;
let i32 = I32::from_uint(underlying);
assert(i32.underlying() == underlying);
}
pub fn max() -> Self
pub fn max() -> Self
The largest value that can be represented by this integer type.
Returns
- [I32] - The newly created
I32
struct.
Examples
use sway_libs::signed_integers::i32::I32;
fn foo() {
let i32 = I32::max();
assert(i32.underlying() == u32::max());
}
pub fn min() -> Self
pub fn min() -> Self
The smallest value that can be represented by this integer type.
Returns
- [I32] - The newly created
I32
type.
Examples
use sway_libs::signed_integers::i32::I32;
fn foo() {
let i32 = I32::min();
assert(i32.underlying() == u32::min());
}
pub fn neg_try_from(value: u32) -> Option<Self>
pub fn neg_try_from(value: u32) -> Option<Self>
Helper function to get a negative value of an unsigned numbers.
Arguments
value
: [u32] - The unsigned number to negate.
Returns
- [Option] - The newly created
I32
struct.
Examples
use sway_libs::signed_integers::i32::I32;
fn foo() {
let underlying = 1u32;
let i32 = I32::neg_try_from(underlying).unwrap();
assert(i32.underlying() == 2147483647u32)
}
pub fn new() -> Self
pub fn new() -> Self
Initializes a new, zeroed I32.
Additional Information
The zero value of I32 is 2147483648u32.
Returns
- [I32] - The newly created
I32
struct.
Examples
use sway_libs::signed_integers::i32::I32;
fn foo() {
let i32 = I32::new();
assert(i32.underlying() == 2147483648u32);
}
pub fn zero() -> Self
pub fn zero() -> Self
The zero value I32
.
Returns
- [I32] - The newly created
I32
type.
Examples
use sway_libs::signed_integers::i32::I32;
fn foo() {
let i32 = I32::zero();
assert(i32.underlying() == 2147483648u32);
}
pub fn is_zero(self) -> bool
pub fn is_zero(self) -> bool
Returns whether a I32
is set to zero.
Returns
- [bool] -> True if the
I32
is zero, otherwise false.
Examples
use sway_libs::signed_integers::i32::I32;
fn foo() {
let i32 = I32::zero();
assert(i32.is_zero());
}
pub fn underlying(self) -> u32
pub fn underlying(self) -> u32
Returns the underlying u32
representing the I32
.
Returns
- [u32] - The
u32
representing theI32
.
Examples
use sway_libs::signed_integers::i32::I32;
fn foo() {
let i32 = I32::zero();
assert(i32.underlying() == 2147483648u32);
}
Trait Implementations
impl AbiEncode for I32
impl AbiEncode for I32
pub fn abi_encode(self, buffer: Buffer) -> Buffer
impl AbiDecode for I32
impl AbiDecode for I32
pub fn abi_decode(refmut buffer: BufferReader) -> Self
impl Eq for I32
impl Eq for I32
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 I32
impl OrdEq for I32
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);
}
impl Subtract for I32
impl Subtract for I32
pub fn subtract(self, other: Self) -> Self
pub fn subtract(self, other: Self) -> Self
Subtract a I32 from a I32. Panics of overflow.
impl Multiply for I32
impl Multiply for I32
pub fn multiply(self, other: Self) -> Self
pub fn multiply(self, other: Self) -> Self
Multiply a I32 with a I32. Panics of overflow.
impl Divide for I32
impl Divide for I32
pub fn divide(self, divisor: Self) -> Self
pub fn divide(self, divisor: Self) -> Self
Divide a I32 by a I32. Panics if divisor is zero.