forked from FuelLabs/sway
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This introduces a new trait `TryFrom` for conversions that could be fallible, and tests it by implementing for `b256` as `impl TryFrom<Bytes> for b256`: ``` pub trait TryFrom<T> { fn try_from(b: T) -> Option<Self>; } ```
- Loading branch information
Showing
4 changed files
with
64 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
library b256; | ||
|
||
use ::assert::assert; | ||
use ::bytes::Bytes; | ||
use ::convert::TryFrom; | ||
use ::option::Option; | ||
use ::logging::log; | ||
|
||
impl TryFrom<Bytes> for b256 { | ||
fn try_from(b: Bytes) -> Option<Self> { | ||
if b.len() > 32 { | ||
Option::None | ||
} else { | ||
let mut val = 0x0000000000000000000000000000000000000000000000000000000000000000; | ||
let ptr = __addr_of(val); | ||
b.buf.ptr().copy_to::<b256>(ptr, 1); | ||
Option::Some(val) | ||
} | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_b256_try_from() { | ||
let mut initial_bytes = Bytes::with_capacity(32); | ||
let mut i = 0; | ||
while i < 32 { | ||
// 0x33 is 51 in decimal | ||
initial_bytes.push(51u8); | ||
i += 1; | ||
} | ||
let res = b256::try_from(initial_bytes); | ||
let expected = 0x3333333333333333333333333333333333333333333333333333333333333333; | ||
|
||
assert(res.unwrap() == expected); | ||
|
||
let mut second_bytes = Bytes::with_capacity(33); | ||
i = 0; | ||
while i < 33 { | ||
// 0x33 is 51 in decimal | ||
second_bytes.push(51u8); | ||
i += 1; | ||
} | ||
let res = b256::try_from(second_bytes); | ||
assert(res.is_none()); | ||
|
||
// bytes is still available to use: | ||
assert(second_bytes.len() == 33); | ||
assert(second_bytes.capacity() == 33); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,17 @@ | ||
//! Traits for conversions between types. | ||
library convert; | ||
|
||
/// Used to do value-to-value conversions while consuming the input value. | ||
use ::option::Option; | ||
|
||
/// Used to do value-to-value conversions. | ||
pub trait From<T> { | ||
fn from(b: T) -> Self; | ||
fn into(self) -> T; | ||
} | ||
|
||
// TODO: return a Result when https://github.com/FuelLabs/sway/issues/610 is resolved | ||
/// Used to attempt to do value-to-value conversions. | ||
/// Returns Option::None if the conversion can't be performed in a lossless manner. | ||
pub trait TryFrom<T> { | ||
fn try_from(b: T) -> Option<Self>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters