Calling functions in external crates directly from handle _request function. #107
-
Hi team, In the example, when the Can i call any functions from any of the external crates, something like below let rust_response = if layered.is_empty() {
RustResponse::default()
} else if layered[0] == "serial" {
if layered[1] == "getPorts" {
// sample_functions::get_ports(rust_request).await;
serial_communication::get_ports(rust_request).await;
} else {
RustResponse::default()
}
} else {
RustResponse::default()
}; native\serial_communication\src\lib.rs use std::collections::HashMap;
// use crate::bridge::api::RustOperation;
// use crate::bridge::api::RustRequest;
// use crate::bridge::api::RustResponse;
// use crate::bridge::api::RustSignal;
// use crate::bridge::send_rust_signal;
use rmp_serde::from_slice;
use rmp_serde::to_vec_named;
use serde::Deserialize;
use serde::Serialize;
use crate::bridge::api::{RustRequest, RustResponse};
pub async fn get_ports(rust_request: RustRequest) -> RustResponse {
match rust_request.operation {
RustOperation::Create => RustResponse::default(),
RustOperation::Read => {
#[derive(Serialize)]
struct RustResponseSchema {
ports: HashMap<String, String>,
}
let mut ports = HashMap::new();
ports.insert(String::from("RUST COM1"), String::from("Description of RUST COM1"));
ports.insert(String::from("RUST COM2"), String::from("Description of RUST COM2"));
RustResponse {
successful: true,
bytes: to_vec_named(&RustResponseSchema {
ports: ports
})
.unwrap(),
}
}
RustOperation::Update => RustResponse::default(),
RustOperation::Delete => RustResponse::default(),
}
} native\serial_communication\Cargo.toml [package]
name = "serial_communication"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
bytemuck = "1.11.0"
lazy_static = "1.4.0"
tokio = { version = "1.28.2", features = ["sync", "macros"] }
serde = { version = "1.0.164", features = ["derive"] }
rmp-serde = "1.1.1"
native\hub\Cargo.toml [package]
# Do not change the name of this crate.
name = "hub"
version = "0.1.0"
edition = "2021"
[lib]
# `lib` is required for non-library targets,
# such as tests and benchmarks.
# `staticlib` is for iOS and macOS.
# `cdylib` is for all other platforms.
crate-type = ["lib", "cdylib", "staticlib"]
# These are dependencies for non-web platforms.
[target.'cfg(not(target_family = "wasm"))'.dependencies]
libc = "0.2"
dart-sys = { version = "4.0.2" }
allo-isolate = { version = "0.1.18", features = ["zero-copy"] }
tokio = { version = "1.28.2", features = ["rt-multi-thread", "time"] }
os-thread-local = "0.1.3"
# These are dependencies for the web.
[target.'cfg(target_family = "wasm")'.dependencies]
wasm-bindgen = { version = "0.2.87" }
wasm-bindgen-futures = "0.4.37"
js-sys = "0.3.64"
web-sys = { version = "0.3.64", features = [
"DedicatedWorkerGlobalScope",
"MessagePort",
"Blob",
"BlobPropertyBag",
"Worker",
"Url",
"BroadcastChannel",
"console",
] }
wasmtimer = "0.2.0"
[dependencies]
bytemuck = "1.11.0"
lazy_static = "1.4.0"
tokio = { version = "1.28.2", features = ["sync", "macros"] }
serde = { version = "1.0.164", features = ["derive"] }
rmp-serde = "1.1.1"
sample_crate = { path = "../sample_crate" }
serial_communication = { path = "../serial_communication" }
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
It would be nice if this was possible, but since This is basically because 'When crate A depends on crate B, crate B cannot depend on crate A'. So in your case, you can call functions like: Hope this helps :) |
Beta Was this translation helpful? Give feedback.
It would be nice if this was possible, but since
RustResponse
struct is defined in thehub
crate, all request handlers that has type annotations of thisRustResponse
struct should be defined inhub
, not other crates.This is basically because 'When crate A depends on crate B, crate B cannot depend on crate A'.
So in your case, you can call functions like:
handle_request
>>custom_handler_function_in_hub
>>custom_function_in_other_crate
instead of:
handle_request
>>custom_function_in_other_crate
Hope this helps :)