pub enum Response<F, E = ()> {
Single(F),
Vec(Vec<F>),
Stream(FrameStream<F, E>),
MultiPacket(Receiver<F>),
Empty,
}Expand description
Represents the full response to a request.
Variants§
Single(F)
A single frame reply.
Vec(Vec<F>)
An optimized list of frames.
Stream(FrameStream<F, E>)
A potentially unbounded stream of frames.
MultiPacket(Receiver<F>)
Frames delivered through a channel.
§Usage and lifecycle
MultiPacket wraps a tokio::sync::mpsc::Receiver that yields frames
(F) sent from another task. The receiver should be polled until it
returns None, signalling the channel has closed and no more frames will
be sent. Frames are yielded in send order.
Back-pressure follows the channel’s capacity: senders await when it is
full. Multiple senders may be cloned from the original Sender.
The stream ends once all senders are dropped and recv returns
None.
§Resource management
To avoid resource leaks or deadlocks:
- Drop the sender once all frames are sent.
- Poll the receiver to completion, consuming all frames.
- Drop the receiver to cancel outstanding sends; subsequent sends fail.
- If the sender is dropped early, the receiver yields
Noneand no further frames will arrive.
§Examples
use tokio::sync::mpsc;
use wireframe::Response;
async fn demo() {
let (tx, rx) = mpsc::channel(1);
tx.send(1u8).await.expect("send");
drop(tx); // close sender
if let Response::MultiPacket(mut rx) = Response::MultiPacket(rx) {
while let Some(f) = rx.recv().await {
assert_eq!(f, 1);
}
}
}Empty
A response with no frames.
Implementations§
Source§impl<F: Send + 'static, E: Send + 'static> Response<F, E>
impl<F: Send + 'static, E: Send + 'static> Response<F, E>
Sourcepub fn with_channel(capacity: usize) -> (Sender<F>, Response<F, E>)
pub fn with_channel(capacity: usize) -> (Sender<F>, Response<F, E>)
Construct a bounded channel and wrap its receiver in a
Response::MultiPacket.
Returns the sending half of the channel alongside the response so a
handler can spawn background tasks that stream frames. The channel is
bounded: once capacity frames are buffered, additional send
operations await until the connection actor drains the queue.
§Panics
Panics if capacity is zero. This mirrors the behaviour of
tokio::sync::mpsc::channel.
§Examples
use tokio::spawn;
use wireframe::Response;
async fn stream_frames() -> (tokio::sync::mpsc::Sender<u8>, Response<u8>) {
let (sender, response) = Response::with_channel(8);
let mut producer = sender.clone();
spawn(async move {
for frame in 0..3u8 {
if producer.send(frame).await.is_err() {
return;
}
}
drop(producer);
});
(sender, response)
}Sourcepub fn into_stream(self) -> FrameStream<F, E>
pub fn into_stream(self) -> FrameStream<F, E>
Convert this response into a stream of frames.
Response::Vec with no frames and Response::Empty produce an empty
stream.
§Examples
use futures::TryStreamExt;
use wireframe::Response;
let (tx, rx) = tokio::sync::mpsc::channel(1);
tx.send(1u8).await.expect("send");
drop(tx);
let resp: Response<u8, ()> = Response::MultiPacket(rx);
let frames: Vec<u8> = resp
.into_stream()
.try_collect()
.await
.expect("stream error");
assert_eq!(frames, vec![1]);