pub struct Sender<D, E = Infallible> { /* private fields */ }channel only.Expand description
A sender half created through Channel::new.
Implementations§
Source§impl<D, E> Sender<D, E>
impl<D, E> Sender<D, E>
Sourcepub async fn send(&mut self, frame: Frame<D>) -> Result<(), SendError>
pub async fn send(&mut self, frame: Frame<D>) -> Result<(), SendError>
Send a frame on the channel.
Sourcepub async fn send_trailers(
&mut self,
trailers: HeaderMap,
) -> Result<(), SendError>
pub async fn send_trailers( &mut self, trailers: HeaderMap, ) -> Result<(), SendError>
Send trailers on trailers channel.
Sourcepub fn try_send(&mut self, frame: Frame<D>) -> Result<(), Frame<D>>
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.
Sourcepub fn capacity(&mut self) -> usize
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);
}Sourcepub fn max_capacity(&mut self) -> usize
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);
}