Skip to main content

WorkQueue

Struct WorkQueue 

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

A bounded, priority-ordered work queue.

Items are stored in a Vec sorted in ascending priority order so that the highest-priority item is always at the back and can be removed in O(1). Pushes maintain sorted order via binary search insertion at O(n) in the worst case, which is acceptable for the typical small queue sizes used in media pipeline scheduling.

§Examples

use oximedia_core::work_queue::{WorkItem, WorkQueue};

let mut q: WorkQueue<i32> = WorkQueue::new(4);
q.push(WorkItem::new(1, 5)).expect("queue not full");
q.push(WorkItem::new(2, 1)).expect("queue not full");
q.push(WorkItem::new(3, 9)).expect("queue not full");
assert_eq!(q.pop().expect("queue not empty").payload, 3); // priority 9 first
assert_eq!(q.len(), 2);

Implementations§

Source§

impl<T> WorkQueue<T>

Source

pub fn new(capacity: usize) -> Self

Creates a new queue with the given maximum capacity.

§Panics

Panics if capacity is zero.

Source

pub const fn capacity(&self) -> usize

Returns the maximum number of items this queue can hold.

Source

pub fn len(&self) -> usize

Returns the current number of items in the queue.

Source

pub fn is_empty(&self) -> bool

Returns true if the queue contains no items.

Source

pub fn is_full(&self) -> bool

Returns true if the queue has reached its capacity.

Source

pub fn push(&mut self, item: WorkItem<T>) -> Result<(), QueueError>

Pushes a work item into the queue in priority order.

§Errors

Returns QueueError::Full if the queue is at capacity.

Source

pub fn pop(&mut self) -> Option<WorkItem<T>>

Removes and returns the highest-priority item, or None if empty.

Source

pub fn pop_batch(&mut self, n: usize) -> Result<Vec<WorkItem<T>>, QueueError>

Removes and returns up to n highest-priority items.

§Errors

Returns QueueError::BatchTooLarge if n > capacity.

Source

pub fn peek(&self) -> Option<&WorkItem<T>>

Peeks at the highest-priority item without removing it.

Source

pub fn clear(&mut self)

Removes all items from the queue (statistics are preserved).

Source

pub fn stats(&self) -> QueueStats

Returns a snapshot of the lifetime statistics.

Source

pub fn iter(&self) -> impl Iterator<Item = &WorkItem<T>>

Returns an iterator over all items in ascending priority order.

Trait Implementations§

Source§

impl<T: Debug> Debug for WorkQueue<T>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T> Freeze for WorkQueue<T>

§

impl<T> RefUnwindSafe for WorkQueue<T>
where T: RefUnwindSafe,

§

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

§

impl<T> Sync for WorkQueue<T>
where T: Sync,

§

impl<T> Unpin for WorkQueue<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for WorkQueue<T>

§

impl<T> UnwindSafe for WorkQueue<T>
where T: UnwindSafe,

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.