Skip to main content

Sender

Struct Sender 

Source
pub struct Sender<D, E = Infallible> { /* private fields */ }
Available on crate feature channel only.
Expand description

A sender half created through Channel::new.

Implementations§

Source§

impl<D, E> Sender<D, E>

Source

pub async fn send(&mut self, frame: Frame<D>) -> Result<(), SendError>

Send a frame on the channel.

Source

pub async fn send_data(&mut self, buf: D) -> Result<(), SendError>

Send data on data channel.

Source

pub async fn send_trailers( &mut self, trailers: HeaderMap, ) -> Result<(), SendError>

Send trailers on trailers channel.

Source

pub fn try_send(&mut self, frame: Frame<D>) -> Result<(), Frame<D>>

Attempts to send a frame on this channel.

This function returns the unsent frame back as an Err(_) if the channel could not (currently) accept another frame.

§Note

This is mostly useful for when trying to send a frame from outside of an asynchronous context. If in an async context, prefer Sender::send_data() instead.

Source

pub fn capacity(&mut self) -> usize

Returns the current capacity of the channel.

The capacity goes down when Frame<T>s are sent. The capacity goes up when these frames are received by the corresponding Channel<D, E>. This is distinct from max_capacity(), which always returns the buffer capacity initially specified when Channel::new() was called.

§Examples
use bytes::Bytes;
use http_body_util::{BodyExt, channel::Channel};
use std::convert::Infallible;

#[tokio::main]
async fn main() {
   let (mut tx, mut body) = Channel::<Bytes, Infallible>::new(4);
   assert_eq!(tx.capacity(), 4);

   // Sending a value decreases the available capacity.
   tx.send_data(Bytes::from("Hel")).await.unwrap();
   assert_eq!(tx.capacity(), 3);

   // Reading a value increases the available capacity.
   let _ = body.frame().await;
   assert_eq!(tx.capacity(), 4);
}
Source

pub fn max_capacity(&mut self) -> usize

Returns the maximum capacity of the channel.

This function always returns the buffer capacity initially specified when Channel::new() was called. This is distinct from capacity(), which returns the currently available capacity.

§Examples
use bytes::Bytes;
use http_body_util::{BodyExt, channel::Channel};
use std::convert::Infallible;

#[tokio::main]
async fn main() {
   let (mut tx, mut body) = Channel::<Bytes, Infallible>::new(4);
   assert_eq!(tx.max_capacity(), 4);

   // Sending a value buffers it, but does not affect the maximum capacity reported.
   tx.send_data(Bytes::from("Hel")).await.unwrap();
   assert_eq!(tx.max_capacity(), 4);
}
Source

pub fn abort(self, error: E)

Aborts the body in an abnormal fashion.

Trait Implementations§

Source§

impl<D, E: Debug> Debug for Sender<D, E>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<D, E = Infallible> !RefUnwindSafe for Sender<D, E>

§

impl<D, E = Infallible> !UnwindSafe for Sender<D, E>

§

impl<D, E> Freeze for Sender<D, E>

§

impl<D, E> Send for Sender<D, E>
where E: Send, D: Send,

§

impl<D, E> Sync for Sender<D, E>
where E: Send, D: Send,

§

impl<D, E> Unpin for Sender<D, E>

§

impl<D, E> UnsafeUnpin for Sender<D, 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<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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.