diff --git a/Cargo.lock b/Cargo.lock index 36b85c4..ef3fae0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -83,6 +83,7 @@ dependencies = [ name = "bogos-binted-demo" version = "0.1.0" dependencies = [ + "bogos-binted", "tokio", "tower", "tracing", diff --git a/crates/demo/Cargo.toml b/crates/demo/Cargo.toml index dedd9c8..1f5462b 100644 --- a/crates/demo/Cargo.toml +++ b/crates/demo/Cargo.toml @@ -9,3 +9,4 @@ tower = { workspace = true, features = ["util"] } tracing = { workspace = true } tracing-futures = { workspace = true } tracing-subscriber = { workspace = true, features = ["env-filter"] } +bogos-binted = { path = "../lib" } diff --git a/crates/demo/src/main.rs b/crates/demo/src/main.rs index bb2fe3f..e345f1f 100644 --- a/crates/demo/src/main.rs +++ b/crates/demo/src/main.rs @@ -1,9 +1,9 @@ use std::{pin::Pin, task::Poll, time::Duration}; -use tower::{Service, ServiceExt}; +use bogos_binted::ServiceExt as _; +use tower::{Service, ServiceExt as _}; use tracing::{info, info_span}; use tracing_futures::Instrument; -use tracing_subscriber::FmtSubscriber; struct UppercaseService; @@ -19,7 +19,7 @@ impl Service for UppercaseService { type Future = Pin> + Send>>; - fn poll_ready(&mut self, cx: &mut std::task::Context<'_>) -> Poll> { + fn poll_ready(&mut self, _cx: &mut std::task::Context<'_>) -> Poll> { Poll::Ready(Ok(())) } @@ -39,7 +39,7 @@ async fn main() { tracing_subscriber::fmt::init(); info!("initialising"); - let mut svc = UppercaseService; + let mut svc = UppercaseService.bint(); dbg!( svc.ready() diff --git a/crates/lib/src/lib.rs b/crates/lib/src/lib.rs index b93cf3f..df08cbc 100644 --- a/crates/lib/src/lib.rs +++ b/crates/lib/src/lib.rs @@ -1,14 +1,55 @@ -pub fn add(left: u64, right: u64) -> u64 { - left + right +use std::{marker::PhantomData, pin::Pin}; + +use tower::Service; + +pub trait ServiceExt: Service { + fn bint(self) -> Binted + where + Self: Sized; +} + +impl ServiceExt for S +where + S: Service, +{ + fn bint(self) -> Binted { + Binted::new(self) + } +} + +pub struct Binted> { + inner: S, + _pd: PhantomData, } -#[cfg(test)] -mod tests { - use super::*; +impl> Binted { + pub fn new(inner: S) -> Self { + Self { + inner, + _pd: PhantomData, + } + } +} + +impl> Service for Binted +where + S::Future: Send + 'static, +{ + type Response = S::Response; + + type Error = S::Error; + + type Future = + Pin> + Send + 'static>>; + + fn poll_ready( + &mut self, + cx: &mut std::task::Context<'_>, + ) -> std::task::Poll> { + self.inner.poll_ready(cx) + } - #[test] - fn it_works() { - let result = add(2, 2); - assert_eq!(result, 4); + fn call(&mut self, req: I) -> Self::Future { + Box::pin(self.inner.call(req)) } }