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

builder,util: add convenience methods for boxing services #616

Merged
merged 14 commits into from
Nov 18, 2021

Conversation

davidpdrsn
Copy link
Member

This adds a couple of new methods to ServiceBuilder and ServiceExt:

  • ServiceBuilder::boxed
  • ServiceExt::boxed
  • ServiceBuilder::clone_boxed
  • ServiceExt::clone_boxed

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.
@davidpdrsn davidpdrsn marked this pull request as ready for review November 11, 2021 09:46
@davidpdrsn davidpdrsn requested review from hawkw and olix0r November 11, 2021 09:47
Copy link
Member

@hawkw hawkw left a 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.

Comment on lines +728 to +736
tower_layer::LayerFn<
fn(
L::Service,
) -> crate::util::BoxService<
R,
<L::Service as Service<R>>::Response,
<L::Service as Service<R>>::Error,
>,
>,
Copy link
Member

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...

Copy link
Member Author

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 == _`

Copy link
Member

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>(
Copy link
Member

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.

Copy link
Member Author

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.

tower/src/util/mod.rs Show resolved Hide resolved
tower/src/util/mod.rs Show resolved Hide resolved
tower/src/builder/mod.rs Outdated Show resolved Hide resolved
tower/src/builder/mod.rs Outdated Show resolved Hide resolved
tower/src/builder/mod.rs Outdated Show resolved Hide resolved
}

/// Convert the resulting service into a [`Service`] + [`Clone`] + [`Send`] trait object.
///
Copy link
Member

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...

Comment on lines +787 to +796
tower_layer::LayerFn<
fn(
L::Service,
) -> crate::util::CloneBoxService<
R,
<L::Service as Service<R>>::Response,
<L::Service as Service<R>>::Error,
>,
>,
L,
Copy link
Member

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.

tower/CHANGELOG.md Outdated Show resolved Hide resolved
@davidpdrsn davidpdrsn requested a review from hawkw November 17, 2021 17:47
Comment on lines +728 to +736
tower_layer::LayerFn<
fn(
L::Service,
) -> crate::util::BoxService<
R,
<L::Service as Service<R>>::Response,
<L::Service as Service<R>>::Error,
>,
>,
Copy link
Member

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.

@davidpdrsn davidpdrsn merged commit 4d80f7e into master Nov 18, 2021
@davidpdrsn davidpdrsn deleted the david/boxed-helper-methods branch November 18, 2021 12:56
davidpdrsn added a commit that referenced this pull request Nov 18, 2021
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
davidpdrsn added a commit that referenced this pull request Nov 18, 2021
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
davidpdrsn added a commit that referenced this pull request Nov 18, 2021
* 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants