Struct BoxCloneService

pub struct BoxCloneService<T, U, E>(/* private field */);

A Clone + Send boxed Service.

BoxCloneService turns a service into a trait object, allowing the response future type to be dynamic, and allowing the service to be cloned.

This is similar to BoxService except the resulting service implements Clone.

Example

use tower::{Service, ServiceBuilder, BoxError, util::BoxCloneService};
use std::time::Duration;
#
# struct Request;
# struct Response;
# impl Response {
#     fn new() -> Self { Self }
# }

// This service has a complex type that is hard to name
let service = ServiceBuilder::new()
    .map_request(|req| {
        println!("received request");
        req
    })
    .map_response(|res| {
        println!("response produced");
        res
    })
    .load_shed()
    .concurrency_limit(64)
    .timeout(Duration::from_secs(10))
    .service_fn(|req: Request| async {
        Ok::<_, BoxError>(Response::new())
    });
# let service = assert_service(service);

// `BoxCloneService` will erase the type so it's nameable
let service: BoxCloneService<Request, Response, BoxError> = BoxCloneService::new(service);
# let service = assert_service(service);

// And we can still clone the service
let cloned_service = service.clone();
#
# fn assert_service<S, R>(svc: S) -> S
# where S: Service<R> { svc }

Implementations

impl<T, U, E> BoxCloneService<T, U, E>

fn new<S>(inner: S) -> Self
where
    S: Service<T, Response = U, Error = E> + Clone + Send + 'static,
    S::Future: Send + 'static,

Create a new BoxCloneService.

fn layer<S>() -> LayerFn<fn(S) -> Self>
where
    S: Service<T, Response = U, Error = E> + Clone + Send + 'static,
    S::Future: Send + 'static,

Returns a Layer for wrapping a Service in a BoxCloneService middleware.

Trait Implementations

impl<T, U, E> Clone for BoxCloneService<T, U, E>

fn clone(&self) -> Self

impl<T, U, E> Debug for BoxCloneService<T, U, E>

fn fmt(&self, fmt: &mut Formatter<'_>) -> Result

impl<T, U, E> Service<T> for BoxCloneService<T, U, E>

type Response = U;
type Error = E;
type Future = Pin<Box<dyn Future<Output = Result<U, E>> + Send>>;
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), E>>
fn call(&mut self, request: T) -> Self::Future

Auto Trait Implementations

impl<T, U, E> !RefUnwindSafe for BoxCloneService<T, U, E>

impl<T, U, E> !Sync for BoxCloneService<T, U, E>

impl<T, U, E> !UnwindSafe for BoxCloneService<T, U, E>

impl<T, U, E> Freeze for BoxCloneService<T, U, E> where Box<dyn CloneService<T, Error = E, Future = Pin<Box<dyn Future<Output = Result<U, E>> + Send>>, Response = U> + Send>: Freeze,

impl<T, U, E> Send for BoxCloneService<T, U, E> where Box<dyn CloneService<T, Error = E, Future = Pin<Box<dyn Future<Output = Result<U, E>> + Send>>, Response = U> + Send>: Send,

impl<T, U, E> Unpin for BoxCloneService<T, U, E> where Box<dyn CloneService<T, Error = E, Future = Pin<Box<dyn Future<Output = Result<U, E>> + Send>>, Response = U> + Send>: Unpin,

impl<T, U, E> UnsafeUnpin for BoxCloneService<T, U, E> where Box<dyn CloneService<T, Error = E, Future = Pin<Box<dyn Future<Output = Result<U, E>> + Send>>, Response = U> + Send>: UnsafeUnpin,

Blanket Implementations

impl<C, Target> MakeConnection<Target> for BoxCloneService<T, U, E> where C: Service<Target>, <C as Service<Target>>::Response: AsyncRead + AsyncWrite,

impl<M, S, Target, Request> MakeService<Target, Request> for BoxCloneService<T, U, E> where M: Service<Target, Response = S>, S: Service<Request>,

impl<T> Any for BoxCloneService<T, U, E> where T: 'static + ?Sized,

impl<T> Borrow<T> for BoxCloneService<T, U, E> where T: ?Sized,

impl<T> BorrowMut<T> for BoxCloneService<T, U, E> where T: ?Sized,

impl<T> CloneToUninit for BoxCloneService<T, U, E> where T: Clone,

impl<T> From<T> for BoxCloneService<T, U, E>

impl<T> Instrument for BoxCloneService<T, U, E>

impl<T> ToOwned for BoxCloneService<T, U, E> where T: Clone,

impl<T> WithSubscriber for BoxCloneService<T, U, E>

impl<T, Request> ServiceExt<Request> for BoxCloneService<T, U, E> where T: Service<Request> + ?Sized,

impl<T, U> Into<U> for BoxCloneService<T, U, E> where U: From<T>,

impl<T, U> TryFrom<U> for BoxCloneService<T, U, E> where U: Into<T>,

impl<T, U> TryInto<U> for BoxCloneService<T, U, E> where U: TryFrom<T>,