pub trait Serve {
type Req: Serialize + for<'de> Deserialize<'de>;
type Resp: Serialize + for<'de> Deserialize<'de>;
// Required method
fn serve(&mut self, req: Self::Req) -> Result<Self::Resp, IpcError>;
// Provided method
fn method(&self, _request: &Self::Req) -> Option<&'static str> { ... }
}
Expand description
The Serve
trait defines the interface for handling requests and generating responses in an IPC context.
Types implementing this trait can be used to process incoming requests and produce appropriate responses.
§Associated Types
Req
- The type of the request messages. It must implementSerialize
andDeserialize
.Resp
- The type of the response messages. It must implementSerialize
andDeserialize
.
§Required Methods
serve
- This method is responsible for processing a single request and generating a response.method
- This method extracts a method name from the request, if applicable. It returns anOption
containing a static string slice representing the method name.
§Example
ⓘ
use ckb_script_ipc_common::ipc::Serve;
use serde::{Serialize, Deserialize};
#[derive(Serialize, Deserialize)]
struct MyRequest {
// request fields
}
#[derive(Serialize, Deserialize)]
struct MyResponse {
// response fields
}
struct MyService;
impl Serve for MyService {
type Req = MyRequest;
type Resp = MyResponse;
fn serve(&mut self, req: Self::Req) -> Result<Self::Resp, IpcError> {
// process the request and generate a response
Ok(MyResponse { /* fields */ })
}
fn method(&self, _request: &Self::Req) -> Option<&'static str> {
Some("my_method")
}
}
Required Associated Types§
Sourcetype Req: Serialize + for<'de> Deserialize<'de>
type Req: Serialize + for<'de> Deserialize<'de>
Type of request.
Sourcetype Resp: Serialize + for<'de> Deserialize<'de>
type Resp: Serialize + for<'de> Deserialize<'de>
Type of response.