pub trait RequestHandler {
// Required methods
fn start<TRequest, TResponse>(
handle_fn: fn(TRequest) -> Result<TResponse, Box<dyn Error>>,
) -> Self
where TRequest: RpcRequest + 'static + DeserializeOwned + Serialize,
TResponse: Serialize + 'static;
fn stop(&self);
}
Expand description
This trait is used to handle rpc requests using meshpulse
Required Methods§
Sourcefn start<TRequest, TResponse>(
handle_fn: fn(TRequest) -> Result<TResponse, Box<dyn Error>>,
) -> Selfwhere
TRequest: RpcRequest + 'static + DeserializeOwned + Serialize,
TResponse: Serialize + 'static,
fn start<TRequest, TResponse>(
handle_fn: fn(TRequest) -> Result<TResponse, Box<dyn Error>>,
) -> Selfwhere
TRequest: RpcRequest + 'static + DeserializeOwned + Serialize,
TResponse: Serialize + 'static,
Starts a new request handler with the given handle function The handle function should take a request and return a result The request type should implement RpcRequest, serde::de::DeserializeOwned and serde::Serialize The response type should implement serde::Serialize
§Example
use meshpulse::prelude::*;
use serde::{Serialize, Deserialize};
std::env::set_var("MQTT_USERNAME", "test");
std::env::set_var("MQTT_PASSWORD", "test");
std::env::set_var("MQTT_HOST", "tcp://localhost:1883");
#[derive(Serialize, Deserialize, RpcRequest)]
struct TestRpcRequest(String);
fn handle_request(request: TestRpcRequest) -> Result<String, Box<dyn std::error::Error>> {
Ok("World".to_string())
}
let handler = RpcRequestHandler::start(handle_request);
// ... when you no longer need the handler
handler.stop();
Sourcefn stop(&self)
fn stop(&self)
Stops the request handler
§Example
use meshpulse::prelude::*;
use serde::{Serialize, Deserialize};
std::env::set_var("MQTT_USERNAME", "test");
std::env::set_var("MQTT_PASSWORD", "test");
std::env::set_var("MQTT_HOST", "tcp://localhost:1883");
#[derive(Serialize, Deserialize, RpcRequest)]
struct MultiplierRequest(i32);
fn handle_request(request: MultiplierRequest) -> Result<i32, Box<dyn std::error::Error>> {
Ok(request.0 * 2)
}
let handler = RpcRequestHandler::start(handle_request);
// ... when you no longer need the handler
handler.stop();
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.
Implementors§
impl RequestHandler for RpcRequestHandler
This struct is used to handle rpc requests using mqtt It is used to start and stop the rpc request handler