shuttle_warp/
lib.rs

1#![doc = include_str!("../README.md")]
2use shuttle_runtime::Error;
3use std::net::SocketAddr;
4use std::ops::Deref;
5
6pub use warp;
7
8/// A wrapper type for [warp::Filter] so we can implement [shuttle_runtime::Service] for it.
9pub struct WarpService<T>(pub T);
10
11#[shuttle_runtime::async_trait]
12impl<T> shuttle_runtime::Service for WarpService<T>
13where
14    T: Send + Sync + Clone + 'static + warp::Filter,
15    T::Extract: warp::reply::Reply,
16{
17    /// Takes the router that is returned by the user in their [shuttle_runtime::main] function
18    /// and binds to an address passed in by shuttle.
19    async fn bind(mut self, addr: SocketAddr) -> Result<(), Error> {
20        warp::serve((*self).clone()).run(addr).await;
21        Ok(())
22    }
23}
24
25impl<T> From<T> for WarpService<T>
26where
27    T: Send + Sync + Clone + 'static + warp::Filter,
28    T::Extract: warp::reply::Reply,
29{
30    fn from(router: T) -> Self {
31        Self(router)
32    }
33}
34
35impl<T> Deref for WarpService<T> {
36    type Target = T;
37
38    fn deref(&self) -> &Self::Target {
39        &self.0
40    }
41}
42
43#[doc = include_str!("../README.md")]
44pub type ShuttleWarp<T> = Result<WarpService<warp::filters::BoxedFilter<T>>, Error>;