diff --git a/README.md b/README.md index 4fbc6550..9cab60d4 100644 --- a/README.md +++ b/README.md @@ -92,8 +92,8 @@ details. `WireframeApp` defaults to a simple `Envelope` containing a message ID and raw payload bytes. Applications can supply their own envelope type by calling -`WireframeApp::<_, _, MyEnv>::new_with_envelope()`. The custom type must -implement the `Packet` trait: +`WireframeApp::<_, _, MyEnv>::new()`. The custom type must implement the +`Packet` trait: ```rust use wireframe::app::{Packet, WireframeApp}; @@ -107,7 +107,7 @@ impl Packet for MyEnv { fn from_parts(id: u32, data: Vec) -> Self { Self { id, data } } } -let app = WireframeApp::<_, _, MyEnv>::new_with_envelope() +let app = WireframeApp::<_, _, MyEnv>::new() .unwrap() .route(1, std::sync::Arc::new(|env: &MyEnv| Box::pin(async move { /* ... */ }))) .unwrap(); diff --git a/examples/echo.rs b/examples/echo.rs index 91840076..1791492c 100644 --- a/examples/echo.rs +++ b/examples/echo.rs @@ -5,7 +5,10 @@ use std::io; -use wireframe::{app::WireframeApp, server::WireframeServer}; +use wireframe::{ + app::{Envelope, WireframeApp}, + server::WireframeServer, +}; #[tokio::main] async fn main() -> io::Result<()> { @@ -14,7 +17,7 @@ async fn main() -> io::Result<()> { .unwrap() .route( 1, - std::sync::Arc::new(|_| { + std::sync::Arc::new(|_: &Envelope| { Box::pin(async move { println!("echo request received"); // `WireframeApp` automatically echoes the envelope back. diff --git a/examples/metadata_routing.rs b/examples/metadata_routing.rs index 8fa7e5b1..45459ac5 100644 --- a/examples/metadata_routing.rs +++ b/examples/metadata_routing.rs @@ -67,7 +67,7 @@ async fn main() -> io::Result<()> { .serializer(HeaderSerializer) .route( 1, - Arc::new(|_env| { + Arc::new(|_env: &Envelope| { Box::pin(async move { println!("received ping message"); }) @@ -76,7 +76,7 @@ async fn main() -> io::Result<()> { .unwrap() .route( 2, - Arc::new(|_env| { + Arc::new(|_env: &Envelope| { Box::pin(async move { println!("received pong message"); }) diff --git a/examples/ping_pong.rs b/examples/ping_pong.rs index edde533e..ed39b217 100644 --- a/examples/ping_pong.rs +++ b/examples/ping_pong.rs @@ -130,7 +130,7 @@ impl Transform> for Logging { fn build_app() -> AppResult { WireframeApp::new()? .serializer(BincodeSerializer) - .route(PING_ID, Arc::new(|_| Box::pin(ping_handler())))? + .route(PING_ID, Arc::new(|_: &Envelope| Box::pin(ping_handler())))? .wrap(PongMiddleware)? .wrap(Logging) } diff --git a/src/app.rs b/src/app.rs index 140b8a84..a84df128 100644 --- a/src/app.rs +++ b/src/app.rs @@ -247,27 +247,49 @@ where } } -impl WireframeApp { +impl WireframeApp +where + E: Packet, +{ /// Construct a new empty application builder. /// /// # Errors /// - /// This function currently never returns an error but uses the - /// [`Result`] type for forward compatibility. + /// This function currently never returns an error but uses [`Result`] for + /// forward compatibility. + /// + /// # Examples + /// + /// ``` + /// use wireframe::app::{Packet, WireframeApp}; + /// + /// #[derive(bincode::Encode, bincode::BorrowDecode)] + /// struct MyEnv { + /// id: u32, + /// data: Vec, + /// } + /// + /// impl Packet for MyEnv { + /// fn id(&self) -> u32 { self.id } + /// fn into_parts(self) -> (u32, Vec) { (self.id, self.data) } + /// fn from_parts(id: u32, data: Vec) -> Self { Self { id, data } } + /// } + /// + /// let app = WireframeApp::<_, _, MyEnv>::new().expect("failed to create app"); + /// ``` pub fn new() -> Result { Ok(Self::default()) } -} -impl WireframeApp -where - E: Packet, -{ /// Construct a new application builder using a custom envelope type. /// + /// Deprecated: call [`WireframeApp::new`] with explicit envelope type + /// parameters. + /// /// # Errors /// /// This function currently never returns an error but uses [`Result`] for /// forward compatibility. - pub fn new_with_envelope() -> Result { Ok(Self::default()) } + #[deprecated(note = "use `WireframeApp::<_, _, E>::new()` instead")] + pub fn new_with_envelope() -> Result { Self::new() } } impl WireframeApp diff --git a/tests/lifecycle.rs b/tests/lifecycle.rs index a81068b0..297f3e5f 100644 --- a/tests/lifecycle.rs +++ b/tests/lifecycle.rs @@ -49,7 +49,7 @@ where let setup_cb = call_counting_callback(setup, state); let teardown_cb = call_counting_callback(teardown, ()); - WireframeApp::<_, _, E>::new_with_envelope() + WireframeApp::<_, _, E>::new() .expect("failed to create app") .on_connection_setup(move || setup_cb(())) .expect("setup callback") @@ -74,7 +74,7 @@ async fn setup_without_teardown_runs() { let setup_count = Arc::new(AtomicUsize::new(0)); let cb = call_counting_callback(&setup_count, ()); - let app = WireframeApp::new() + let app = WireframeApp::<_, _, Envelope>::new() .expect("failed to create app") .on_connection_setup(move || cb(())) .expect("setup callback"); @@ -89,7 +89,7 @@ async fn teardown_without_setup_does_not_run() { let teardown_count = Arc::new(AtomicUsize::new(0)); let cb = call_counting_callback(&teardown_count, ()); - let app = WireframeApp::new() + let app = WireframeApp::<_, _, Envelope>::new() .expect("failed to create app") .on_connection_teardown(cb) .expect("teardown callback"); diff --git a/tests/response.rs b/tests/response.rs index 20a5857c..60b9de04 100644 --- a/tests/response.rs +++ b/tests/response.rs @@ -6,7 +6,7 @@ use bytes::BytesMut; use rstest::rstest; use wireframe::{ - app::WireframeApp, + app::{Envelope, WireframeApp}, frame::{Endianness, FrameProcessor, LengthFormat, LengthPrefixedProcessor}, message::Message, serializer::BincodeSerializer, @@ -39,7 +39,7 @@ impl<'de> bincode::BorrowDecode<'de, ()> for FailingResp { /// Tests that sending a response serialises and frames the data correctly, /// and that the response can be decoded and deserialised back to its original value asynchronously. async fn send_response_encodes_and_frames() { - let app = WireframeApp::new() + let app = WireframeApp::<_, _, Envelope>::new() .expect("failed to create app") .frame_processor(LengthPrefixedProcessor::default()) .serializer(BincodeSerializer); @@ -131,7 +131,7 @@ fn custom_length_roundtrip( #[tokio::test] async fn send_response_propagates_write_error() { - let app = WireframeApp::new() + let app = WireframeApp::<_, _, Envelope>::new() .expect("app creation failed") .frame_processor(LengthPrefixedProcessor::default()); @@ -190,7 +190,7 @@ fn encode_fails_for_length_too_large(#[case] fmt: LengthFormat, #[case] len: usi /// This test sends a `FailingResp` using `send_response` and asserts that the resulting /// error is of the `Serialize` variant, indicating a failure during response encoding. async fn send_response_returns_encode_error() { - let app = WireframeApp::new().expect("failed to create app"); + let app = WireframeApp::<_, _, Envelope>::new().expect("failed to create app"); let err = app .send_response(&mut Vec::new(), &FailingResp) .await diff --git a/tests/routes.rs b/tests/routes.rs index dbaa1652..93888cab 100644 --- a/tests/routes.rs +++ b/tests/routes.rs @@ -40,7 +40,7 @@ struct Echo(u8); async fn handler_receives_message_and_echoes_response() { let called = Arc::new(AtomicUsize::new(0)); let called_clone = called.clone(); - let app = WireframeApp::<_, _, TestEnvelope>::new_with_envelope() + let app = WireframeApp::<_, _, TestEnvelope>::new() .expect("failed to create app") .frame_processor(LengthPrefixedProcessor::default()) .route( @@ -79,7 +79,7 @@ async fn handler_receives_message_and_echoes_response() { #[tokio::test] async fn multiple_frames_processed_in_sequence() { - let app = WireframeApp::<_, _, TestEnvelope>::new_with_envelope() + let app = WireframeApp::<_, _, TestEnvelope>::new() .expect("failed to create app") .frame_processor(LengthPrefixedProcessor::default()) .route( diff --git a/tests/wireframe_protocol.rs b/tests/wireframe_protocol.rs index 79f107dc..774cb79e 100644 --- a/tests/wireframe_protocol.rs +++ b/tests/wireframe_protocol.rs @@ -16,7 +16,7 @@ use tokio_util::sync::CancellationToken; use wireframe::{ ConnectionContext, WireframeProtocol, - app::WireframeApp, + app::{Envelope, WireframeApp}, connection::ConnectionActor, push::PushQueues, }; @@ -51,7 +51,7 @@ async fn builder_produces_protocol_hooks() { let protocol = TestProtocol { counter: counter.clone(), }; - let app = WireframeApp::new() + let app = WireframeApp::<_, _, Envelope>::new() .expect("failed to create app") .with_protocol(protocol); let mut hooks = app.protocol_hooks(); @@ -75,7 +75,7 @@ async fn connection_actor_uses_protocol_from_builder() { let protocol = TestProtocol { counter: counter.clone(), }; - let app = WireframeApp::new() + let app = WireframeApp::<_, _, Envelope>::new() .expect("failed to create app") .with_protocol(protocol);