-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[framework] Consolidate conversions (#3516)
This PR introduces as the matching counterpart to `std::bcs` the module `aptos_std::from_bcs`. It seems to be appropriate to tie the byte conversions together under the `bcs` label because that is how we have implemented them. All safe conversions from primitive types + Strings bytes to values are supported. Naming is as in `from_bcs::to_<type>`. Example: ``` use std::bcs; use aptos_std::from_bcs; assert!(from_bcs::to_address(bcs::to_bytes(&@0xabcdef)) == @0xabcdef; ``` Also the friend native `from_bytes` is now defined in `from_bcs` instead of `any`.
- Loading branch information
Showing
15 changed files
with
140 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/// 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 `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); | ||
/// ``` | ||
module aptos_std::from_bcs { | ||
use std::string::{Self, String}; | ||
|
||
/// UTF8 check failed in conversion from bytes to string | ||
const EINVALID_UTF8: u64 = 0x1; | ||
|
||
public fun to_bool(v: vector<u8>): bool { | ||
from_bytes<bool>(v) | ||
} | ||
|
||
public fun to_u8(v: vector<u8>): u8 { | ||
from_bytes<u8>(v) | ||
} | ||
|
||
public fun to_u64(v: vector<u8>): u64 { | ||
from_bytes<u64>(v) | ||
} | ||
|
||
public fun to_u128(v: vector<u8>): u128 { | ||
from_bytes<u128>(v) | ||
} | ||
|
||
public fun to_address(v: vector<u8>): address { | ||
from_bytes<address>(v) | ||
} | ||
|
||
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(string::bytes(&s)), EINVALID_UTF8); | ||
s | ||
} | ||
|
||
/// 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) native fun from_bytes<T>(bytes: vector<u8>): T; | ||
friend aptos_std::any; | ||
friend aptos_std::copyable_any; | ||
|
||
|
||
#[test_only] | ||
use std::bcs::to_bytes; | ||
|
||
#[test] | ||
fun test_address() { | ||
let addr = @0x01; | ||
let addr_vec = x"0000000000000000000000000000000000000000000000000000000000000001"; | ||
let addr_out = to_address(addr_vec); | ||
let addr_vec_out = to_bytes(&addr_out); | ||
assert!(addr == addr_out, 0); | ||
assert!(addr_vec == addr_vec_out, 1); | ||
} | ||
|
||
#[test] | ||
#[expected_failure(abort_code = 0x10001)] | ||
fun test_address_fail() { | ||
let bad_vec = b"01"; | ||
to_address(bad_vec); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.