Skip to content

Commit

Permalink
Adding an Address type to the stdlib (#394)
Browse files Browse the repository at this point in the history
* 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
nfurfaro authored Nov 27, 2021
1 parent 60815f7 commit bf7304d
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 0 deletions.
25 changes: 25 additions & 0 deletions stdlib/src/address.sw
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,
}
}
}
1 change: 1 addition & 0 deletions stdlib/src/lib.sw
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ dep storage;
dep constants;
dep b512;
dep chain;
dep address;
1 change: 1 addition & 0 deletions test/src/e2e_vm_tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ pub fn run(filter_regex: Option<regex::Regex>) {
("generic_functions", ProgramState::Return(1)), // true
("generic_enum", ProgramState::Return(1)), // true
("import_method_from_other_file", ProgramState::Return(10)), // true
("address_test", ProgramState::Return(1)), // true
("generic_struct", ProgramState::Return(1)), // true
("assert_test", ProgramState::Return(1)), // true
("b512_test", ProgramState::Return(1)), // true
Expand Down
8 changes: 8 additions & 0 deletions test/src/e2e_vm_tests/test_programs/address_test/Forc.toml
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 test/src/e2e_vm_tests/test_programs/address_test/src/main.sw
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
}

0 comments on commit bf7304d

Please sign in to comment.