Struct BoxService

struct BoxService<T, U, E> { ... }

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 futures_util::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 as >::Future: Send + 'static
fn layer<S>() -> LayerFn<fn(_: S) -> Self>
where
    S: Service<T, Response = U, Error = E> + Send + 'static,
    <S as >::Future: Send + 'static

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

impl<M, S, Target, Request> MakeService for BoxService<T, U, E>

fn poll_ready(self: &mut Self, cx: &mut Context<'_>) -> Poll<Result<(), <M as MakeService<Target, Request>>::MakeError>>
fn make_service(self: &mut Self, target: Target) -> <M as MakeService<Target, Request>>::Future

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

fn type_id(self: &Self) -> TypeId

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

fn borrow(self: &Self) -> &T

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

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

impl<T> From 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 for BoxService<T, U, E>

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

fn into(self: 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 for BoxService<T, U, E>

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

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

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

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

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

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

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

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

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

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

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

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

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

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