Struct Queue

Source
pub struct Queue<T> { /* private fields */ }
Expand description

Main implementation of a multi-producer, multi-consumer (MPMC) asynchronous queue.

This queue allows multiple asynchronous tasks to concurrently send and receive messages. It uses a fixed-size buffer and provides backpressure by waiting when the queue is full (for producers) or empty (for consumers).

§Examples

// Using the legacy Queue API
use tokio_mpmc::Queue;

#[tokio::main]
async fn main() {
    // Create a queue with capacity of 100
    let queue = Queue::new(100);

    // Send a message
    if let Err(e) = queue.send("Hello").await {
        eprintln!("Send failed: {}", e);
    }

    // Receive a message
    match queue.receive().await {
        Ok(Some(msg)) => println!("Received message: {}", msg),
        Ok(None) => println!("Queue is empty"),
        Err(e) => eprintln!("Receive failed: {}", e),
    }

    // Close the queue
    queue.close();
}

Implementations§

Source§

impl<T> Queue<T>

Source

pub fn new(capacity: usize) -> Self

Creates a new MPMC queue with the specified capacity.

§Arguments
  • capacity - The maximum number of elements the queue can hold.
§Returns

A new Queue instance.

Source

pub async fn send(&self, value: T) -> QueueResult<()>

Sends a message value to the queue.

If the queue is full, the calling task will be suspended until space becomes available or the queue is closed.

§Arguments
  • value - The message to send.
§Returns

Ok(()) if the message was successfully sent. Err(QueueError::QueueClosed) if the queue was closed while waiting or before sending.

Source

pub async fn receive(&self) -> QueueResult<Option<T>>

Receives a message from the queue.

If the queue is empty, the calling task will be suspended until an item becomes available or the queue is closed.

§Returns

Ok(Some(value)) if a message was successfully received. Ok(None) if the queue is closed and empty. Err(QueueError::QueueClosed) if the queue was closed while waiting but not empty.

Source

pub fn close(&self)

Closes the queue.

This prevents any new messages from being sent. Tasks currently waiting in send will return Err(QueueError::QueueClosed). Tasks waiting in receive will return Ok(None) once the queue is empty.

Source

pub fn len(&self) -> usize

Gets the current number of elements in the queue.

§Returns

The number of elements currently in the queue.

Source

pub fn is_empty(&self) -> bool

Checks if the queue is currently empty.

§Returns

true if the queue contains no elements, false otherwise.

Source

pub fn is_full(&self) -> bool

Checks if the queue is currently full.

§Returns

true if the number of elements equals the capacity, false otherwise.

Source

pub fn is_closed(&self) -> bool

Checks if the queue has been closed.

§Returns

true if the queue is closed, false otherwise.

Trait Implementations§

Source§

impl<T: Clone> Clone for Queue<T>

Source§

fn clone(&self) -> Queue<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

§

impl<T> Freeze for Queue<T>

§

impl<T> RefUnwindSafe for Queue<T>

§

impl<T> Send for Queue<T>
where T: Send,

§

impl<T> Sync for Queue<T>
where T: Send,

§

impl<T> Unpin for Queue<T>

§

impl<T> UnwindSafe for Queue<T>

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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