Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

util: Add BoxService layer helpers #503

Merged
merged 7 commits into from
Dec 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions tower/src/util/boxed/sync.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use tower_layer::{layer_fn, LayerFn};
use tower_service::Service;

use std::fmt;
Expand Down Expand Up @@ -39,6 +40,15 @@ impl<T, U, E> BoxService<T, U, E> {
let inner = Box::new(Boxed { inner });
BoxService { inner }
}

/// Returns a [`Layer`] for wrapping a [`Service`] in a `BoxService` middleware.
pub fn layer<S>() -> LayerFn<fn(S) -> Self>
where
S: Service<T, Response = U, Error = E> + Send + 'static,
S::Future: Send + 'static,
{
layer_fn(Self::new)
}
}

impl<T, U, E> Service<T> for BoxService<T, U, E> {
Expand Down
10 changes: 10 additions & 0 deletions tower/src/util/boxed/unsync.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use tower_layer::{layer_fn, LayerFn};
use tower_service::Service;

use std::fmt;
Expand Down Expand Up @@ -33,6 +34,15 @@ impl<T, U, E> UnsyncBoxService<T, U, E> {
let inner = Box::new(UnsyncBoxed { inner });
UnsyncBoxService { inner }
}

/// Returns a [`Layer`] for wrapping a [`Service`] in an `UnsyncBoxService` middleware.
pub fn layer<S>() -> LayerFn<fn(S) -> Self>
where
S: Service<T, Response = U, Error = E> + 'static,
S::Future: 'static,
{
layer_fn(Self::new)
}
}

impl<T, U, E> Service<T> for UnsyncBoxService<T, U, E> {
Expand Down