Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve TODO and implement TryFrom<Bytes> for b256 #6958

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 31 additions & 7 deletions sway-lib-std/src/bytes.sw
Original file line number Diff line number Diff line change
Expand Up @@ -924,17 +924,41 @@ impl From<b256> for Bytes {
}
}

impl From<Bytes> for b256 {
// NOTE: this cas be lossy! Added here as the From trait currently requires it,
// but the conversion from `Bytes` ->`b256` should be implemented as
// `impl TryFrom<Bytes> for b256` when the `TryFrom` trait lands:
// https://github.com/FuelLabs/sway/pull/3881
fn from(bytes: Bytes) -> b256 {
impl Into<Bytes> for b256 {
fn into(self) -> Bytes {
// Artificially create bytes with capacity and len
let mut bytes = Bytes::with_capacity(32);
bytes.len = 32;
// Copy bytes from contract_id into the buffer of the target bytes
__addr_of(self).copy_bytes_to(bytes.buf.ptr, 32);

bytes
}
}

impl TryFrom<Bytes> for b256 {
fn try_from(bytes: Bytes) -> Option<Self> {
if bytes.len() != 32 {
return None;
}
let mut value = 0x0000000000000000000000000000000000000000000000000000000000000000;
let ptr = __addr_of(value);
bytes.buf.ptr().copy_to::<b256>(ptr, 1);

value
Some(value)
}
}

impl TryInto<b256> for Bytes {
fn try_into(self) -> Option<b256> {
if self.len != 32 {
return None;
}
let mut value = 0x0000000000000000000000000000000000000000000000000000000000000000;
let ptr = __addr_of(value);
self.buf.ptr().copy_to::<b256>(ptr, 1);

Some(value)
}
}

Expand Down
2 changes: 1 addition & 1 deletion sway-lib-std/src/bytes_conversions/b256.sw
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,6 @@ impl b256 {
/// ```
pub fn from_be_bytes(bytes: Bytes) -> Self {
assert(bytes.len() == 32);
bytes.into()
bytes.try_into().unwrap()
}
}
2 changes: 1 addition & 1 deletion sway-lib-std/src/bytes_conversions/u256.sw
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ impl u256 {
/// ```
pub fn from_be_bytes(bytes: Bytes) -> Self {
assert(bytes.len() == 32);
let b: b256 = bytes.into();
let b: b256 = bytes.try_into().unwrap();
asm(r1: b) {
r1: u256
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -863,7 +863,7 @@ fn bytes_from_b256() {
}

#[test]
fn bytes_into_b256() {
fn bytes_try_into_b256() {
let mut initial_bytes = Bytes::with_capacity(32);

let mut i = 0;
Expand All @@ -873,14 +873,33 @@ fn bytes_into_b256() {
i += 1;
}

let value: b256 = initial_bytes.into();
let value: b256 = initial_bytes.try_into().unwrap();
let expected: b256 = 0x3333333333333333333333333333333333333333333333333333333333333333;

assert(value == expected);

let empty_bytes = Bytes::new();
let empty_result: Option<b256> = empty_bytes.try_into();
assert(empty_result.is_none());

let mut small_bytes = Bytes::new();
small_bytes.push(1u8);
let small_result: Option<b256> = small_bytes.try_into();
assert(small_result.is_none());

let mut large_bytes = Bytes::new();
let mut i = 0;
while i < 33 {
// 0x33 is 51 in decimal
large_bytes.push(51u8);
i += 1;
}
let large_result: Option<b256> = large_bytes.try_into();
assert(large_result.is_none());
}

#[test]
fn bytes_b256_from() {
fn bytes_b256_try_from() {
let control = 0x3333333333333333333333333333333333333333333333333333333333333333;
let mut bytes = Bytes::with_capacity(32);

Expand All @@ -891,9 +910,27 @@ fn bytes_b256_from() {
i += 1;
}

let result_b256: b256 = b256::from(bytes);

let result_b256: b256 = b256::try_from(bytes).unwrap();
assert(result_b256 == control);

let empty_bytes = Bytes::new();
let empty_result = b256::try_from(empty_bytes);
assert(empty_result.is_none());

let mut small_bytes = Bytes::new();
small_bytes.push(1u8);
let small_result = b256::try_from(small_bytes);
assert(small_result.is_none());

let mut large_bytes = Bytes::new();
let mut i = 0;
while i < 33 {
// 0x33 is 51 in decimal
large_bytes.push(51u8);
i += 1;
}
let large_result = b256::try_from(large_bytes);
assert(large_result.is_none());
}

#[test]
Expand Down
Loading