1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//! Service trait for defining RPC handlers.
//!
//! A [`Serve`] implementation defines the request/response types
//! and the handler logic for a single RPC service. The handler
//! returns an [`Io`] producing the response, keeping side effects
//! lazy and composable.
//!
//! [`Io`]: comp_cat_rs::effect::io::Io
//!
//! # Examples
//!
//! ```rust,ignore
//! use tarpc_cat::serve::Serve;
//! use tarpc_cat::error::Error;
//! use comp_cat_rs::effect::io::Io;
//! use serde::{Serialize, Deserialize};
//!
//! #[derive(Serialize, Deserialize)]
//! enum MathRequest { Add(i32, i32) }
//!
//! #[derive(Serialize, Deserialize)]
//! enum MathResponse { Sum(i32) }
//!
//! #[derive(Clone)]
//! struct MathService;
//!
//! impl Serve for MathService {
//! type Request = MathRequest;
//! type Response = MathResponse;
//!
//! fn handle(&self, request: MathRequest) -> Io<Error, MathResponse> {
//! match request {
//! MathRequest::Add(a, b) => Io::pure(MathResponse::Sum(a + b)),
//! }
//! }
//! }
//! ```
use Io;
use DeserializeOwned;
use Serialize;
use crateError;
/// An RPC service handler.
///
/// Implementors define [`Request`](Serve::Request) and
/// [`Response`](Serve::Response) types plus a [`handle`](Serve::handle)
/// method that processes a request and returns an [`Io`] producing
/// the response.
///
/// [`Clone`] is required because the server clones the service for
/// each connection thread. [`Send`] and `'static` are required
/// for thread safety.