Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(client,service): completely remove tower-service crate, reimplem…
…ent service::oneshot::Oneshot
- Loading branch information
Showing
9 changed files
with
50 additions
and
134 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,73 +1,34 @@ | ||
// TODO: Eventually to be replaced with tower_util::Oneshot. | ||
|
||
use pin_project_lite::pin_project; | ||
use tower_service::Service; | ||
|
||
use crate::common::{task, Future, Pin, Poll}; | ||
use crate::service::Service; | ||
|
||
pub(crate) fn oneshot<S, Req>(svc: S, req: Req) -> Oneshot<S, Req> | ||
pub(crate) fn oneshot<S, Req, F>(mut svc: S, req: Req) -> Oneshot<F> | ||
where | ||
S: Service<Req>, | ||
S: Service<Req, Future = F>, | ||
{ | ||
Oneshot { | ||
state: State::NotReady { svc, req }, | ||
} | ||
Oneshot { fut: svc.call(req) } | ||
} | ||
|
||
pin_project! { | ||
// A `Future` consuming a `Service` and request, waiting until the `Service` | ||
// is ready, and then calling `Service::call` with the request, and | ||
// waiting for that `Future`. | ||
#[allow(missing_debug_implementations)] | ||
pub struct Oneshot<S: Service<Req>, Req> { | ||
pub struct Oneshot<F> { | ||
#[pin] | ||
state: State<S, Req>, | ||
} | ||
} | ||
|
||
pin_project! { | ||
#[project = StateProj] | ||
#[project_replace = StateProjOwn] | ||
enum State<S: Service<Req>, Req> { | ||
NotReady { | ||
svc: S, | ||
req: Req, | ||
}, | ||
Called { | ||
#[pin] | ||
fut: S::Future, | ||
}, | ||
Tmp, | ||
fut: F, | ||
} | ||
} | ||
|
||
impl<S, Req> Future for Oneshot<S, Req> | ||
impl<F, Response, Error> Future for Oneshot<F> | ||
where | ||
S: Service<Req>, | ||
F: Future<Output = Result<Response, Error>>, | ||
{ | ||
type Output = Result<S::Response, S::Error>; | ||
type Output = Result<Response, Error>; | ||
|
||
fn poll(self: Pin<&mut Self>, cx: &mut task::Context<'_>) -> Poll<Self::Output> { | ||
let mut me = self.project(); | ||
|
||
loop { | ||
match me.state.as_mut().project() { | ||
StateProj::NotReady { ref mut svc, .. } => { | ||
ready!(svc.poll_ready(cx))?; | ||
// fallthrough out of the match's borrow | ||
} | ||
StateProj::Called { fut } => { | ||
return fut.poll(cx); | ||
} | ||
StateProj::Tmp => unreachable!(), | ||
} | ||
|
||
match me.state.as_mut().project_replace(State::Tmp) { | ||
StateProjOwn::NotReady { mut svc, req } => { | ||
me.state.set(State::Called { fut: svc.call(req) }); | ||
} | ||
_ => unreachable!(), | ||
} | ||
} | ||
let me = self.project(); | ||
me.fut.poll(cx) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters