Skip to content

Commit

Permalink
Add new ServiceExt API for DX
Browse files Browse the repository at this point in the history
  • Loading branch information
TTWNO committed Jun 18, 2024
1 parent beacfe2 commit 55edb3d
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
3 changes: 3 additions & 0 deletions odilia/src/tower/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ pub mod choice;
pub mod from_state;
pub mod handler;
pub mod iter_svc;
pub mod service_ext;
pub mod service_set;
pub mod state_svc;
pub mod sync_try;
pub mod unwrap_svc;
pub use handler::Handler;
pub use service_ext::ServiceExt;

pub mod handlers;
pub use handlers::*;
44 changes: 44 additions & 0 deletions odilia/src/tower/service_ext.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use crate::tower::{
async_try::{AsyncTryInto, AsyncTryIntoLayer, AsyncTryIntoService},
iter_svc::IterService,
state_svc::{StateLayer, StateService},
sync_try::{TryIntoLayer, TryIntoService},
unwrap_svc::UnwrapService,
};
use std::{convert::Infallible, future::Future, sync::Arc};
use tower::{util::MapResult, Layer, Service, ServiceExt as TowerServiceExt};

pub trait ServiceExt<Request>: Service<Request> {
fn request_try_from<I, R, Fut1>(self) -> TryIntoService<Request, I, Self, R, Fut1>
where
Self: Sized,
I: TryInto<Request>,
Self: Service<Request, Response = R, Future = Fut1>,
{
TryIntoLayer::new().layer(self)
}
fn request_async_try_from<I, R, Fut1>(
self,
) -> AsyncTryIntoService<Request, I, Self, R, Fut1>
where
I: AsyncTryInto<Request>,
Self: Service<Request, Response = R, Future = Fut1> + Clone,
{
AsyncTryIntoLayer::new().layer(self)
}
fn with_state<S>(self, s: Arc<S>) -> StateService<Self, S>
where
Self: Sized,
{
StateLayer::new(s).layer(self)
}
fn unwrap_map<R, E, F>(self, f: F) -> UnwrapService<Self, Request, Self::Response, R, E, F>
where
Self: Service<Request, Error = Infallible> + Sized,
F: FnOnce(<Self as Service<Request>>::Response) -> Result<R, E>,
{
UnwrapService::new(self, f)
}
}

impl<T: ?Sized, Request> ServiceExt<Request> for T where T: Service<Request> {}

0 comments on commit 55edb3d

Please sign in to comment.