apache_dubbo/utils/
boxed.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 tower::ServiceExt;
19use tower_layer::{layer_fn, LayerFn};
20use tower_service::Service;
21
22use std::fmt;
23use std::{
24    future::Future,
25    pin::Pin,
26    task::{Context, Poll},
27};
28
29/// A boxed `Service + Send` trait object.
30///
31/// [`BoxService`] turns a service into a trait object, allowing the response
32/// future type to be dynamic. This type requires both the service and the
33/// response future to be [`Send`].
34///
35/// If you need a boxed [`Service`] that implements [`Clone`] consider using
36/// [`BoxCloneService`](crate::util::BoxCloneService).
37///
38/// See module level documentation for more details.
39pub struct BoxService<T, U, E> {
40    inner: Box<dyn Service<T, Response = U, Error = E, Future = BoxFuture<U, E>> + Send + Sync>,
41}
42
43/// A boxed `Future + Send` trait object.
44///
45/// This type alias represents a boxed future that is [`Send`] and can be moved
46/// across threads.
47type BoxFuture<T, E> = Pin<Box<dyn Future<Output = Result<T, E>> + Send>>;
48
49impl<T, U, E> BoxService<T, U, E> {
50    #[allow(missing_docs)]
51    pub fn new<S>(inner: S) -> Self
52    where
53        S: Service<T, Response = U, Error = E> + Send + Sync + 'static,
54        S::Future: Send + 'static,
55    {
56        let inner = Box::new(inner.map_future(|f: S::Future| Box::pin(f) as _));
57        BoxService { inner }
58    }
59
60    // /// Returns a [`Layer`] for wrapping a [`Service`] in a [`BoxService`]
61    // /// middleware.
62    // ///
63    // /// [`Layer`]: crate::Layer
64    pub fn layer<S>() -> LayerFn<fn(S) -> Self>
65    where
66        S: Service<T, Response = U, Error = E> + Send + Sync + 'static,
67        S::Future: Send + 'static,
68    {
69        layer_fn(Self::new)
70    }
71}
72
73impl<T, U, E> Service<T> for BoxService<T, U, E> {
74    type Response = U;
75    type Error = E;
76    type Future = BoxFuture<U, E>;
77
78    fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), E>> {
79        self.inner.poll_ready(cx)
80    }
81
82    fn call(&mut self, request: T) -> BoxFuture<U, E> {
83        self.inner.call(request)
84    }
85}
86
87impl<T, U, E> fmt::Debug for BoxService<T, U, E> {
88    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
89        fmt.debug_struct("BoxService").finish()
90    }
91}