Adds support for tuples in runtime-interface (#7672)

This commit is contained in:
Bastian Köcher
2020-12-06 15:14:19 +01:00
committed by GitHub
parent 4b8cee9e55
commit d9b1b14769
6 changed files with 36 additions and 3 deletions
@@ -23,6 +23,7 @@ codec = { package = "parity-scale-codec", version = "1.3.1", default-features =
static_assertions = "1.0.0"
primitive-types = { version = "0.7.0", default-features = false }
sp-storage = { version = "2.0.0", default-features = false, path = "../storage" }
impl-trait-for-tuples = "0.1.3"
[dev-dependencies]
sp-runtime-interface-test-wasm = { version = "2.0.0", path = "test-wasm" }
@@ -365,7 +365,9 @@ impl<T: codec::Codec> PassBy for Option<T> {
type PassBy = Codec<Self>;
}
impl PassBy for (u32, u32, u32, u32) {
#[impl_trait_for_tuples::impl_for_tuples(30)]
#[tuple_types_no_default_trait_bound]
impl PassBy for Tuple where Self: codec::Codec {
type PassBy = Codec<Self>;
}
@@ -99,7 +99,7 @@
//! | `*const T` | `u32` | `Identity` |
//! | `Option<T>` | `u64` | `let e = v.encode();`<br><br><code>e.len() 32bit << 32 &#124; e.as_ptr() 32bit</code> |
//! | [`T where T: PassBy<PassBy=Inner>`](./pass_by#Inner) | Depends on inner | Depends on inner |
//! | [`T where T:PassBy<PassBy=Codec>`](./pass_by#Codec)|`u64`|<code>v.len() 32bit << 32 &#124;v.as_ptr() 32bit</code>|
//! | [`T where T: PassBy<PassBy=Codec>`](./pass_by#Codec)|`u64`|<code>v.len() 32bit << 32 &#124;v.as_ptr() 32bit</code>|
//!
//! `Identity` means that the value is converted directly into the corresponding FFI type.
@@ -120,6 +120,16 @@ pub trait TestApi {
fn test_versionning(&self, data: u32) -> bool {
data == 42
}
/// Returns the input values as tuple.
fn return_input_as_tuple(
a: Vec<u8>,
b: u32,
c: Option<Vec<u32>>,
d: u8,
) -> (Vec<u8>, u32, Option<Vec<u32>>, u8) {
(a, b, c, d)
}
}
/// This function is not used, but we require it for the compiler to include `sp-io`.
@@ -258,4 +268,18 @@ wasm_export_functions! {
assert!(!test_api::test_versionning(50));
assert!(!test_api::test_versionning(102));
}
fn test_return_input_as_tuple() {
let a = vec![1, 3, 4, 5];
let b = 10000;
let c = Some(vec![2, 3]);
let d = 5;
let res = test_api::return_input_as_tuple(a.clone(), b, c.clone(), d);
assert_eq!(a, res.0);
assert_eq!(b, res.1);
assert_eq!(c, res.2);
assert_eq!(d, res.3);
}
}
@@ -208,4 +208,9 @@ fn test_tracing() {
let inner = subscriber.0.lock().unwrap();
assert!(inner.spans.contains("return_input_version_1"));
}
}
#[test]
fn test_return_input_as_tuple() {
call_wasm_method::<HostFunctions>(&wasm_binary_unwrap()[..], "test_return_input_as_tuple");
}