Trait Serve

Source
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 implement Serialize and Deserialize.
  • Resp - The type of the response messages. It must implement Serialize and Deserialize.

§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 an Option 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§

Source

type Req: Serialize + for<'de> Deserialize<'de>

Type of request.

Source

type Resp: Serialize + for<'de> Deserialize<'de>

Type of response.

Required Methods§

Source

fn serve(&mut self, req: Self::Req) -> Result<Self::Resp, IpcError>

Responds to a single request.

Provided Methods§

Source

fn method(&self, _request: &Self::Req) -> Option<&'static str>

Extracts a method name from the request.

Implementors§