Part source from https://github.com/aptos-labs/aptos-core/blob/main/aptos-move/framework/aptos-stdlib/sources/from_bcs.move
This module provides a number of functions to convert primitive types from their representation in std::bcs
to values. This is the opposite of bcs::to_bytes
.
Note we provie a generic public from_bytes
function and protected it with #[data_struct(T)]
.
- Struct
BCS
- Constants
- Function
to_bytes
- Function
to_bool
- Function
to_u8
- Function
to_u64
- Function
to_u128
- Function
to_address
- Function
new
- Function
into_remainder_bytes
- Function
peel_address
- Function
peel_bool
- Function
peel_u8
- Function
peel_u16
- Function
peel_u32
- Function
peel_u64
- Function
peel_u128
- Function
peel_u256
- Function
peel_vec_length
- Function
peel_vec_address
- Function
peel_vec_bool
- Function
peel_vec_u8
- Function
peel_vec_vec_u8
- Function
peel_vec_u16
- Function
peel_vec_u32
- Function
peel_vec_u64
- Function
peel_vec_u128
- Function
peel_vec_u256
- Function
peel_option_address
- Function
peel_option_bool
- Function
peel_option_u8
- Function
peel_option_u16
- Function
peel_option_u32
- Function
peel_option_u64
- Function
peel_option_u128
- Function
peel_option_u256
- Function
from_bytes
- Function
from_bytes_option
- Function
native_from_bytes
use 0x1::bcs;
use 0x1::option;
use 0x1::vector;
A helper struct that saves resources on operations. For better
vector performance, it stores reversed bytes of the BCS and
enables use of vector::pop_back
.
struct BCS has copy, drop, store
const ErrorInvalidBool: u64 = 4;
const ErrorInvalidBytes: u64 = 2;
const ErrorInvalidLength: u64 = 3;
const ErrorLengthOutOfRange: u64 = 6;
const ErrorOutOfRange: u64 = 5;
The request Move type is not match with input Move type.
const ErrorTypeNotMatch: u64 = 1;
public fun to_address(v: vector<u8>): address
Creates a new instance of BCS wrapper that holds inversed bytes for better performance.
Unpack the BCS
struct returning the leftover bytes.
Useful for passing the data further after partial deserialization.
public fun into_remainder_bytes(bcs: bcs::BCS): vector<u8>
Read address
value from the bcs-serialized bytes.
public fun peel_address(bcs: &mut bcs::BCS): address
Read a bool
value from bcs-serialized bytes.
Read u8
value from bcs-serialized bytes.
Read u16
value from bcs-serialized bytes.
Read u32
value from bcs-serialized bytes.
Read u64
value from bcs-serialized bytes.
Read u128
value from bcs-serialized bytes.
Read u256
value from bcs-serialized bytes.
Read ULEB bytes expecting a vector length. Result should
then be used to perform peel_*
operation LEN times.
In BCS vector
length is implemented with ULEB128;
See more here: https://en.wikipedia.org/wiki/LEB128
public fun peel_vec_length(bcs: &mut bcs::BCS): u64
Peel a vector of address
from serialized bytes.
public fun peel_vec_address(bcs: &mut bcs::BCS): vector<address>
Peel a vector of bool
from serialized bytes.
public fun peel_vec_bool(bcs: &mut bcs::BCS): vector<bool>
Peel a vector of u8
(eg string) from serialized bytes.
public fun peel_vec_u8(bcs: &mut bcs::BCS): vector<u8>
Peel a vector<vector<u8>>
(eg vec of string) from serialized bytes.
public fun peel_vec_vec_u8(bcs: &mut bcs::BCS): vector<vector<u8>>
Peel a vector of u16
from serialized bytes.
public fun peel_vec_u16(bcs: &mut bcs::BCS): vector<u16>
Peel a vector of u32
from serialized bytes.
public fun peel_vec_u32(bcs: &mut bcs::BCS): vector<u32>
Peel a vector of u64
from serialized bytes.
public fun peel_vec_u64(bcs: &mut bcs::BCS): vector<u64>
Peel a vector of u128
from serialized bytes.
public fun peel_vec_u128(bcs: &mut bcs::BCS): vector<u128>
Peel a vector of u256
from serialized bytes.
public fun peel_vec_u256(bcs: &mut bcs::BCS): vector<u256>
Peel Option<address>
from serialized bytes.
public fun peel_option_address(bcs: &mut bcs::BCS): option::Option<address>
Peel Option<bool>
from serialized bytes.
public fun peel_option_bool(bcs: &mut bcs::BCS): option::Option<bool>
Peel Option<u8>
from serialized bytes.
public fun peel_option_u8(bcs: &mut bcs::BCS): option::Option<u8>
Peel Option<u16>
from serialized bytes.
public fun peel_option_u16(bcs: &mut bcs::BCS): option::Option<u16>
Peel Option<u32>
from serialized bytes.
public fun peel_option_u32(bcs: &mut bcs::BCS): option::Option<u32>
Peel Option<u64>
from serialized bytes.
public fun peel_option_u64(bcs: &mut bcs::BCS): option::Option<u64>
Peel Option<u128>
from serialized bytes.
public fun peel_option_u128(bcs: &mut bcs::BCS): option::Option<u128>
Peel Option<u256>
from serialized bytes.
public fun peel_option_u256(bcs: &mut bcs::BCS): option::Option<u256>
Function to deserialize a type T.
Note the data_struct
ensure the T
must be a #[data_struct]
type
#[data_struct(#[T])]
public fun from_bytes<T>(bytes: vector<u8>): T
Function to deserialize a type T.
Note the data_struct
ensure the T
must be a #[data_struct]
type
If the bytes are invalid, it will return None.
#[data_struct(#[T])]
public fun from_bytes_option<T>(bytes: vector<u8>): option::Option<T>
public(friend) fun native_from_bytes<T>(bytes: vector<u8>): option::Option<T>