0x1::FromBCS
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 offerred. If
a general conversion back-and-force is needed, consider the StarcoinFramework::Any
type which preserves invariants.
Example:
use std::bcs;
use StarcoinFramework::from_bcs;
assert!(from_bcs::to_address(bcs::to_bytes(&@0xabcdef)) == @0xabcdef, 0);
to_bool
to_u8
to_u64
to_u128
to_address
from_bytes
UTF8 check failed in conversion from bytes to string
const EINVALID_UTF8: u64 = 1;
to_bool
public fun to_bool(v: vector<u8>): bool
public fun to_bool(v: vector<u8>): bool {
from_bytes<bool>(v)
}
to_u8
public fun to_u8(v: vector<u8>): u8
public fun to_u8(v: vector<u8>): u8 {
from_bytes<u8>(v)
}
to_u64
public fun to_u64(v: vector<u8>): u64
public fun to_u64(v: vector<u8>): u64 {
from_bytes<u64>(v)
}
to_u128
public fun to_u128(v: vector<u8>): u128
public fun to_u128(v: vector<u8>): u128 {
from_bytes<u128>(v)
}
to_address
public fun to_address(v: vector<u8>): address
public fun to_address(v: vector<u8>): address {
from_bytes<address>(v)
}
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.
public(friend) fun from_bytes<T>(bytes: vector<u8>): T
public(friend) native fun from_bytes<T>(bytes: vector<u8>): T;
pragma opaque;