-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: remove tower dep by vendoring Oneshot
- Loading branch information
1 parent
d3e9699
commit 26ea763
Showing
6 changed files
with
87 additions
and
16 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
//! Service utilities. | ||
#[cfg(feature = "service")] | ||
mod glue; | ||
#[cfg(any(feature = "client-legacy", feature = "service"))] | ||
mod oneshot; | ||
|
||
#[cfg(feature = "service")] | ||
pub use self::glue::TowerToHyperService; | ||
#[cfg(any(feature = "client-legacy", feature = "service"))] | ||
pub(crate) use self::oneshot::Oneshot; |
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 |
---|---|---|
@@ -0,0 +1,62 @@ | ||
use pin_project_lite::pin_project; | ||
use std::future::Future; | ||
use std::pin::Pin; | ||
use std::task::{ready, Context, Poll}; | ||
use tower_service::Service; | ||
|
||
// Vendored from tower::util to reduce dependencies, the code is small enough. | ||
|
||
// Not really pub, but used in a trait for bounds | ||
pin_project! { | ||
#[project = OneshotProj] | ||
#[derive(Debug)] | ||
pub enum Oneshot<S: Service<Req>, Req> { | ||
NotReady { | ||
svc: S, | ||
req: Option<Req>, | ||
}, | ||
Called { | ||
#[pin] | ||
fut: S::Future, | ||
}, | ||
Done, | ||
} | ||
} | ||
|
||
impl<S, Req> Oneshot<S, Req> | ||
where | ||
S: Service<Req>, | ||
{ | ||
pub(crate) const fn new(svc: S, req: Req) -> Self { | ||
Oneshot::NotReady { | ||
svc, | ||
req: Some(req), | ||
} | ||
} | ||
} | ||
|
||
impl<S, Req> Future for Oneshot<S, Req> | ||
where | ||
S: Service<Req>, | ||
{ | ||
type Output = Result<S::Response, S::Error>; | ||
|
||
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { | ||
loop { | ||
let this = self.as_mut().project(); | ||
match this { | ||
OneshotProj::NotReady { svc, req } => { | ||
let _ = ready!(svc.poll_ready(cx))?; | ||
let fut = svc.call(req.take().expect("already called")); | ||
self.set(Oneshot::Called { fut }); | ||
} | ||
OneshotProj::Called { fut } => { | ||
let res = ready!(fut.poll(cx))?; | ||
self.set(Oneshot::Done); | ||
return Poll::Ready(Ok(res)); | ||
} | ||
OneshotProj::Done => panic!("polled after complete"), | ||
} | ||
} | ||
} | ||
} |