u32
Expand description
Implementations
pub fn min() -> Self
pub fn min() -> Self
The smallest value that can be represented by this integer type.
Returns
- [u32] - The smallest
u32
value.
Examples
fn foo() {
let val == u32::min();
assert(val == 0u32);
}
pub fn max() -> Self
pub fn max() -> Self
The largest value that can be represented by this integer type,
232 - 1.
Returns
- [u32] - The largest
u32
value.
Examples
fn foo() {
let val == u32:max();
assert(val == 4294967295u32);
}
pub fn bits() -> u64
pub fn bits() -> u64
The size of this integer type in bits.
Returns
- [u32] - The number of bits for a
u32
.
Examples
fn foo() {
let bits = u32::bits();
assert(bits == 32);
}
pub fn zero() -> Self
pub fn zero() -> Self
Returns the zero value for the u32
type.
Returns
- [u32] -> The zero value for the
u32
type.
Examples
fn foo() {
let zero_u32 = u32::zero();
assert(zero_u32 == 0u32);
}
pub fn is_zero(self) -> bool
pub fn is_zero(self) -> bool
Returns whether a u32
is set to zero.
Returns
- [bool] -> True if the
u32
is zero, otherwise false.
Examples
fn foo() {
let zero_u32 = u32::zero();
assert(zero_u32.is_zero());
}
pub fn as_u64(self) -> u64
pub fn as_u64(self) -> u64
Extends a u32
to a u64
.
Returns
- [u64] - The converted
u32
value.
Examples
fn foo() {
let val = 10u32;
let result = val.as_u64();
assert(result == 10);
}
pub fn as_u256(self) -> u256
pub fn as_u256(self) -> u256
Extends a u32
to a u256
.
Returns
- [u256] - The converted
u32
value.
Examples
fn foo() {
let val = 2u32;
let result = val.as_u256();
assert(result == 0x0000000000000000000000000000000000000000000000000000000000000002u256);
}
pub fn try_as_u8(self) -> Option<u8>
pub fn try_as_u8(self) -> Option<u8>
Attempts to convert the u32 value into a u8 value.
Additional Information
The max value a u8 can represent is 255.
Returns
[Option] - Some(u8)
if the u32 is less than or equal to the max u8 value. Else None
.
Examples
fn foo() {
let val = 255_u32.try_as_u8();
assert(val == Some(255_u8));
// Conversion fails as value is above the max a u8 can represent.
let val2 = 256_u32.try_as_u8();
assert(val == None);
}
pub fn try_as_u16(self) -> Option<u16>
pub fn try_as_u16(self) -> Option<u16>
Attempts to convert the u32 value into a u16 value.
Additional Information
The max value a u16 can represent is 65_535.
Returns
[Option] - Some(u16)
if the u32 is less than or equal to the max u16 value. Else None
.
Examples
fn foo() {
let val = 65_535_u32.try_as_u16();
assert(val == Some(65_535_u16));
// Conversion fails as value is above the max a u16 can represent.
let val2 = 65_536_u32.try_as_u16();
assert(val == None);
}
pub fn to_le_bytes(self) -> [u8; 4]
pub fn to_le_bytes(self) -> [u8; 4]
Converts the u32
to a sequence of little-endian bytes.
Returns
- [[u8; 4]] - An array of 4
u8
bytes that compose theu32
.
Examples
fn foo() {
let x: u32 = 67305985;
let result = x.to_le_bytes();
assert(result[0] == 1_u8);
assert(result[1] == 2_u8);
assert(result[2] == 3_u8);
assert(result[3] == 4_u8);
}
pub fn from_le_bytes(bytes: [u8; 4]) -> Self
pub fn from_le_bytes(bytes: [u8; 4]) -> Self
Converts a sequence of little-endian bytes to a u32
.
Arguments
bytes
: [[u8; 4]] - A sequence of 4u8
bytes that represent au32
.
Returns
- [u32] - The resulting
u32
value.
Examples
fn foo() {
let bytes = [1_u8, 2_u8, 3_u8, 4_u8];
let result = u32::from_le_bytes(bytes);
assert(result == 67305985_u32);
}
pub fn to_be_bytes(self) -> [u8; 4]
pub fn to_be_bytes(self) -> [u8; 4]
Converts the u32
to a sequence of big-endian bytes.
Returns
- [[u8; 4]] - An array of 4
u8
bytes that compose theu32
.
Examples
fn foo() {
let x: u32 = 67305985;
let result = x.to_be_bytes();
assert(result[0] == 4_u8);
assert(result[1] == 3_u8);
assert(result[2] == 2_u8);
assert(result[3] == 1_u8);
}
pub fn from_be_bytes(bytes: [u8; 4]) -> Self
pub fn from_be_bytes(bytes: [u8; 4]) -> Self
Converts a sequence of big-endian bytes to a u32
.
Arguments
bytes
: [[u8; 4]] - A sequence of 4u8
bytes that represent au32
.
Returns
- [u32] - The resulting
u32
value.
Examples
fn foo() {
let bytes = [4_u8, 3_u8, 2_u8, 1_u8];
let result = u32::from_be_bytes(bytes);
assert(result == 67305985_u32);
}
pub fn to_le_bytes(self) -> Bytes
pub fn to_le_bytes(self) -> Bytes
Converts the u32
to a sequence of little-endian bytes.
Returns
- [Bytes] - The 4 bytes that compose the
u32
.
Examples
use std::bytes_conversions::u32::*;
fn foo() {
let x: u32 = 67305985;
let result = x.to_le_bytes();
assert(result.get(0).unwrap() == 1_u8);
assert(result.get(1).unwrap() == 2_u8);
assert(result.get(2).unwrap() == 3_u8);
assert(result.get(3).unwrap() == 4_u8);
}
pub fn from_le_bytes(bytes: Bytes) -> Self
pub fn from_le_bytes(bytes: Bytes) -> Self
Converts a sequence of little-endian bytes to a u32
.
Arguments
bytes
: [Bytes] - The 4 bytes that compose theu32
.
Returns
- [u32] - The resulting
u32
value.
Examples
use std::{bytes::Bytes, bytes_conversions::u32::*};
fn foo() {
let mut bytes = Bytes::new();
bytes.push(1_u8);
bytes.push(2_u8);
bytes.push(3_u8);
bytes.push(4_u8);
let result = u32::from_le_bytes(bytes);
assert(result == 67305985_u32);
}
pub fn to_be_bytes(self) -> Bytes
pub fn to_be_bytes(self) -> Bytes
Converts the u32
to a sequence of big-endian bytes.
Returns
- [Bytes] - The 4 bytes that compose the
u32
.
Examples
use std::bytes_conversions::u32::*;
fn foo() {
let x: u32 = 67305985;
let result = x.to_be_bytes();
assert(result.get(0).unwrap() == 4_u8);
assert(result.get(1).unwrap() == 3_u8);
assert(result.get(2).unwrap() == 2_u8);
assert(result.get(3).unwrap() == 1_u8);
}
pub fn from_be_bytes(bytes: Bytes) -> Self
pub fn from_be_bytes(bytes: Bytes) -> Self
Converts a sequence of big-endian bytes to a u32
.
Arguments
bytes
: [Bytes] - The 4 bytes that compose theu32
.
Returns
- [u32] - The resulting
u32
value.
Examples
use std::{bytes::Bytes, bytes_conversions::u32::*};
fn foo() {
let mut bytes = Bytes::new();
bytes.push(4_u8);
bytes.push(3_u8);
bytes.push(2_u8);
bytes.push(1_u8);
let result = u32::from_be_bytes(bytes);
assert(result == 67305985_u32);
}
Trait Implementations
impl PartialEq for u32
impl PartialEq for u32
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 PartialEq 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 Eq for u32
impl BitwiseAnd for u32
impl BitwiseAnd for u32
pub fn binary_and(self, other: Self) -> Self
impl BitwiseXor for u32
impl BitwiseXor for u32
pub fn binary_xor(self, other: Self) -> Self
impl OrdEq for u32
impl OrdEq for u32
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 AbiEncode for u32
impl AbiEncode for u32
pub fn abi_encode(self, buffer: Buffer) -> Buffer
impl AbiDecode for u32
impl AbiDecode for u32
pub fn abi_decode(refmut buffer: BufferReader) -> u32
impl From<u8> for u32
impl From<u8> for u32
pub fn from(u: u8) -> Self
pub fn from(u: u8) -> Self
Casts a u8
to a u32
.
Returns
- [u32] - The
u32
representation of theu8
value.
Examples
fn foo() {
let u32_value = u32::from(0u8);
}
impl From<u16> for u32
impl From<u16> for u32
pub fn from(u: u16) -> Self
pub fn from(u: u16) -> Self
Casts a u16
to a u32
.
Returns
- [u32] - The
u32
representation of theu16
value.
Examples
fn foo() {
let u32_value = u32::from(0u16);
}