-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding an
Address
type to the stdlib (#394)
* Initial commit of basic type * Update Address type to have a from_b256 method * Fix type to be pub * Update lib file * Add basic test * Update mod.rs with new test * Add comments * fixup * It builds * Update address.sw * Update test for address type * Refactor address tests to use assert() * Apply formatting to stdlib * Apply cargo fmt to mod.rs * Rename inner to value * Fixup * Simplfy from function for address by removing asm
- Loading branch information
Showing
5 changed files
with
53 additions
and
0 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,25 @@ | ||
library address; | ||
//! A wrapper around the b256 type to help enhance type-safety. | ||
|
||
/// The Address type, a struct wrappper around the inner `value`. | ||
pub struct Address { | ||
value: b256, | ||
} | ||
|
||
// @todo make this generic when possible | ||
pub trait From { | ||
fn from(b: b256) -> Self; | ||
} { | ||
fn into(addr: Address) -> b256 { | ||
addr.value | ||
} | ||
} | ||
|
||
/// Functions for casting between the b256 and Address types. | ||
impl From for Address { | ||
fn from(bits: b256) -> Address { | ||
Address { | ||
value: bits, | ||
} | ||
} | ||
} |
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 |
---|---|---|
|
@@ -6,3 +6,4 @@ dep storage; | |
dep constants; | ||
dep b512; | ||
dep chain; | ||
dep address; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[project] | ||
author = "Nick Furfaro" | ||
license = "MIT" | ||
name = "address_test" | ||
entry = "main.sw" | ||
|
||
[dependencies] | ||
std = { path = "../../../../../stdlib"} |
18 changes: 18 additions & 0 deletions
18
test/src/e2e_vm_tests/test_programs/address_test/src/main.sw
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,18 @@ | ||
script; | ||
|
||
use std::address::Address; | ||
use std::chain::assert; | ||
|
||
fn main() -> bool { | ||
let bits = 0x8900c5bec4ca97d4febf9ceb4754a60d782abbf3cd815836c1872116f203f861; | ||
|
||
// test from() | ||
let addr = ~Address::from(bits); | ||
assert(addr.value == bits); | ||
|
||
// test into() | ||
let new_bits = addr.into(); | ||
assert(new_bits == bits); | ||
|
||
true | ||
} |