Response

Enum Response 

Source
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 None and 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>

Source

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)
}
Source

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]);

Trait Implementations§

Source§

impl<F: Debug, E> Debug for Response<F, E>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<F, E> From<F> for Response<F, E>

Source§

fn from(f: F) -> Self

Converts to this type from the input type.
Source§

impl<F, E> From<Vec<F>> for Response<F, E>

Source§

fn from(v: Vec<F>) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<F, E> Freeze for Response<F, E>
where F: Freeze,

§

impl<F, E = ()> !RefUnwindSafe for Response<F, E>

§

impl<F, E> Send for Response<F, E>
where F: Send,

§

impl<F, E = ()> !Sync for Response<F, E>

§

impl<F, E> Unpin for Response<F, E>
where F: Unpin,

§

impl<F, E = ()> !UnwindSafe for Response<F, E>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<!> for T

Source§

fn from(t: !) -> T

Converts to this type from the input type.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more