Add combinators for working with Services#104
Add combinators for working with Services#104carllerche merged 8 commits intotower-rs:masterfrom fafhrd91:service-ext
Conversation
hawkw
left a comment
There was a problem hiding this comment.
This seems right at a glance, so far. I think there's some code here that can be simplified with try_ready!
tower-util/src/ext/then.rs
Outdated
| type Future = ThenFuture<A, B>; | ||
|
|
||
| fn poll_ready(&mut self) -> Poll<(), Self::Error> { | ||
| match self.a.poll_ready() { |
There was a problem hiding this comment.
Take it or leave it: I think this could just be
let _ = try_ready!(self.a.poll_ready());
self.b.poll_ready()
tower-util/src/ext/map.rs
Outdated
| type Error = T::Error; | ||
|
|
||
| fn poll(&mut self) -> Poll<Self::Item, Self::Error> { | ||
| match self.fut.poll()? { |
There was a problem hiding this comment.
This could just be
let resp = try_ready!(self.fut.poll());
Ok(Async::Ready((self.f)(resp)))
tower-util/src/ext/apply.rs
Outdated
| pub struct Apply<T, F, R, Req> { | ||
| service: T, | ||
| f: F, | ||
| r: PhantomData<Fn(Req) -> R>, |
There was a problem hiding this comment.
Nit: since this field isn't used, it should probably have a leading _.
There was a problem hiding this comment.
I also think only the request is needed as a generic on the struct?
There was a problem hiding this comment.
i am not sure, we need F which is Fn(Req, T) -> IntoFuture. do i miss anything?
tower-util/src/ext/and_then.rs
Outdated
| return fut.poll(); | ||
| } | ||
|
|
||
| match self.fut_a.poll() { |
There was a problem hiding this comment.
This can just be
let resp = try_ready!(self.fut_a.poll());
self.fut_b = Some(self.b.call(resp));
self.poll()
tower-util/src/ext/and_then.rs
Outdated
| type Future = AndThenFuture<A, B>; | ||
|
|
||
| fn poll_ready(&mut self) -> Poll<(), Self::Error> { | ||
| match self.a.poll_ready() { |
There was a problem hiding this comment.
This could be:
let _ = try_ready!(self.a.poll_ready());
self.b.poll_ready()
carllerche
left a comment
There was a problem hiding this comment.
Looking good. I left some thoughts inline. Looking forward to the rest.
tower-util/src/ext/and_then.rs
Outdated
| impl<A, B> AndThen<A, B> | ||
| where | ||
| A: Service, | ||
| A::Error: Into<B::Error>, |
There was a problem hiding this comment.
I would recommend against the Into strategy for matching up error types. This is considered a futures 0.1 mistake (the issue doesn't have much discussion...). The problem is it creates a bunch of type inference issues.
The strategy is to either require both services to have the same error type or make the combinator's error Either<A, B>. In this case, I believe it makes most sense to require thee same error type. The user would be able to map the error w/ map_err.
There was a problem hiding this comment.
just figured out inference problems.
tower-util/src/ext/apply.rs
Outdated
| pub struct Apply<T, F, R, Req> { | ||
| service: T, | ||
| f: F, | ||
| r: PhantomData<Fn(Req) -> R>, |
There was a problem hiding this comment.
I also think only the request is needed as a generic on the struct?
tower-util/src/ext/map_err.rs
Outdated
| pub struct MapErr<T, F, E> { | ||
| service: T, | ||
| f: F, | ||
| e: PhantomData<E>, |
There was a problem hiding this comment.
Is this PhantomData required?
| impl<T, F, E> MapErr<T, F, E> | ||
| where | ||
| T: Service, | ||
| F: Fn(T::Error) -> E + Clone, |
There was a problem hiding this comment.
Did Clone on closures land in Rust stable?
There was a problem hiding this comment.
tower-util/src/ext/mod.rs
Outdated
| fn and_then<B, U>(self, service: U) -> AndThen<Self, B> | ||
| where | ||
| Self: Sized, | ||
| U: Into<B>, |
There was a problem hiding this comment.
I don't understand why the B service is being converted here.
There was a problem hiding this comment.
this would be useful for #69, but i found that From/Into does not work well with type inference.
we need to use custom IntoService trait. i can remove conversion or implement IntoService, it is up to you?
There was a problem hiding this comment.
Yeah, I think we should limit Into / From bounds due to type inference issues.
Let's remove the conversion for now.
Separately, I think IntoService will helpful at some point, but lets punt for this PR so that we can get it merged.
carllerche
left a comment
There was a problem hiding this comment.
Thanks 👍 LGTM. I'll probably play around w/ these combinators some after merging.
Is this ready to merge?
|
Yes, it is ready |
|
Thanks 👍 |
Uh oh!
There was an error while loading. Please reload this page.