Skip to content

Latest commit

 

History

History
578 lines (265 loc) · 15.2 KB

bcs.md

File metadata and controls

578 lines (265 loc) · 15.2 KB

Module 0x2::bcs

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

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

Constants

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;

Function to_bytes

public fun to_bytes<MoveValue>(v: &MoveValue): vector<u8>

Function to_bool

public fun to_bool(v: vector<u8>): bool

Function to_u8

public fun to_u8(v: vector<u8>): u8

Function to_u64

public fun to_u64(v: vector<u8>): u64

Function to_u128

public fun to_u128(v: vector<u8>): u128

Function to_address

public fun to_address(v: vector<u8>): address

Function new

Creates a new instance of BCS wrapper that holds inversed bytes for better performance.

public fun new(bytes: vector<u8>): bcs::BCS

Function into_remainder_bytes

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>

Function peel_address

Read address value from the bcs-serialized bytes.

public fun peel_address(bcs: &mut bcs::BCS): address

Function peel_bool

Read a bool value from bcs-serialized bytes.

public fun peel_bool(bcs: &mut bcs::BCS): bool

Function peel_u8

Read u8 value from bcs-serialized bytes.

public fun peel_u8(bcs: &mut bcs::BCS): u8

Function peel_u16

Read u16 value from bcs-serialized bytes.

public fun peel_u16(bcs: &mut bcs::BCS): u16

Function peel_u32

Read u32 value from bcs-serialized bytes.

public fun peel_u32(bcs: &mut bcs::BCS): u32

Function peel_u64

Read u64 value from bcs-serialized bytes.

public fun peel_u64(bcs: &mut bcs::BCS): u64

Function peel_u128

Read u128 value from bcs-serialized bytes.

public fun peel_u128(bcs: &mut bcs::BCS): u128

Function peel_u256

Read u256 value from bcs-serialized bytes.

public fun peel_u256(bcs: &mut bcs::BCS): u256

Function peel_vec_length

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

Function peel_vec_address

Peel a vector of address from serialized bytes.

public fun peel_vec_address(bcs: &mut bcs::BCS): vector<address>

Function peel_vec_bool

Peel a vector of bool from serialized bytes.

public fun peel_vec_bool(bcs: &mut bcs::BCS): vector<bool>

Function peel_vec_u8

Peel a vector of u8 (eg string) from serialized bytes.

public fun peel_vec_u8(bcs: &mut bcs::BCS): vector<u8>

Function peel_vec_vec_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>>

Function peel_vec_u16

Peel a vector of u16 from serialized bytes.

public fun peel_vec_u16(bcs: &mut bcs::BCS): vector<u16>

Function peel_vec_u32

Peel a vector of u32 from serialized bytes.

public fun peel_vec_u32(bcs: &mut bcs::BCS): vector<u32>

Function peel_vec_u64

Peel a vector of u64 from serialized bytes.

public fun peel_vec_u64(bcs: &mut bcs::BCS): vector<u64>

Function peel_vec_u128

Peel a vector of u128 from serialized bytes.

public fun peel_vec_u128(bcs: &mut bcs::BCS): vector<u128>

Function peel_vec_u256

Peel a vector of u256 from serialized bytes.

public fun peel_vec_u256(bcs: &mut bcs::BCS): vector<u256>

Function peel_option_address

Peel Option<address> from serialized bytes.

public fun peel_option_address(bcs: &mut bcs::BCS): option::Option<address>

Function peel_option_bool

Peel Option<bool> from serialized bytes.

public fun peel_option_bool(bcs: &mut bcs::BCS): option::Option<bool>

Function peel_option_u8

Peel Option<u8> from serialized bytes.

public fun peel_option_u8(bcs: &mut bcs::BCS): option::Option<u8>

Function peel_option_u16

Peel Option<u16> from serialized bytes.

public fun peel_option_u16(bcs: &mut bcs::BCS): option::Option<u16>

Function peel_option_u32

Peel Option<u32> from serialized bytes.

public fun peel_option_u32(bcs: &mut bcs::BCS): option::Option<u32>

Function peel_option_u64

Peel Option<u64> from serialized bytes.

public fun peel_option_u64(bcs: &mut bcs::BCS): option::Option<u64>

Function peel_option_u128

Peel Option<u128> from serialized bytes.

public fun peel_option_u128(bcs: &mut bcs::BCS): option::Option<u128>

Function peel_option_u256

Peel Option<u256> from serialized bytes.

public fun peel_option_u256(bcs: &mut bcs::BCS): option::Option<u256>

Function from_bytes

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 from_bytes_option

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>

Function native_from_bytes

public(friend) fun native_from_bytes<T>(bytes: vector<u8>): option::Option<T>