monolake_core/thrift/mod.rs
1//! Thrift protocol handling for asynchronous services.
2//!
3//! This module provides traits and types for implementing Thrift handlers
4//! that can be used with asynchronous services. It defines a common interface
5//! for processing Thrift requests and generating responses, with support for
6//! context-aware handling.
7//!
8//! # Key Components
9//!
10//! - [`ThriftHandler`]: A trait for implementing Thrift request handlers.
11//! - [`ThriftRequest`]: A type alias for Thrift requests using TTHeader protocol.
12//! - [`ThriftResponse`]: A type alias for Thrift responses using TTHeader protocol.
13//! - [`ThriftBody`]: A type alias for the payload of Thrift requests and responses.
14
15use std::future::Future;
16
17use monoio_thrift::codec::ttheader::TTHeaderPayload;
18use service_async::Service;
19
20use crate::sealed::SealedT;
21
22/// Type alias for the Thrift request/response body.
23///
24/// Currently uses `bytes::Bytes` for efficient memory management.
25/// TODO: Support discontinuous memory in the future.
26pub type ThriftBody = bytes::Bytes;
27
28/// Type alias for a Thrift request using TTHeader protocol.
29pub type ThriftRequest<T> = TTHeaderPayload<T>;
30
31/// Type alias for a Thrift response using TTHeader protocol.
32pub type ThriftResponse<T> = TTHeaderPayload<T>;
33
34struct ThriftSeal;
35
36/// A trait for Thrift request handlers.
37///
38/// This trait defines the interface for processing Thrift requests and generating responses.
39/// It is designed to work with asynchronous services and supports context-aware handling.
40///
41/// # Type Parameters
42///
43/// - `CX`: The context type for additional request processing information.
44///
45/// # Associated Types
46///
47/// - `Error`: The error type that may occur during request handling.
48///
49/// # Examples
50///
51/// ```ignore
52/// use your_crate::{ThriftHandler, ThriftRequest, ThriftResponse, ThriftBody};
53///
54/// struct MyThriftHandler;
55///
56/// impl ThriftHandler<()> for MyThriftHandler {
57/// type Error = std::io::Error;
58///
59/// async fn handle(&self, request: ThriftRequest<ThriftBody>, ctx: ())
60/// -> Result<ThriftResponse<ThriftBody>, Self::Error> {
61/// // Process the Thrift request and generate a response
62/// let response = ThriftResponse::new(/* ... */);
63/// Ok(response)
64/// }
65/// }
66/// ```
67///
68/// The `ThriftHandler` trait is automatically implemented for types that implement the `Service`
69/// trait with request type `(ThriftRequest<ThriftBody>, CX)` and response type
70/// `ThriftResponse<ThriftBody>`.
71#[allow(private_bounds)]
72pub trait ThriftHandler<CX>: SealedT<ThriftSeal, CX> {
73 type Error;
74
75 fn handle(
76 &self,
77 request: ThriftRequest<ThriftBody>,
78 ctx: CX,
79 ) -> impl Future<Output = Result<ThriftResponse<ThriftBody>, Self::Error>>;
80}
81
82impl<T, CX> SealedT<ThriftSeal, CX> for T where
83 T: Service<(ThriftRequest<ThriftBody>, CX), Response = ThriftResponse<ThriftBody>>
84{
85}
86
87impl<T, CX> ThriftHandler<CX> for T
88where
89 T: Service<(ThriftRequest<ThriftBody>, CX), Response = ThriftResponse<ThriftBody>>,
90{
91 type Error = T::Error;
92
93 async fn handle(
94 &self,
95 req: ThriftRequest<ThriftBody>,
96 ctx: CX,
97 ) -> Result<ThriftResponse<ThriftBody>, Self::Error> {
98 self.call((req, ctx)).await
99 }
100}