|
| 1 | +use crate::call::{HandlerCallOptions, Incoming, StreamingRequest, StreamingResponseWriter}; |
| 2 | +use crate::server::method_handler::GenericByteStreamMethodHandler; |
| 3 | +use crate::stream::PushStreamProducer; |
| 4 | +use crate::Status; |
| 5 | +use bytes::Buf; |
| 6 | + |
| 7 | +/// A method handler that processes raw bytes. |
| 8 | +#[trait_variant::make(Send)] |
| 9 | +#[dynosaur::dynosaur(pub DynByteStreamMethodHandler = dyn(box) ByteStreamMethodHandler)] |
| 10 | +pub trait ByteStreamMethodHandler<ReqB, W, P>: Send + Sync { |
| 11 | + type RespB: Buf + Send; |
| 12 | + |
| 13 | + async fn call( |
| 14 | + &self, |
| 15 | + options: HandlerCallOptions, |
| 16 | + req: StreamingRequest<Incoming<ReqB>, P>, |
| 17 | + resp: W, |
| 18 | + ) -> Result<(), Status> |
| 19 | + where |
| 20 | + ReqB: Buf + Send, |
| 21 | + P: PushStreamProducer<Item = Incoming<ReqB>> + Send, |
| 22 | + W: StreamingResponseWriter<Self::RespB>; |
| 23 | +} |
| 24 | + |
| 25 | +/// Adapter to bridge `GenericByteStreamMethodHandler` to `ByteStreamMethodHandler`. |
| 26 | +pub struct GenericByteStreamAdapter<T>(pub T); |
| 27 | + |
| 28 | +impl<T, ReqB, W, P> ByteStreamMethodHandler<ReqB, W, P> for GenericByteStreamAdapter<T> |
| 29 | +where |
| 30 | + T: GenericByteStreamMethodHandler, |
| 31 | + W: StreamingResponseWriter<T::RespB> + Send, |
| 32 | +{ |
| 33 | + type RespB = T::RespB; |
| 34 | + |
| 35 | + async fn call( |
| 36 | + &self, |
| 37 | + options: HandlerCallOptions, |
| 38 | + req: StreamingRequest<Incoming<ReqB>, P>, |
| 39 | + resp: W, |
| 40 | + ) -> Result<(), Status> |
| 41 | + where |
| 42 | + ReqB: Buf + Send, |
| 43 | + W: StreamingResponseWriter<Self::RespB>, |
| 44 | + P: PushStreamProducer<Item = Incoming<ReqB>> + Send, |
| 45 | + { |
| 46 | + <T as GenericByteStreamMethodHandler>::call(&self.0, options, req, resp).await |
| 47 | + } |
| 48 | +} |
0 commit comments