apache_dubbo/triple/server/
service.rs

1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements.  See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18use futures_util::{Future, Stream};
19use tower_service::Service;
20
21use crate::invocation::{Request, Response};
22use crate::triple::decode::Decoding;
23
24pub trait StreamingSvc<R> {
25    type Response;
26
27    type ResponseStream: Stream<Item = Result<Self::Response, crate::status::Status>>;
28
29    type Future: Future<Output = Result<Response<Self::ResponseStream>, crate::status::Status>>;
30
31    fn call(&mut self, req: Request<Decoding<R>>) -> Self::Future;
32}
33
34impl<T, S, M1, M2> StreamingSvc<M1> for T
35where
36    T: Service<Request<Decoding<M1>>, Response = Response<S>, Error = crate::status::Status>,
37    S: Stream<Item = Result<M2, crate::status::Status>>,
38{
39    type Response = M2;
40
41    type ResponseStream = S;
42
43    type Future = T::Future;
44
45    fn call(&mut self, req: Request<Decoding<M1>>) -> Self::Future {
46        Service::call(self, req)
47    }
48}
49
50pub trait UnarySvc<R> {
51    type Response;
52    type Future: Future<Output = Result<Response<Self::Response>, crate::status::Status>>;
53
54    fn call(&mut self, req: Request<R>) -> Self::Future;
55}
56
57impl<T, M1, M2> UnarySvc<M1> for T
58where
59    T: Service<Request<M1>, Response = Response<M2>, Error = crate::status::Status>,
60{
61    type Response = M2;
62
63    type Future = T::Future;
64
65    fn call(&mut self, req: Request<M1>) -> Self::Future {
66        T::call(self, req)
67    }
68}
69
70pub trait ClientStreamingSvc<R> {
71    type Response;
72    type Future: Future<Output = Result<Response<Self::Response>, crate::status::Status>>;
73
74    fn call(&mut self, req: Request<Decoding<R>>) -> Self::Future;
75}
76
77impl<T, M1, M2> ClientStreamingSvc<M1> for T
78where
79    T: Service<Request<Decoding<M1>>, Response = Response<M2>, Error = crate::status::Status>,
80{
81    type Response = M2;
82
83    type Future = T::Future;
84
85    fn call(&mut self, req: Request<Decoding<M1>>) -> Self::Future {
86        T::call(self, req)
87    }
88}
89
90pub trait ServerStreamingSvc<R> {
91    type Response;
92
93    type ResponseStream: Stream<Item = Result<Self::Response, crate::status::Status>>;
94
95    type Future: Future<Output = Result<Response<Self::ResponseStream>, crate::status::Status>>;
96
97    fn call(&mut self, req: Request<R>) -> Self::Future;
98}
99
100impl<T, S, M1, M2> ServerStreamingSvc<M1> for T
101where
102    T: Service<Request<M1>, Response = Response<S>, Error = crate::status::Status>,
103    S: Stream<Item = Result<M2, crate::status::Status>>,
104{
105    type Response = M2;
106
107    type ResponseStream = S;
108
109    type Future = T::Future;
110
111    fn call(&mut self, req: Request<M1>) -> Self::Future {
112        Service::call(self, req)
113    }
114}