Skip to content

Commit

Permalink
Adds support for tuples in runtime-interface (paritytech#7672)
Browse files Browse the repository at this point in the history
  • Loading branch information
bkchr authored and darkfriend77 committed Jan 11, 2021
1 parent 0098cef commit b3bac76
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 3 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions primitives/runtime-interface/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
4 changes: 3 additions & 1 deletion primitives/runtime-interface/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>;
}

Expand Down
2 changes: 1 addition & 1 deletion primitives/runtime-interface/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
24 changes: 24 additions & 0 deletions primitives/runtime-interface/test-wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down Expand Up @@ -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);
}
}
7 changes: 6 additions & 1 deletion primitives/runtime-interface/test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

0 comments on commit b3bac76

Please sign in to comment.