Skip to content

Commit

Permalink
Adding documentation for the standard library (FuelLabs#3314)
Browse files Browse the repository at this point in the history
This PR aims to add documentation aswell as examples for more std
library functions

Example documentation
```
/// Inserts a key-value pair into the map.
/// 
/// # Arguments
///
/// * `key` - The key to which the value is paired
/// * `value` - The value to be stored
///
/// # Examples
/// 
/// ```
/// storage {
///     map: StorageMap = StorageMap {} 
/// }
/// 
/// fn foo() {
///     let key = 5_u64;
///     let value = true;
///     storage.map.insert(key, value);
///     let retrieved_value = storage.map.get(key);
///     assert(value == retrieved_value);
/// }
/// ```
#[storage(write)]
fn insert(self, key: K, value: V) {
    let key = sha256((key, __get_storage_key()));
    store::<V>(key, value);
}
```
closes FuelLabs#3313

Co-authored-by: Mohammad Fawaz <[email protected]>
Co-authored-by: Anton Trunov <[email protected]>
  • Loading branch information
3 people authored Nov 10, 2022
1 parent 7a389dd commit 7a8ab39
Show file tree
Hide file tree
Showing 8 changed files with 834 additions and 27 deletions.
18 changes: 18 additions & 0 deletions sway-lib-std/src/assert.sw
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,24 @@ use ::revert::revert;
/// Asserts that the given `condition` will always be `true` during runtime.
/// To check for conditions that may not be `true`, use `std::revert::require` instead.
/// See: https://en.wikipedia.org/wiki/Assertion_(software_development)#Comparison_with_error_handling
///
/// ### Arguments
///
/// * `condition` - The condition which will be asserted to be `true`
///
/// ### Reverts
///
/// Reverts when `condition` is `false`
///
/// ### Examples
///
/// ```sway
/// fn foo(a: u64, b: u64) {
/// assert(a == b);
/// // if code execution continues, that means a was equal to b
/// log("a is equal to b");
/// }
/// ```
pub fn assert(condition: bool) {
if !condition {
revert(0);
Expand Down
2 changes: 1 addition & 1 deletion sway-lib-std/src/message.sw
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use ::error_signals::FAILED_SEND_MESSAGE_SIGNAL;

/// Sends a message to `recipient` of length `msg_len` through `output` with amount of `coins`
///
/// # Arguments
/// ### Arguments
///
/// * `recipient` - The address of the message recipient
/// * `msg_data` - arbitrary length message data
Expand Down
42 changes: 42 additions & 0 deletions sway-lib-std/src/revert.sw
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,53 @@ use ::error_signals::FAILED_REQUIRE_SIGNAL;
/// Context-dependent:
/// will panic if used in a predicate
/// will revert if used in a contract
///
/// ### Arguments
///
/// * `code` - The code with which to revert the program
///
/// ### Reverts
///
/// Reverts when called in a contract
///
/// ### Panics
///
/// Panics when called in a predicate
///
/// ### Examples
///
/// ```sway
/// fn foo(should_revert: bool) {
/// match should_revert {
/// true => revert(0),
/// false => {},
/// }
/// }
/// ```
pub fn revert(code: u64) {
__revert(code)
}

/// Checks if the given `condition` is `true` and if not, logs `value` and reverts.
///
/// ### Arguments
///
/// * `condition` - The condition upon which to decide whether to revert or not
/// * `value` - The value which will be logged in case `condition` is `false`
///
/// ### Reverts
///
/// Reverts when `condition` is false
///
/// ### Examples
///
/// ```sway
/// fn foo(a: u64, b: u64) {
/// require(a == b, "a was not equal to b");
/// // If the condition was true, code execution will continue
/// log("The require function did not revert");
/// }
/// ```
pub fn require<T>(condition: bool, value: T) {
if !condition {
log(value);
Expand Down
Loading

0 comments on commit 7a8ab39

Please sign in to comment.