Skip to content

Commit

Permalink
Rename core::num to core::primitives (FuelLabs#3488)
Browse files Browse the repository at this point in the history
The current `core::num` name is not well aligned with types like `b256`
which are not numeric types.
In addition, in Rust, methods like `min(), `max()` and `bits()` are in
the `primitives` module.
Closes FuelLabs#2777
  • Loading branch information
nfurfaro authored Dec 2, 2022
1 parent 2188c1b commit c471e82
Show file tree
Hide file tree
Showing 18 changed files with 26 additions and 42 deletions.
1 change: 0 additions & 1 deletion examples/hashing/src/main.sw
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
script;

use core::num::*;
use std::{hash::{keccak256, sha256}, logging::log};

const VALUE_A = 0x9280359a3b96819889d30614068715d634ad0cf9bba70c0f430a8c201138f79f;
Expand Down
2 changes: 1 addition & 1 deletion sway-lib-core/src/lib.sw
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
library core;

dep num;
dep primitives;
dep raw_ptr;
dep raw_slice;
dep ops;
Expand Down
2 changes: 1 addition & 1 deletion sway-lib-core/src/ops.sw
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
library ops;

use ::num::*;
use ::primitives::*;

pub trait Add {
fn add(self, other: Self) -> Self;
Expand Down
2 changes: 1 addition & 1 deletion sway-lib-core/src/prelude.sw
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ library prelude;
//! Defines the Sway core library prelude.
//! The prelude consists of implicitly available items,
//! for which `use` is not required.
use ::num::*;
use ::primitives::*;
use ::raw_ptr::*;
use ::raw_slice::*;
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
library num;
library primitives;

impl u64 {
/// The smallest value that can be represented by this integer type.
Expand Down
2 changes: 0 additions & 2 deletions sway-lib-std/src/hash.sw
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
library hash;

use ::core::num::*;

/// Returns the SHA-2-256 hash of `param`.
pub fn sha256<T>(param: T) -> b256 {
let mut result_buffer: b256 = b256::min();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
script;

use core::num::*;
use std::assert::assert;
use std::logging::log;

Expand Down Expand Up @@ -38,7 +37,6 @@ fn main() -> bool {
///////////////////////////////////////////////////////
// test &, |, ^
///////////////////////////////////////////////////////

assert(a & b == c);
assert(a & c == c);
assert(b & c == c);
Expand Down Expand Up @@ -102,7 +100,6 @@ fn main() -> bool {
///////////////////////////////////////////////////////
// test Ord
///////////////////////////////////////////////////////

assert(one > b256::min());
assert(two > one);
assert(one < two);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
script;

use std::assert::assert;
use core::num::*;

fn main() -> bool {

assert(u64::max() == 18446744073709551615);
assert(u64::min() == 0u64);
assert(u64::bits() == 64u32);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
script;
use std::{assert::assert, constants::BASE_ASSET_ID, contract_id::ContractId};
use context_testing_abi::*;
use core::num::*;

fn main() -> bool {
let zero = b256::min();
Expand All @@ -14,29 +13,34 @@ fn main() -> bool {

// test Context::contract_id():
let returned_contract_id = test_contract.get_id {
gas: gas, coins: 0, asset_id: BASE_ASSET_ID.into()
}
();
gas: gas,
coins: 0,
asset_id: BASE_ASSET_ID.into(),
}();
assert(returned_contract_id.into() == other_contract_id.into());

// @todo set up a test contract to mint some tokens for testing balances.
// test Context::this_balance():
let returned_this_balance = test_contract.get_this_balance {
gas: gas, coins: 0, asset_id: BASE_ASSET_ID.into()
}
(base_asset_id);
gas: gas,
coins: 0,
asset_id: BASE_ASSET_ID.into(),
}(base_asset_id);
assert(returned_this_balance == 0);

// test Context::balance_of_contract():
let returned_contract_balance = test_contract.get_balance_of_contract {
gas: gas, coins: 0, asset_id: BASE_ASSET_ID.into()
}
(base_asset_id, other_contract_id);
gas: gas,
coins: 0,
asset_id: BASE_ASSET_ID.into(),
}(base_asset_id, other_contract_id);
assert(returned_contract_balance == 0);




// The checks below don't work (AssertIdNotFound). The test should be
// updated to forward coins that are actually available.

// test Context::msg_value():
/*let returned_amount = test_contract.get_amount {
gas: gas, coins: amount, asset_id: BASE_ASSET_ID
Expand Down Expand Up @@ -66,6 +70,5 @@ fn main() -> bool {
}
();
assert(global_gas == 1000);*/

true
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
script;

use core::num::*;
use std::alloc::*;
use std::intrinsics::*;
use std::assert::assert;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ script;

use std::b512::B512;
use std::assert::assert;
use core::num::*;

// helper to prove contiguity of memory in B512 type's hi & lo fields.
fn are_fields_contiguous(big_value: B512) -> bool {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ script;

use std::{assert::assert, math::*};
use std::revert::revert;
use core::num::*;

fn main() -> bool {
// u64
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ script;

use std::intrinsics::*;
use std::assert::assert;
use core::num::*;

struct TestStruct {
field_1: bool,
Expand All @@ -21,8 +20,7 @@ fn main() -> bool {
let e: b256 = zero;
let f: str[11] = "Fuel rocks!";

let test_array = [42u16;
3];
let test_array = [42u16; 3];

let test_struct = TestStruct {
field_1: false,
Expand All @@ -45,8 +43,7 @@ fn main() -> bool {
assert(size_of::<u8>() == 8);
assert(size_of::<b256>() == 32);
assert(size_of::<str[11]>() == 16);
assert(size_of::<[u16;
3]>() == 24);
assert(size_of::<[u16; 3]>() == 24);
assert(size_of::<TestStruct>() == 16);

assert(size_of_val(a) == 8);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ script;

use std::assert::assert;
use std::u256::U256;
use core::num::*;

fn main() -> bool {
let zero = U256::from((0, 0, 0, 0));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ script;

use std::assert::assert;
use std::u256::U256;
use core::num::*;

fn main() -> bool {
let zero = U256::from((0, 0, 0, 0));
Expand All @@ -23,7 +22,7 @@ fn main() -> bool {
assert(mul_128_max.d == 1);

let a_2_62_mul_2 = U256::from((1 << 62, 0, 0, 0)) * two;
assert(a_2_62_mul_2.a == (1 << 63) );
assert(a_2_62_mul_2.a == (1 << 63));
assert(a_2_62_mul_2.b == 0);

let a_2_61_mul_5 = U256::from((1 << 61, 0, 0, 0)) * U256::from((0, 0, 0, 5));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ script;

use std::assert::assert;
use std::u256::U256;
use core::num::*;

fn main() -> bool {
let first = U256::from((0, 0, 0, 0));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
script;

use core::num::*;
use std::{assert::assert, result::Result, u128::U128, u256::{U256, U256Error}};

fn main() -> bool {
Expand All @@ -15,7 +14,7 @@ fn main() -> bool {
assert(new == empty);

// test from(() & into()
let(l, m, n, o) = new.into();
let (l, m, n, o) = new.into();
assert(l == 0);
assert(m == 0);
assert(n == 0);
Expand All @@ -37,7 +36,7 @@ fn main() -> bool {
assert(x.d == d);
assert(x.d != a);

let(e, f, g, h) = x.into();
let (e, f, g, h) = x.into();
assert(e == a);
assert(f == b);
assert(g == c);
Expand All @@ -48,13 +47,13 @@ fn main() -> bool {
// test min() & max()
let max = U256::max();
let min = U256::min();
let(one, two, three, four) = max.into();
let (one, two, three, four) = max.into();
assert(one == u64::max());
assert(two == u64::max());
assert(three == u64::max());
assert(four == u64::max());

let(min_1, min_2, min_3, min_4) = min.into();
let (min_1, min_2, min_3, min_4) = min.into();
assert(min_1 == u64::min());
assert(min_2 == u64::min());
assert(min_3 == u64::min());
Expand Down
1 change: 0 additions & 1 deletion test/src/sdk-harness/test_projects/hashing/src/main.sw
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
contract;

use std::hash::{keccak256, sha256};
use core::num::*;

abi MyContract {
fn sha256_u8(value: u8) -> b256;
Expand Down

0 comments on commit c471e82

Please sign in to comment.