From 48bc0e52e2e261d7992f3070c8fabe689f8d555e Mon Sep 17 00:00:00 2001 From: vms Date: Sat, 22 Aug 2020 00:07:57 +0300 Subject: [PATCH] introduce CallParameters --- crates/macro/Cargo.toml | 4 ++ crates/main/Cargo.toml | 4 ++ crates/main/src/call_parameters.rs | 47 +++++++++++++++++++ crates/main/src/lib.rs | 4 ++ crates/wit/Cargo.toml | 4 ++ .../record_generator.rs | 16 ++++++- src/lib.rs | 2 + 7 files changed, 79 insertions(+), 2 deletions(-) create mode 100644 crates/main/src/call_parameters.rs diff --git a/crates/macro/Cargo.toml b/crates/macro/Cargo.toml index 2688df7..261bf93 100644 --- a/crates/macro/Cargo.toml +++ b/crates/macro/Cargo.toml @@ -18,3 +18,7 @@ proc-macro = true [dependencies] fluence-sdk-wit = { path = "../wit", version = "=0.2.0" } + +[features] +# Indicates that this crate is included to the Fluence Rust sdk (it affects internal path adjusting) +fce = ["fluence-sdk-wit/fce"] diff --git a/crates/main/Cargo.toml b/crates/main/Cargo.toml index 1d20f11..0a8d454 100644 --- a/crates/main/Cargo.toml +++ b/crates/main/Cargo.toml @@ -19,6 +19,7 @@ crate-type = ["rlib"] [dependencies] log = { version = "0.4.8", features = ["std"] } +fluence-sdk-macro = { path = "../macro" } [dev-dependencies] simple_logger = "1.6.0" # used in doc test @@ -30,3 +31,6 @@ debug = [] # Enable logger (this will cause log_utf8_string to appear in imports) logger = [] + +# Indicates that this crate is included to the Fluence Rust sdk (it affects internal path adjusting) +fce = ["fluence-sdk-macro/fce"] diff --git a/crates/main/src/call_parameters.rs b/crates/main/src/call_parameters.rs new file mode 100644 index 0000000..c17a807 --- /dev/null +++ b/crates/main/src/call_parameters.rs @@ -0,0 +1,47 @@ +/* + * Copyright 2020 Fluence Labs Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +use fluence_sdk_macro::fce; + +/// This struct contains parameters that would be accessible by Wasm modules. +#[fce] +#[derive(Clone, PartialEq, Default, Eq, Debug)] +pub struct CallParameters { + pub call_id: String, + pub user_name: String, +} + +impl CallParameters { + pub fn new(call_id: C, user_name: U) -> Self + where + C: Into, + U: Into, + { + Self { + call_id: call_id.into(), + user_name: user_name.into(), + } + } +} + +#[cfg(not(feature = "fce"))] +#[fce] +#[link(wasm_import_module = "host")] +#[allow(improper_ctypes)] +extern "C" { + // returns current call parameters + pub fn get_call_parameters() -> CallParameters; +} diff --git a/crates/main/src/lib.rs b/crates/main/src/lib.rs index d2c8a4c..c4b4926 100644 --- a/crates/main/src/lib.rs +++ b/crates/main/src/lib.rs @@ -31,11 +31,15 @@ )] #![warn(rust_2018_idioms)] +mod call_parameters; mod export_allocator; #[cfg(any(feature = "debug", feature = "logger"))] mod logger; mod result; +pub use call_parameters::CallParameters; +#[cfg(not(feature = "fce"))] +pub use call_parameters::get_call_parameters; pub use export_allocator::allocate; pub use export_allocator::deallocate; #[cfg(feature = "logger")] diff --git a/crates/wit/Cargo.toml b/crates/wit/Cargo.toml index 021f8f3..3177866 100644 --- a/crates/wit/Cargo.toml +++ b/crates/wit/Cargo.toml @@ -20,3 +20,7 @@ serde = { version = "1.0.110", features = ["derive"] } serde_json = "1.0.56" syn = { version = '1.0.33', features = ['full'] } uuid = { version = "0.8.1", features = ["v4"] } + +[features] +# Indicates that this crate is included to the Fluence Rust sdk (it affects internal path adjusting) +fce = [] diff --git a/crates/wit/src/token_stream_generator/record_generator.rs b/crates/wit/src/token_stream_generator/record_generator.rs index ea071ec..b5e2de8 100644 --- a/crates/wit/src/token_stream_generator/record_generator.rs +++ b/crates/wit/src/token_stream_generator/record_generator.rs @@ -40,13 +40,25 @@ impl quote::ToTokens for fce_ast_types::AstRecordItem { let serializer_fn = generate_serializer_fn(self); let deserializer_fn = generate_deserializer_fn(self); + #[cfg(feature = "fce")] + let trait_path = || { + quote::quote! { crate::FCEStructSerializable } + }; + + #[cfg(not(feature = "fce"))] + let trait_path = || { + quote::quote! { fluence::internal::FCEStructSerializable } + }; + + let trait_path = trait_path(); + let glue_code = quote::quote! { #original - #[cfg(target_arch = "wasm32")] + #[cfg(any(target_arch = "wasm32", feature = "fce"))] #[doc(hidden)] #[allow(clippy::all)] - impl fluence::internal::FCEStructSerializable for #record_name { + impl #trait_path for #record_name { #serializer_fn #deserializer_fn diff --git a/src/lib.rs b/src/lib.rs index da4aceb..1fe278f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -71,6 +71,8 @@ extern crate fluence_sdk_macro; extern crate fluence_sdk_main; pub use fluence_sdk_macro::fce; +pub use fluence_sdk_main::CallParameters; +pub use fluence_sdk_main::get_call_parameters; #[cfg(feature = "logger")] pub use fluence_sdk_main::WasmLogger;