- Struct
Result
- Constants
- Function
ok
- Function
is_ok
- Function
get
- Function
err
- Function
err_str
- Function
is_err
- Function
get_err
- Function
as_err
- Function
unpack
- Function
and_then
- Function
unwrap
- Function
unwrap_err
- Function
assert_ok
- Function
assert_err
use 0x1::option;
use 0x1::string;
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
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;
public fun ok<T, E>(value: T): result::Result<T, E>
public fun is_ok<T, E>(result: &result::Result<T, E>): bool
public fun get<T, E>(result: &result::Result<T, E>): &option::Option<T>
public fun err<T, E>(err: E): result::Result<T, E>
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>
public fun is_err<T, E>(result: &result::Result<T, E>): bool
public fun get_err<T, E>(result: &result::Result<T, E>): &option::Option<E>
Convert an error Result<T, String> to error Result<U, String>.
public fun as_err<U, T>(self: result::Result<T, string::String>): result::Result<U, string::String>
public fun unpack<T, E>(result: result::Result<T, E>): (option::Option<T>, option::Option<E>)
public fun and_then<U, T, E>(result: result::Result<U, E>, f: |U|result::Result<T, E>): result::Result<T, E>
public fun unwrap<T, E: drop>(result: result::Result<T, E>): T
public fun unwrap_err<T, E>(result: result::Result<T, E>): E
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
public fun assert_err<T, E>(result: result::Result<T, E>, abort_code: u64): E