Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -107,7 +107,7 @@ impl Packet for MyEnv {
fn from_parts(id: u32, data: Vec<u8>) -> 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();
Expand Down
7 changes: 5 additions & 2 deletions examples/echo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<()> {
Expand All @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions examples/metadata_routing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
})
Expand All @@ -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");
})
Expand Down
2 changes: 1 addition & 1 deletion examples/ping_pong.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ impl<E: Packet> Transform<HandlerService<E>> for Logging {
fn build_app() -> AppResult<WireframeApp> {
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)
}
Expand Down
40 changes: 31 additions & 9 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,27 +247,49 @@ where
}
}

impl WireframeApp<BincodeSerializer, (), Envelope> {
impl<E> WireframeApp<BincodeSerializer, (), E>
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<u8>,
/// }
///
/// impl Packet for MyEnv {
/// fn id(&self) -> u32 { self.id }
/// fn into_parts(self) -> (u32, Vec<u8>) { (self.id, self.data) }
/// fn from_parts(id: u32, data: Vec<u8>) -> Self { Self { id, data } }
/// }
///
/// let app = WireframeApp::<_, _, MyEnv>::new().expect("failed to create app");
/// ```
pub fn new() -> Result<Self> { Ok(Self::default()) }
}

impl<E> WireframeApp<BincodeSerializer, (), E>
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<Self> { Ok(Self::default()) }
#[deprecated(note = "use `WireframeApp::<_, _, E>::new()` instead")]
pub fn new_with_envelope() -> Result<Self> { Self::new() }
}

impl<S, C, E> WireframeApp<S, C, E>
Expand Down
6 changes: 3 additions & 3 deletions tests/lifecycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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");
Expand All @@ -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");
Expand Down
8 changes: 4 additions & 4 deletions tests/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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());

Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions tests/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down
6 changes: 3 additions & 3 deletions tests/wireframe_protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use tokio_util::sync::CancellationToken;
use wireframe::{
ConnectionContext,
WireframeProtocol,
app::WireframeApp,
app::{Envelope, WireframeApp},
connection::ConnectionActor,
push::PushQueues,
};
Expand Down Expand Up @@ -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();
Expand All @@ -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);

Expand Down
Loading