Skip to content

Latest commit

 

History

History
228 lines (101 loc) · 6.39 KB

result.md

File metadata and controls

228 lines (101 loc) · 6.39 KB

Module 0x2::result

Struct Result

The same as Rust's Result type. Most of the time, we do not need the Result type in smart contract, we can directly abort the transaction. But in some cases, we need to return a result to ensure the caller can handle the error.

struct Result<T, E> has copy, drop

Constants

Expected the result is err but the result is ok.

const ErrorExpectErr: u64 = 2;

Expected the result is ok but the result is err.

const ErrorExpectOk: u64 = 1;

Function ok

public fun ok<T, E>(value: T): result::Result<T, E>

Function is_ok

public fun is_ok<T, E>(result: &result::Result<T, E>): bool

Function get

public fun get<T, E>(result: &result::Result<T, E>): &option::Option<T>

Function err

public fun err<T, E>(err: E): result::Result<T, E>

Function err_str

A shortcut to create a Result<T, String> with an error String with err_str(b"msg").

public fun err_str<T>(err: vector<u8>): result::Result<T, string::String>

Function is_err

public fun is_err<T, E>(result: &result::Result<T, E>): bool

Function get_err

public fun get_err<T, E>(result: &result::Result<T, E>): &option::Option<E>

Function as_err

Convert an error Result<T, String> to error Result<U, String>.

Function unpack

public fun unpack<T, E>(result: result::Result<T, E>): (option::Option<T>, option::Option<E>)

Function and_then

public fun and_then<U, T, E>(result: result::Result<U, E>, f: |U|result::Result<T, E>): result::Result<T, E>

Function unwrap

public fun unwrap<T, E: drop>(result: result::Result<T, E>): T

Function unwrap_err

public fun unwrap_err<T, E>(result: result::Result<T, E>): E

Function assert_ok

Assert the result is ok, and return the value. Otherwise, abort with the abort_code. This function is inline, so it will be expanded in the caller. This ensures the abort_code is the caller's location.

public fun assert_ok<T, E>(result: result::Result<T, E>, abort_code: u64): T

Function assert_err

public fun assert_err<T, E>(result: result::Result<T, E>, abort_code: u64): E