starcoin-framework

Module 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);

Constants

UTF8 check failed in conversion from bytes to string

const EINVALID_UTF8: u64 = 1;

Function to_bool

public fun to_bool(v: vector<u8>): bool
Implementation
public fun to_bool(v: vector<u8>): bool {
    from_bytes<bool>(v)
}

Function to_u8

public fun to_u8(v: vector<u8>): u8
Implementation
public fun to_u8(v: vector<u8>): u8 {
    from_bytes<u8>(v)
}

Function to_u64

public fun to_u64(v: vector<u8>): u64
Implementation
public fun to_u64(v: vector<u8>): u64 {
    from_bytes<u64>(v)
}

Function to_u128

public fun to_u128(v: vector<u8>): u128
Implementation
public fun to_u128(v: vector<u8>): u128 {
    from_bytes<u128>(v)
}

Function to_address

public fun to_address(v: vector<u8>): address
Implementation
public fun to_address(v: vector<u8>): address {
    from_bytes<address>(v)
}

Function 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
Implementation
public(friend) native fun from_bytes<T>(bytes: vector<u8>): T;
Specification
pragma opaque;