From bf7304db959bcc4fdb4ff77e337d5a2545b17b3d Mon Sep 17 00:00:00 2001 From: Nick Furfaro Date: Sat, 27 Nov 2021 09:12:37 -0800 Subject: [PATCH] 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 --- stdlib/src/address.sw | 25 +++++++++++++++++++ stdlib/src/lib.sw | 1 + test/src/e2e_vm_tests/mod.rs | 1 + .../test_programs/address_test/Forc.toml | 8 ++++++ .../test_programs/address_test/src/main.sw | 18 +++++++++++++ 5 files changed, 53 insertions(+) create mode 100644 stdlib/src/address.sw create mode 100644 test/src/e2e_vm_tests/test_programs/address_test/Forc.toml create mode 100644 test/src/e2e_vm_tests/test_programs/address_test/src/main.sw diff --git a/stdlib/src/address.sw b/stdlib/src/address.sw new file mode 100644 index 00000000000..216e8287a77 --- /dev/null +++ b/stdlib/src/address.sw @@ -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, + } + } +} diff --git a/stdlib/src/lib.sw b/stdlib/src/lib.sw index cad98cbeb13..01e3ea6247b 100644 --- a/stdlib/src/lib.sw +++ b/stdlib/src/lib.sw @@ -6,3 +6,4 @@ dep storage; dep constants; dep b512; dep chain; +dep address; diff --git a/test/src/e2e_vm_tests/mod.rs b/test/src/e2e_vm_tests/mod.rs index e54395b8f2e..25a911dbf50 100644 --- a/test/src/e2e_vm_tests/mod.rs +++ b/test/src/e2e_vm_tests/mod.rs @@ -59,6 +59,7 @@ pub fn run(filter_regex: Option) { ("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 diff --git a/test/src/e2e_vm_tests/test_programs/address_test/Forc.toml b/test/src/e2e_vm_tests/test_programs/address_test/Forc.toml new file mode 100644 index 00000000000..a6f9ebdf84a --- /dev/null +++ b/test/src/e2e_vm_tests/test_programs/address_test/Forc.toml @@ -0,0 +1,8 @@ +[project] +author = "Nick Furfaro" +license = "MIT" +name = "address_test" +entry = "main.sw" + +[dependencies] +std = { path = "../../../../../stdlib"} \ No newline at end of file diff --git a/test/src/e2e_vm_tests/test_programs/address_test/src/main.sw b/test/src/e2e_vm_tests/test_programs/address_test/src/main.sw new file mode 100644 index 00000000000..036b6dc0bb3 --- /dev/null +++ b/test/src/e2e_vm_tests/test_programs/address_test/src/main.sw @@ -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 +}