from_bcs - [mainnet]
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 that it is not safe to define a generic public from_bytes
function because this can violate implicit struct invariants, therefore only primitive types are offered. If
a general conversion back-and-force is needed, consider the aptos_std::Any
type which preserves invariants.
Example:
use std::bcs;use aptos_std::from_bcs;
assert!(from_bcs::to_address(bcs::to_bytes(&@0xabcdef)) == @0xabcdef, 0);
use 0x1::string;
Constants
UTF8 check failed in conversion from bytes to string
const EINVALID_UTF8: u64 = 1;
Functions
to_bool
public fun to_bool(v: vector<u8>): bool
Implementation
public fun to_bool(v: vector<u8>): bool { from_bytes<bool>(v)}
to_u8
public fun to_u8(v: vector<u8>): u8
Implementation
public fun to_u8(v: vector<u8>): u8 { from_bytes<u8>(v)}
to_u16
public fun to_u16(v: vector<u8>): u16
Implementation
public fun to_u16(v: vector<u8>): u16 { from_bytes<u16>(v)}
to_u32
public fun to_u32(v: vector<u8>): u32
Implementation
public fun to_u32(v: vector<u8>): u32 { from_bytes<u32>(v)}
to_u64
public fun to_u64(v: vector<u8>): u64
Implementation
public fun to_u64(v: vector<u8>): u64 { from_bytes<u64>(v)}
to_u128
public fun to_u128(v: vector<u8>): u128
Implementation
public fun to_u128(v: vector<u8>): u128 { from_bytes<u128>(v)}
to_u256
public fun to_u256(v: vector<u8>): u256
Implementation
public fun to_u256(v: vector<u8>): u256 { from_bytes<u256>(v)}
to_address
public fun to_address(v: vector<u8>): address
Implementation
public fun to_address(v: vector<u8>): address { from_bytes<address>(v)}
to_bytes
public fun to_bytes(v: vector<u8>): vector<u8>
Implementation
public fun to_bytes(v: vector<u8>): vector<u8> { from_bytes<vector<u8>>(v)}
to_string
public fun to_string(v: vector<u8>): string::String
Implementation
public fun to_string(v: vector<u8>): String { // To make this safe, we need to evaluate the utf8 invariant. let s = from_bytes<String>(v); assert!(string::internal_check_utf8(s.bytes()), EINVALID_UTF8); s}
from_bytes
Package private native function to deserialize a type T.
Note that this function does not put any constraint on T
. If code uses this function to
deserialize a linear value, its their responsibility that the data they deserialize is
owned.
Function would abort if T has signer in it.
public(friend) fun from_bytes<T>(bytes: vector<u8>): T
Implementation
native friend fun from_bytes<T>(bytes: vector<u8>): T;
Specification
fun deserialize<T>(bytes: vector<u8>): T;
fun deserializable<T>(bytes: vector<u8>): bool;axiom<T> forall b1: vector<u8>, b2: vector<u8>: ( b1 == b2 ==> deserializable<T>(b1) == deserializable<T>(b2) );axiom<T> forall b1: vector<u8>, b2: vector<u8>: ( b1 == b2 ==> deserialize<T>(b1) == deserialize<T>(b2) );
from_bytes
public(friend) fun from_bytes<T>(bytes: vector<u8>): T
pragma opaque;aborts_if !deserializable<T>(bytes);ensures result == deserialize<T>(bytes);