Skip to content

Commit

Permalink
Add logging::log & assert::assert_eq to prelude.sw (FuelLabs#3916)
Browse files Browse the repository at this point in the history
  • Loading branch information
Braqzen authored Jan 29, 2023
1 parent dd8b2f6 commit 4131a16
Show file tree
Hide file tree
Showing 116 changed files with 74 additions and 192 deletions.
2 changes: 2 additions & 0 deletions docs/book/src/introduction/standard_library.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,7 @@ The current version of the prelude lives in [`std::prelude`](https://github.com/
- [`std::option::Option`](https://github.com/FuelLabs/sway/blob/master/sway-lib-std/src/option.sw), an enum which expresses the presence or absence of a value.
- [`std::result::Result`](https://github.com/FuelLabs/sway/blob/master/sway-lib-std/src/result.sw), an enum for functions that may succeed or fail.
- [`std::assert::assert`](https://github.com/FuelLabs/sway/blob/master/sway-lib-std/src/assert.sw), a function that reverts the VM if the condition provided to it is `false`.
- [`std::assert::assert_eq`](https://github.com/FuelLabs/sway/blob/master/sway-lib-std/src/assert.sw), a function that reverts the VM and logs its two inputs `v1` and `v2` if the condition `v1` == `v2` is `false`.
- [`std::revert::require`](https://github.com/FuelLabs/sway/blob/master/sway-lib-std/src/revert.sw), a function that reverts the VM and logs a given value if the condition provided to it is `false`.
- [`std::revert::revert`](https://github.com/FuelLabs/sway/blob/master/sway-lib-std/src/revert.sw), a function that reverts the VM.
- [`std::logging::log`](https://github.com/FuelLabs/sway/blob/master/sway-lib-std/src/logging.sw), a function that logs arbitrary stack types.
2 changes: 0 additions & 2 deletions docs/reference/src/code/operations/logging/src/lib.sw
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
library logging;

// ANCHOR: logging
use std::logging::log;

fn log_data(number: u64) {
// generic T = `number` of type `u64`
log(number);
Expand Down
2 changes: 1 addition & 1 deletion examples/hashing/src/main.sw
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
script;

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

const VALUE_A = 0x9280359a3b96819889d30614068715d634ad0cf9bba70c0f430a8c201138f79f;

Expand Down
2 changes: 1 addition & 1 deletion examples/signatures/src/main.sw
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
script;

use std::{b512::B512, ecr::{ec_recover, ec_recover_address, EcRecoverError}, logging::log};
use std::{b512::B512, ecr::{ec_recover, ec_recover_address, EcRecoverError}};

const MSG_HASH = 0xee45573606c96c98ba970ff7cf9511f1b8b25e6bcd52ced30b89df1e4a9c4323;

Expand Down
2 changes: 0 additions & 2 deletions examples/storage_map/src/main.sw
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
contract;

use std::logging::log;

storage {
// ANCHOR: storage_map_decl
map: StorageMap<Address, u64> = StorageMap {},
Expand Down
2 changes: 0 additions & 2 deletions examples/storage_vec/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ contract;
// ANCHOR: storage_vec_import
use std::storage::StorageVec;
// ANCHOR_END: storage_vec_import
use std::logging::log;

// ANCHOR: storage_vec_multiple_types_enum
enum TableCell {
Int: u64,
Expand Down
2 changes: 1 addition & 1 deletion examples/subcurrency/src/main.sw
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// ANCHOR: body
contract;

use std::{auth::{AuthError, msg_sender}, hash::sha256, logging::log};
use std::{auth::{AuthError, msg_sender}, hash::sha256};

////////////////////////////////////////
// Event declarations
Expand Down
2 changes: 0 additions & 2 deletions examples/vec/src/main.sw
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
script;

use std::logging::log;

fn main() {
// ANCHOR: vec_new
let v: Vec<u64> = Vec::new();
Expand Down
2 changes: 1 addition & 1 deletion sway-lib-std/src/logging.sw
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! Allows logging of arbitrary types, emitted as either `Log` or `Logd` receipts.
//! Allows logging of arbitrary stack types, emitted as either `Log` or `Logd` receipts.
library logging;

/// Log any stack type.
Expand Down
5 changes: 4 additions & 1 deletion sway-lib-std/src/prelude.sw
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@ use ::storage::StorageMap;
use ::vec::Vec;

// Error handling
use ::assert::assert;
use ::assert::{assert, assert_eq};
use ::option::Option;
use ::result::Result;
use ::revert::{require, revert};

// Convert
use ::convert::From;

// Logging
use ::logging::log;
12 changes: 6 additions & 6 deletions sway-lsp/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1032,11 +1032,11 @@ mod tests {
"range": {
"end": {
"character": 7,
"line": 13
"line": 11
},
"start": {
"character": 0,
"line": 13
"line": 11
}
}
}),
Expand All @@ -1053,11 +1053,11 @@ mod tests {
"range": {
"end": {
"character": 7,
"line": 8
"line": 6
},
"start": {
"character": 0,
"line": 8
"line": 6
}
}
}),
Expand All @@ -1069,11 +1069,11 @@ mod tests {
"range": {
"end": {
"character": 7,
"line": 4
"line": 2
},
"start": {
"character": 3,
"line": 4
"line": 2
}
}
}),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
script;

use std::revert::revert;

fn main() {
let x = if true {
42u64
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
script;

use std::{
assert::assert,
logging::log,
};

trait MyAdd {
fn my_add(self, other: Self) -> Self;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
script;

use std::{assert::assert, hash::sha256, option::Option, revert::revert, vec::Vec};
use std::hash::sha256;

struct SimpleStruct {
x: u32,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
script;

use std::{assert::assert, vec::Vec};

fn main() {
let mut vector = Vec::new();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
script;

use std::{assert::assert, vec::Vec};

fn main() {
let mut vector = Vec::new();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
script;

use std::logging::log;

struct Foo {
value: u64
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
script;

use std::revert::revert;

enum Result<T, E> {
Ok: T,
Err: E,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
script;

use std::{address::Address, identity::Identity, assert::assert, revert::revert};

const B1 = Address {
value: 0x0100000000000000000000000000000000000000000000000000000000000010
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
script;

use std::assert::assert;

// This file tests different kinds of ASM generation and parsing.

fn blockheight() -> u64 {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
script;

use std::assert::assert;
use std::logging::log;

fn main() -> bool {
let a: b256 = 0b1000000000000001_1000000000000001_1000000000000001_1000000000000001_1000000000000001_1000000000000001_1000000000000001_1000000000000001_1000000000000001_1000000000000001_1000000000000001_1000000000000001_1000000000000001_1000000000000001_1000000000000001_1000000000000001;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
script;

use std::assert::assert;

fn main() -> u64 {

let a: u8 = 2;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
script;

use std::{assert::assert, logging::log};

const N = 10;

fn simple_break_test() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ script;

dep test_lib;

use std::{assert::assert, contract_id::ContractId};

fn main() -> u64 {
let x = test_lib::NUMBER;
let zero = std::constants::ZERO_B256;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
contract;
use std::contract_id::ContractId;

abi MyContract {
fn test_function() -> bool;
Expand All @@ -12,5 +11,5 @@ impl MyContract for Contract {
}

fn caller(address: ContractId) -> ContractCaller<_> {
abi(MyContract, address.value)
abi(MyContract, address.value)
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
script;

use std::assert::assert;

fn revert<T>() -> T {
let code = 1u64;
__revert(code)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
script;

use std::{
b512::B512,
assert::assert,
logging::log
};
use std::b512::B512;

fn main() -> bool {
let hi_bits: b256 = 0x7777777777777777777777777777777777777777777777777777777777777777;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
script;

use std::{address::Address, assert::assert, identity::Identity};

enum Result<T, E> {
Ok: T,
Err: E,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
script;

use std::assert::assert;

struct T1 {
t1: u64,
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
script;

use std::address::Address;
use std::assert::assert;
use std::b512::B512;
use std::contract_id::ContractId;

fn main() -> bool {
// Primitive types
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
script;

use std::assert::assert;

fn main() -> u64 {

assert(__eq(true, true) == (true == true));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
script;

use std::{result::*, revert::*, u128::*, assert::assert};
use std::u128::*;

struct Data<T> {
value: T
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,37 @@
}
}
],
"loggedTypes": [],
"loggedTypes": [
{
"logId": 0,
"loggedType": {
"name": "",
"type": 1,
"typeArguments": null
}
},
{
"logId": 1,
"loggedType": {
"name": "",
"type": 1,
"typeArguments": null
}
}
],
"messagesTypes": [],
"types": [
{
"components": [],
"type": "()",
"typeId": 0,
"typeParameters": null
},
{
"components": null,
"type": "u64",
"typeId": 1,
"typeParameters": null
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ script;

dep utils;

use std::assert::assert;

use utils::*;

struct CustomType {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
library utils;

use std::revert::revert;

pub fn vec_from(vals: [u32; 3]) -> Vec<u32> {
let mut vec = Vec::new();
vec.push(vals[0]);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
script;

use std::{assert::assert, logging::log};

enum Bool {
True: (),
False: (),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
script;

use std::assert::assert;

fn f(cond: bool) -> u64 {
if cond {
10
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
script;

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

fn check_prime(n: u64) -> bool {
if n == 0 || n == 1 {
Expand Down
Loading

0 comments on commit 4131a16

Please sign in to comment.