diff --git a/README.md b/README.md index 2ae2a02..7ac2e8e 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,6 @@ See [examples](examples/) for usage. ```rust use bytes::Bytes; use futures::{SinkExt, StreamExt}; -use raknet_rs::client::{self, ConnectTo}; use raknet_rs::server::{self, MakeIncoming}; let socket = tokio::net::UdpSocket::bind("127.0.0.1:0").await?; @@ -47,7 +46,6 @@ io.send(data).await.unwrap(); use bytes::Bytes; use futures::{SinkExt, StreamExt}; use raknet_rs::client::{self, ConnectTo}; -use raknet_rs::server::{self, MakeIncoming}; let socket = tokio::net::UdpSocket::bind("0.0.0.0:0").await?; let config = client::ConfigBuilder::default() diff --git a/src/lib.rs b/src/lib.rs index a413ac9..dbfb0f1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -66,9 +66,6 @@ pub mod server; /// Raknet client pub mod client; -/// Service -pub mod service; - /// Unit tests #[cfg(test)] mod tests; diff --git a/src/service/mod.rs b/src/service/mod.rs deleted file mode 100644 index 73cc48a..0000000 --- a/src/service/mod.rs +++ /dev/null @@ -1,80 +0,0 @@ -/// Utils function for service building -mod service_fn; - -pub use service_fn::service_fn; - -/// Tower's `Service` is a highly valuable tool for application development. Nevertheless, it -/// appears somewhat intricate when applied to Raknet services. Here, I've made a streamlined and -/// swifter alternative to the tower's `Service`, which enable the `async_fn_in_trait` feature for -/// static dispatching `Service::call`. -/// -/// Context can be omitted within a `Service` or provided by a global variable with a static -/// lifetime. This capability allows for greater flexibility. -pub trait Service { - /// Error made by this service - type Error; - - /// Response made by this service - type Response; - - /// Process the request asynchronously. - /// Set `where Self: Sized` to allow creating `dyn Service` - #[allow(async_fn_in_trait)] // No need to consider the auto trait for now. - async fn call(&self, req: Request) -> Result - where - Self: Sized; -} - -#[cfg(test)] -mod test { - use futures::executor::block_on; - - use super::*; - - struct EchoService; - - impl Service for EchoService { - type Error = String; - type Response = String; - - async fn call(&self, req: String) -> Result { - Ok(req) - } - } - - struct DummyMiddleware { - good_smell: u64, - inner: S, - } - - impl Service for DummyMiddleware - where - S: Service, - { - type Error = S::Error; - type Response = S::Response; - - async fn call(&self, req: String) -> Result { - self.inner.call(format!("{}-{req}", self.good_smell)).await - } - } - - #[test] - fn echo_service_test() { - let echo = EchoService; - let fut = echo.call("request".to_string()); - let resp = block_on(fut); - assert_eq!(resp, Ok("request".to_string())); - } - - #[test] - fn dummy_middleware_test() { - let svc = DummyMiddleware { - good_smell: 114514, - inner: EchoService, - }; - let fut = svc.call("request".to_string()); - let resp = block_on(fut); - assert_eq!(resp, Ok("114514-request".to_string())); - } -} diff --git a/src/service/service_fn.rs b/src/service/service_fn.rs deleted file mode 100644 index f4d1cfe..0000000 --- a/src/service/service_fn.rs +++ /dev/null @@ -1,48 +0,0 @@ -use std::fmt::{Debug, Formatter}; -use std::future::Future; - -#[cfg(test)] -use futures::executor::block_on; - -use crate::service::Service; - -/// Create a service from an async function -pub const fn service_fn(f: F) -> ServiceFn { - ServiceFn { f } -} - -/// Service hold an async function -#[derive(Clone, Copy)] -pub struct ServiceFn { - /// inner - f: F, -} - -impl Debug for ServiceFn { - fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { - write!(f, "ServiceFn") - } -} - -impl Service for ServiceFn -where - F: Fn(Req) -> Fut, - Fut: Future>, -{ - type Error = Err; - type Response = Res; - - async fn call(&self, req: Req) -> Result { - (self.f)(req).await - } -} - -#[cfg(test)] -#[test] -fn service_fn_should_work() { - let req = String::from("request"); - let svc_fn = |_: &str| async { Err::<&str, _>("not available") }; - let svc = service_fn(svc_fn); - let resp = block_on(svc.call(req.as_str())); - assert_eq!(resp, Err("not available")); -}