-
Notifications
You must be signed in to change notification settings - Fork 286
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
builder,util: add convenience methods for boxing services #616
Conversation
This adds a couple of new methods to `ServiceBuilder` and `ServiceExt`: - `ServiceBuilder::boxed` - `ServiceExt::boxed` - `ServiceBuilder::clone_boxed` - `ServiceExt::clone_boxed` They apply `BoxService::layer` and `CloneBoxService::layer` respectively.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I commented on some docs nits and stuff, but I'm mostly good with this. It would be nice to fix the wrong syntax in links before merging, though.
tower_layer::LayerFn< | ||
fn( | ||
L::Service, | ||
) -> crate::util::BoxService< | ||
R, | ||
<L::Service as Service<R>>::Response, | ||
<L::Service as Service<R>>::Error, | ||
>, | ||
>, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeesh...i feel like it's almost worth introducing a named Layer
type for BoxService
just to avoid having this type signature...but, I think that might be a breaking change...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I went down that route initially actually but you end up having to have the request type as a type param on the layer:
// have to have `T` here otherwise its unconstrained in the layer impl
pub struct CloneBoxServiceLayer<T> {
_request: PhantomData<T>,
}
impl<S, T> Layer<S> for CloneBoxServiceLayer<T>
where
S: Service<T> + Clone + Send + 'static,
S::Future: Send + 'static,
{
type Service = CloneBoxService<T, S::Response, S::Error>;
fn layer(&self, inner: S) -> Self::Service {
CloneBoxService::new(inner)
}
}
That could be workable to leads to inference issues when actually using the builder method:
---- src/builder/mod.rs - builder::ServiceBuilder<L>::clone_boxed (line 754) stdout ----
error[E0284]: type annotations needed
--> src/builder/mod.rs:765:6
|
14 | .clone_boxed()
| ^^^^^^^^^^^ cannot infer type for type parameter `S`
|
= note: cannot satisfy `<_ as Service<Request>>::Future == _`
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ugh, never mind. it's probably better to avoid introducing phantomdata anyway, as they will result in rustc having to typecheck a larger type, potentially impacting build time... this is fine, then.
/// [`CloneBoxService:layer()`]: crate::util::CloneBoxService::layer() | ||
#[cfg(feature = "util")] | ||
#[cfg_attr(docsrs, doc(cfg(feature = "util")))] | ||
pub fn clone_boxed<S, R>( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TIOLI but I might have called this boxed_clone
? although on the other hand, this isn't consistent with the name of the return type...idk.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right I went with this name because it matches the service name, which we can rename since its not released. CloneBoxService
or BoxCloneService
seem kinda same same to me though.
} | ||
|
||
/// Convert the resulting service into a [`Service`] + [`Clone`] + [`Send`] trait object. | ||
/// |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit, take it or leave it: maybe worth adding a note comparing this with box_service
, like I mentioned on the ServiceExt
methods? not a blocker...
tower_layer::LayerFn< | ||
fn( | ||
L::Service, | ||
) -> crate::util::CloneBoxService< | ||
R, | ||
<L::Service as Service<R>>::Response, | ||
<L::Service as Service<R>>::Error, | ||
>, | ||
>, | ||
L, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
gosh... since the CloneBoxService::layer()
method hasn't been released yet, we could potentially replace the LayerFn
with a named Layer
type. IDK if it's worth it just to get rid of this type signature, but...wow.
Co-authored-by: Eliza Weisman <[email protected]>
Co-authored-by: Eliza Weisman <[email protected]>
tower_layer::LayerFn< | ||
fn( | ||
L::Service, | ||
) -> crate::util::BoxService< | ||
R, | ||
<L::Service as Service<R>>::Response, | ||
<L::Service as Service<R>>::Error, | ||
>, | ||
>, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ugh, never mind. it's probably better to avoid introducing phantomdata anyway, as they will result in rustc having to typecheck a larger type, potentially impacting build time... this is fine, then.
Added - **util**: Add `CloneBoxService` which is a `Clone + Send` boxed `Service` ([#615]) - **util**: Add `ServiceExt::boxed` and `ServiceExt::clone_boxed` for applying the `BoxService` and `CloneBoxService` middleware ([#616]) - **builder**: Add `ServiceBuilder::boxed` and `ServiceBuilder::clone_boxed` for applying `BoxService` and `CloneBoxService` layers ([#616]) Fixed - **balance**: Remove redundant `Req: Clone` bound from `Clone` impls for `MakeBalance`, and `MakeBalanceLayer` ([#607]) - **balance**: Remove redundant `Req: Debug` bound from `Debug` impls for `MakeBalance`, `MakeFuture`, `Balance`, and `Pool` ([#607]) - **ready-cache**: Remove redundant `Req: Debug` bound from `Debug` impl for `ReadyCache` ([#607]) - **steer**: Remove redundant `Req: Debug` bound from `Debug` impl for `Steer` ([#607]) - **util**: Remove redundant `F: Clone` bound from `ServiceExt::map_request` ([#607]) - **docs**: Fix `doc(cfg(...))` attributes of `PeakEwmaDiscover`, and `PendingRequestsDiscover` ([#610]) - **util**: Remove unnecessary `Debug` bounds from `impl Debug for BoxService` ([#617]) - **util**: Remove unnecessary `Debug` bounds from `impl Debug for UnsyncBoxService` ([#617]) [#607]: #607 [#610]: #610 [#616]: #616 [#617]: #617 [#615]: #615
Added - **util**: Add `CloneBoxService` which is a `Clone + Send` boxed `Service` ([#615]) - **util**: Add `ServiceExt::boxed` and `ServiceExt::clone_boxed` for applying the `BoxService` and `CloneBoxService` middleware ([#616]) - **builder**: Add `ServiceBuilder::boxed` and `ServiceBuilder::clone_boxed` for applying `BoxService` and `CloneBoxService` layers ([#616]) Fixed - **balance**: Remove redundant `Req: Clone` bound from `Clone` impls for `MakeBalance`, and `MakeBalanceLayer` ([#607]) - **balance**: Remove redundant `Req: Debug` bound from `Debug` impls for `MakeBalance`, `MakeFuture`, `Balance`, and `Pool` ([#607]) - **ready-cache**: Remove redundant `Req: Debug` bound from `Debug` impl for `ReadyCache` ([#607]) - **steer**: Remove redundant `Req: Debug` bound from `Debug` impl for `Steer` ([#607]) - **util**: Remove redundant `F: Clone` bound from `ServiceExt::map_request` ([#607]) - **docs**: Fix `doc(cfg(...))` attributes of `PeakEwmaDiscover`, and `PendingRequestsDiscover` ([#610]) - **util**: Remove unnecessary `Debug` bounds from `impl Debug for BoxService` ([#617]) - **util**: Remove unnecessary `Debug` bounds from `impl Debug for UnsyncBoxService` ([#617]) [#607]: #607 [#610]: #610 [#616]: #616 [#617]: #617 [#615]: #615
* tower: prepare to release 0.4.11 Added - **util**: Add `CloneBoxService` which is a `Clone + Send` boxed `Service` ([#615]) - **util**: Add `ServiceExt::boxed` and `ServiceExt::clone_boxed` for applying the `BoxService` and `CloneBoxService` middleware ([#616]) - **builder**: Add `ServiceBuilder::boxed` and `ServiceBuilder::clone_boxed` for applying `BoxService` and `CloneBoxService` layers ([#616]) Fixed - **balance**: Remove redundant `Req: Clone` bound from `Clone` impls for `MakeBalance`, and `MakeBalanceLayer` ([#607]) - **balance**: Remove redundant `Req: Debug` bound from `Debug` impls for `MakeBalance`, `MakeFuture`, `Balance`, and `Pool` ([#607]) - **ready-cache**: Remove redundant `Req: Debug` bound from `Debug` impl for `ReadyCache` ([#607]) - **steer**: Remove redundant `Req: Debug` bound from `Debug` impl for `Steer` ([#607]) - **util**: Remove redundant `F: Clone` bound from `ServiceExt::map_request` ([#607]) - **docs**: Fix `doc(cfg(...))` attributes of `PeakEwmaDiscover`, and `PendingRequestsDiscover` ([#610]) - **util**: Remove unnecessary `Debug` bounds from `impl Debug for BoxService` ([#617]) - **util**: Remove unnecessary `Debug` bounds from `impl Debug for UnsyncBoxService` ([#617]) [#607]: #607 [#610]: #610 [#616]: #616 [#617]: #617 [#615]: #615 * sorting * Rename `CloneBoxService` to `BoxCloneService` * formatting * also update changelog
This adds a couple of new methods to
ServiceBuilder
andServiceExt
:ServiceBuilder::boxed
ServiceExt::boxed
ServiceBuilder::clone_boxed
ServiceExt::clone_boxed