From df5774e76c1da8efcf7e10ee82cc9bc4ebee64a3 Mon Sep 17 00:00:00 2001 From: David Pedersen Date: Thu, 25 Feb 2021 22:33:35 +0100 Subject: [PATCH] builder: Add `ServiceBuilder::layer_fn` (#560) This gem as just [sitting in Tonic][sit] waiting to be upstreamed. [sit]: https://github.com/hyperium/tonic/blob/master/tonic/src/transport/service/layer.rs Co-authored-by: Eliza Weisman --- tower/CHANGELOG.md | 1 + tower/src/builder/mod.rs | 9 +++++++++ 2 files changed, 10 insertions(+) diff --git a/tower/CHANGELOG.md b/tower/CHANGELOG.md index 1e7497e..ca97e67 100644 --- a/tower/CHANGELOG.md +++ b/tower/CHANGELOG.md @@ -6,6 +6,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 # Unreleased +- **builder**: Add `ServiceBuilder::layer_fn` to add a layer built from a function. - **builder**: Add `ServiceBuilder::map_future` for transforming the futures produced by a service. - **util**: Add example for `service_fn`. diff --git a/tower/src/builder/mod.rs b/tower/src/builder/mod.rs index 20b1a36..f733861 100644 --- a/tower/src/builder/mod.rs +++ b/tower/src/builder/mod.rs @@ -159,6 +159,15 @@ impl ServiceBuilder { self.layer(crate::util::option_layer(layer)) } + /// Add a [`Layer`] built from a function that accepts a service and returns another service. + /// + /// See the documentation for [`layer_fn`] for more details. + /// + /// [`layer_fn`]: crate::layer::layer_fn + pub fn layer_fn(self, f: F) -> ServiceBuilder, L>> { + self.layer(crate::layer::layer_fn(f)) + } + /// Buffer requests when when the next layer is not ready. /// /// This wraps the inner service with an instance of the [`Buffer`]