b256
Expand description
Implementations
fn is_zero(self) -> bool
fn is_zero(self) -> bool
Returns whether a b256
is set to zero.
Returns
- [bool] -> True if the
b256
is zero, otherwise false.
Examples
fn foo() {
let zero_b256 = b256::zero();
assert(zero_b256.is_zero());
}
fn to_le_bytes(self) -> [u8; 32]
fn to_le_bytes(self) -> [u8; 32]
Converts the b256
to a sequence of little-endian bytes.
Returns
- [[u8; 32]] - An array of 32
u8
bytes that compose theb256
.
Examples
fn foo() {
let x = b256::new(0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20);
let bytes = x.to_le_bytes();
let mut i: u8 = 0;
while i < 32_u8 {
assert(bytes[i.as_u64()] == 32_u8 - i);
i += 1_u8;
}
}
fn from_le_bytes(bytes: [u8; 32]) -> Self
fn from_le_bytes(bytes: [u8; 32]) -> Self
Converts a sequence of little-endian bytes to a b256
.
Arguments
bytes
: [[u8; 32]] - A sequence of 32u8
bytes that represent ab256
.
Returns
- [b256] - The resulting
b256
value.
Examples
fn foo() {
let bytes = [32_u8, 31_u8, 30_u8, 29_u8, 28_u8, 27_u8, 26_u8, 25_u8, 24_u8, 23_u8,
22_u8, 21_u8, 20_u8, 19_u8, 18_u8, 17_u8, 16_u8, 15_u8, 14_u8, 13_u8,
12_u8, 11_u8, 10_u8, 9_u8, 8_u8, 7_u8, 6_u8, 5_u8, 4_u8, 3_u8,
2_u8, 1_u8];
let x = b256::from_le_bytes(bytes);
assert(x == 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20);
fn to_be_bytes(self) -> [u8; 32]
fn to_be_bytes(self) -> [u8; 32]
Converts the b256
to a sequence of big-endian bytes.
Returns
- [[u8; 32]] - An array of 32
u8
bytes that compose theb256
.
Examples
fn foo() {
let x: b256 = 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20;
let bytes = x.to_be_bytes();
let mut i: u8 = 0;
while i < 32_u8 {
assert(bytes[i.as_u64()] == i + 1_u8);
i += 1_u8;
}
}
fn from_be_bytes(bytes: [u8; 32]) -> Self
fn from_be_bytes(bytes: [u8; 32]) -> Self
Converts a sequence of big-endian bytes to a b256
.
Arguments
bytes
: [[u8; 32]] - A sequence of 32u8
bytes that represent ab256
.
Returns
- [b256] - The resulting
b256
value.
Examples
fn foo() {
let bytes = [1_u8, 2_u8, 3_u8, 4_u8, 5_u8, 6_u8, 7_u8, 8_u8, 9_u8, 10_u8,
11_u8, 12_u8, 13_u8, 14_u8, 15_u8, 16_u8, 17_u8, 18_u8, 19_u8, 20_u8,
21_u8, 22_u8, 23_u8, 24_u8, 25_u8, 26_u8, 27_u8, 28_u8, 29_u8, 30_u8,
31_u8, 32_u8];
let x = b256::from_be_bytes(bytes);
assert(x == 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20);
}
fn to_le_bytes(self) -> Bytes
fn to_le_bytes(self) -> Bytes
Converts the b256
to a sequence of little-endian bytes.
Returns
- [Bytes] - The 32 bytes that compose the
b256
.
Examples
fn foo() {
let x: b256 = 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20;
let bytes = x.to_le_bytes();
let mut i: u8 = 0;
while i < 32_u8 {
assert(bytes.get(i.as_u64()).unwrap() == 32_u8 - i);
i += 1_u8;
}
}
fn from_le_bytes(bytes: Bytes) -> Self
fn from_le_bytes(bytes: Bytes) -> Self
Converts a sequence of little-endian bytes to a b256
.
Arguments
bytes
: [Bytes] - The 32 bytes that compose theb256
.
Returns
- [b256] - The resulting
b256
value.
Examples
fn foo() {
let mut bytes = Bytes::with_capacity(32);
let mut i: u8 = 0;
while i < 32_u8 {
bytes.push(32_u8 - i);
i += 1_u8;
}
let x = b256::from_le_bytes(bytes);
assert(x == 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20);
}
fn to_be_bytes(self) -> Bytes
fn to_be_bytes(self) -> Bytes
Converts the b256
to a sequence of big-endian bytes.
Returns
- [Bytes] - The 32 bytes that compose the
b256
.
Examples
fn foo() {
let x: b256 = 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20;
let bytes = x.to_be_bytes();
let mut i: u8 = 0;
while i < 32_u8 {
assert(bytes.get(i.as_u64()).unwrap() == i + 1_u8);
i += 1_u8;
}
}
fn from_be_bytes(bytes: Bytes) -> Self
fn from_be_bytes(bytes: Bytes) -> Self
Converts a sequence of big-endian bytes to a b256
.
Arguments
bytes
: [Bytes] - The 32 bytes that compose theb256
.
Returns
- [b256] - The resulting
b256
value.
Examples
fn foo() {
let mut bytes = Bytes::with_capacity(32);
let mut i: u8 = 0;
while i < 32_u8 {
bytes.push(i + 1);
i += 1_u8;
}
let x = b256::from_be_bytes(bytes);
assert(x == 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20);
}
Trait Implementations
impl TryFrom<B512> for b256
impl TryFrom<B512> for b256
fn try_from(val: B512) -> Option<Self>
fn try_from(val: B512) -> Option<Self>
Attempts conversion from a B512
to a b256
.
Additional Information
If the high bits of the B512
are not zero, the conversion will fail.
Arguments
val
: [B512] - TheB512
to be converted.
Returns
- [Option] - The
b256
representation of theB512
value.
Examples
use std::b512::B512;
fn foo() {
let b512_value = B512::new();
let b256_value = b256::try_from(b512_value).unwrap();
}
impl From<u256> for b256
impl From<u256> for b256
fn from(num: u256) -> Self
fn from(num: u256) -> Self
Casts a u256
to raw b256
data.
Returns
- [b256] - The underlying raw
b256
data of theu256
.
Examples
fn foo() {
let b256_value = b256::from(0x0000000000000000000000000000000000000000000000000000000000000000_u256);
}
impl From<U128> for b256
impl From<U128> for b256
fn from(num: U128) -> Self
fn from(num: U128) -> Self
Converts a U128
to a b256
.
Arguments
num
: [U128] - TheU128
to be converted.
Returns
- [b256] - The
b256
representation of theU128
value.
Examples
use std::u128::U128;
fn foo() {
let u128_value = U128::from((18446744073709551615_u64, 18446744073709551615_u64));
let b256_value = b256::from(u128_value);
}
impl From<(u64, u64, u64, u64)> for b256
impl From<(u64, u64, u64, u64)> for b256
fn from(nums: (u64, u64, u64, u64)) -> Self
fn from(nums: (u64, u64, u64, u64)) -> Self
Casts a tuple of 4 u64
values to a b256
.
Arguments
nums
: (u64, u64, u64, u64) - The tuple ofu64
values to be casted.
Returns
- [b256] - The
b256
representation of the tuple ofu64
values.
Examples
fn foo() {
let b256_value = b256::from((1, 2, 3, 4));
}
impl From<ContractId> for b256
impl From<ContractId> for b256
fn from(id: ContractId) -> Self
fn from(id: ContractId) -> Self
Casts a ContractId
to raw b256
data.
Returns
- [b256] - The underlying raw
b256
data of theContractId
.
Examples
fn foo() {
let contract_id = ContractId::from(b256::zero());
let b256_data: b256 = contract_id.into();
assert(b256_data == b256::zero());
}
impl From<AssetId> for b256
impl From<AssetId> for b256
fn from(id: AssetId) -> Self
fn from(id: AssetId) -> Self
Casts an AssetId
to raw b256
data.
Returns
- [b256] - The underlying raw
b256
data of theAssetId
.
Examples
fn foo() {
let asset_id = AssetId::b256::zero();
let b256_data: b256 = asset_id.into();
assert(b256_data == b256::zero());
}
impl From<Address> for b256
impl From<Address> for b256
fn from(address: Address) -> Self
fn from(address: Address) -> Self
Casts an Address
to raw b256
data.
Returns
- [b256] - The underlying raw
b256
data of theAddress
.
Examples
fn foo() {
let address = Address::zero();
let b256_data: b256 = address.into();
assert(b256_data == b256::zero());
}
impl From<EvmAddress> for b256
impl From<EvmAddress> for b256
fn from(addr: EvmAddress) -> b256
fn from(addr: EvmAddress) -> b256
Casts an EvmAddress
to raw b256
data.
Returns
- [b256] - The underlying raw
b256
data of theEvmAddress
.
Examples
use std::evm::EvmAddress;
fn foo() {
let evm_address = EvmAddress::zero();
let b256_data: b256 = evm_address.into();
assert(b256_data == b256::zero());
}