Struct BoxService

pub struct BoxService<T, U, E> { /* private fields */ }

A boxed Service + Send trait object.

BoxService turns a service into a trait object, allowing the response future type to be dynamic. This type requires both the service and the response future to be Send.

If you need a boxed Service that implements Clone consider using BoxCloneService.

Dynamically dispatched Service objects allow for erasing the underlying Service type and using the Service instances as opaque handles. This can be useful when the service instance cannot be explicitly named for whatever reason.

Examples

use std::future::ready;
# use tower_service::Service;
# use tower::util::{BoxService, service_fn};
// Respond to requests using a closure, but closures cannot be named...
# pub fn main() {
let svc = service_fn(|mut request: String| {
    request.push_str(" response");
    ready(Ok(request))
});

let service: BoxService<String, String, ()> = BoxService::new(svc);
# drop(service);
}

Implementations

impl<T, U, E> BoxService<T, U, E>

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

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

Trait Implementations

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

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

impl<T, U, E> Service<T> for BoxService<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) -> Pin<Box<dyn Future<Output = Result<U, E>> + Send>>

Auto Trait Implementations

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

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

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

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

impl<T, U, E> Sync for BoxService<T, U, E> where SyncWrapper<Box<dyn Service<T, Response = U, Future = Pin<Box<dyn Future<Output = Result<U, E>> + Send>>, Error = E> + Send>>: Sync,

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

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

Blanket Implementations

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

type Connection = <C as Service<Target>>::Response;
type Error = <C as Service<Target>>::Error;
type Future = <C as Service<Target>>::Future;
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), <C as MakeConnection<Target>>::Error>>
fn make_connection(&mut self, target: Target) -> <C as MakeConnection<Target>>::Future

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

type Response = <S as Service<Request>>::Response;
type Error = <S as Service<Request>>::Error;
type Service = S;
type MakeError = <M as Service<Target>>::Error;
type Future = <M as Service<Target>>::Future;
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), <M as MakeService<Target, Request>>::MakeError>>
fn make_service(&mut self, target: Target) -> <M as MakeService<Target, Request>>::Future

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

fn type_id(&self) -> TypeId

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

fn borrow(&self) -> &T

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

fn borrow_mut(&mut self) -> &mut T

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

fn from(t: T) -> T

Returns the argument unchanged.

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

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

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

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

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

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

type Error = Infallible;
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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

type Error = <U as TryFrom<T>>::Error;
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>