Skip to content

Commit

Permalink
TryFrom< B512 > for b256, u256 (#6010)
Browse files Browse the repository at this point in the history
## Description
Adds TryFrom implementation for b256 and u256 from B512

partially addresses #5797

## Checklist

- [x] I have linked to any relevant issues.
- [ ] I have commented my code, particularly in hard-to-understand
areas.
- [x] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [ ] If my change requires substantial documentation changes, I have
[requested support from the DevRel
team](https://github.com/FuelLabs/devrel-requests/issues/new/choose)
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [ ] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.

---------

Co-authored-by: K1-R1 <77465250+K1-R1@users.noreply.github.com>
Co-authored-by: Cameron Carstens <bitzoic.eth@gmail.com>
  • Loading branch information
3 people committed May 21, 2024
1 parent 8a0a722 commit 10b7221
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 3 deletions.
4 changes: 2 additions & 2 deletions sway-lib-std/src/lib.sw
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
library;

pub mod constants;
pub mod error_signals;
pub mod logging;
pub mod revert;
Expand All @@ -16,17 +17,16 @@ pub mod bytes;
pub mod math;
pub mod flags;
pub mod u128;
pub mod b512;
pub mod primitive_conversions;
pub mod alias;
pub mod hash;
pub mod asset_id;
pub mod contract_id;
pub mod execution;
pub mod constants;
pub mod call_frames;
pub mod context;
pub mod external;
pub mod b512;
pub mod tx;
pub mod outputs;
pub mod address;
Expand Down
50 changes: 50 additions & 0 deletions sway-lib-std/src/primitive_conversions/b256.sw
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
library;

use ::bytes::Bytes;
use ::constants::ZERO_B256;
use ::convert::{From, TryFrom};
use ::option::Option::{self, *};
use ::u128::U128;
use ::b512::B512;

impl TryFrom<Bytes> for b256 {
fn try_from(b: Bytes) -> Option<Self> {
Expand All @@ -18,6 +20,41 @@ impl TryFrom<Bytes> for b256 {
}
}

impl TryFrom<B512> for b256 {
/// 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] - The `B512` to be converted.
///
/// # Returns
///
/// * [Option<b256>] - The `b256` representation of the `B512` value.
///
/// # Examples
///
/// ```sway
/// use std::b512::B512;
///
/// fn foo() {
/// let b512_value = B512::new();
/// let b256_value = b256::try_from(b512_value).unwrap();
/// }
/// ```
fn try_from(val: B512) -> Option<Self> {
let bits = val.bits();
if bits[0] == ZERO_B256 {
Some(bits[1])
} else {
None
}
}
}

impl From<u256> for b256 {
/// Casts a `u256` to raw `b256` data.
///
Expand Down Expand Up @@ -152,3 +189,16 @@ fn test_b256_from_tuple() {
b256_value == 0x0000000000000001000000000000000200000000000000030000000000000004,
);
}

#[test]
fn test_b256_try_from_b512() {
use ::assert::assert;

let b512_value = B512::new();
let b256_value = b256::try_from(b512_value);
assert(b256_value.is_some());

let b512_value = B512::from((0x0000000000000000000000000000000000000000000000000000000000000001, ZERO_B256));
let b256_value = b256::try_from(b512_value);
assert(b256_value.is_none());
}
53 changes: 52 additions & 1 deletion sway-lib-std/src/primitive_conversions/u256.sw
Original file line number Diff line number Diff line change
@@ -1,7 +1,45 @@
library;

use ::convert::From;
use ::constants::ZERO_B256;
use ::convert::{From, TryFrom};
use ::option::Option::{self, *};
use ::u128::U128;
use ::b512::B512;

impl TryFrom<B512> for u256 {
/// Attempts conversion from a `B512` to a `u256`.
///
/// # Additional Information
///
/// If the high bits of the `B512` are not zero, the conversion will fail.
///
/// # Arguments
///
/// * `val`: [B512] - The `B512` to be converted.
///
/// # Returns
///
/// * [Option<u256>] - The `u256` representation of the `B512` value.
///
/// # Examples
///
/// ```sway
/// use std::b512::B512;
///
/// fn foo() {
/// let b512_value = B512::new();
/// let u256_value = u256::try_from(b512_value).unwrap();
/// }
/// ```
fn try_from(val: B512) -> Option<Self> {
let bits = val.bits();
if bits[0] == ZERO_B256 {
Some(bits[1].as_u256())
} else {
None
}
}
}

/// Functions for casting between `u256` and other types.
impl From<u8> for u256 {
Expand Down Expand Up @@ -255,3 +293,16 @@ fn test_u256_from_tuple() {
u256_value == 0x0000000000000001000000000000000200000000000000030000000000000004_u256,
);
}

#[test]
fn test_u256_try_from_b512() {
use ::assert::assert;

let b512_value = B512::new();
let u256_value = u256::try_from(b512_value);
assert(u256_value.is_some());

let b512_value = B512::from((0x0000000000000000000000000000000000000000000000000000000000000001, ZERO_B256));
let u256_value = u256::try_from(b512_value);
assert(u256_value.is_none());
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ registers.sw
flags.sw
math.sw
u128.sw
b512.sw
bytes_conversions.sw
bytes_conversions/b256.sw
bytes_conversions/u16.sw
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
library;

pub mod constants;
pub mod error_signals;
pub mod logging;
pub mod revert;
Expand All @@ -16,6 +17,7 @@ pub mod registers;
pub mod flags;
pub mod math;
pub mod u128;
pub mod b512;
pub mod primitive_conversions;
pub mod array_conversions;
pub mod bytes_conversions;
Expand Down

0 comments on commit 10b7221

Please sign in to comment.