YCQueue

Struct YCQueue 

Source
pub struct YCQueue<'a> { /* private fields */ }

Implementations§

Source§

impl<'a> YCQueue<'a>

Source

pub fn new( shared_metadata: YCQueueSharedMeta<'a>, data_region: &'a mut [u8], ) -> Result<YCQueue<'a>, YCQueueError>

Create a queue backed by shared metadata and a contiguous data region.

§Arguments
  • shared_metadata - Shared ownership state that tracks slot usage across threads.
  • data_region - Contiguous slice that will be divided into fixed-size slots.
§Returns

Ok(YCQueue) when data_region aligns with the metadata configuration, or Err(YCQueueError::InvalidArgs) if the arguments disagree.

§Examples
use yep_coc::queue_alloc_helpers::YCQueueOwnedData;
use yep_coc::{YCQueue, YCQueueSharedMeta};

let mut owned = YCQueueOwnedData::new(4, 32);
let shared = YCQueueSharedMeta::new(&owned.meta);
let queue = YCQueue::new(shared, owned.data.as_mut_slice());
assert!(queue.is_ok());
Source

pub fn in_flight_count(&self) -> u16

Returns the number of slots that have been produced (or are being produced into) but not yet consumed.

§Returns

Count of slots currently in flight between producers and consumers.

§Examples
use yep_coc::queue_alloc_helpers::YCQueueOwnedData;
use yep_coc::{YCQueue, YCQueueSharedMeta};

let mut owned = YCQueueOwnedData::new(2, 16);
let shared = YCQueueSharedMeta::new(&owned.meta);
let mut queue = YCQueue::new(shared, owned.data.as_mut_slice()).unwrap();

assert_eq!(queue.in_flight_count(), 0);
let slot = queue.get_produce_slot().unwrap();
queue.mark_slot_produced(slot).unwrap();
assert_eq!(queue.in_flight_count(), 1);
Source

pub fn produce_idx(&self) -> u16

Returns the circular index that will be reserved by the next producer call.

§Returns

The slot index measured modulo the queue capacity.

Source

pub fn consume_idx(&self) -> u16

Returns the circular index that will be reserved by the next consumer call.

§Returns

The slot index measured modulo the queue capacity.

Source

pub fn capacity(&self) -> u16

Returns the total number of slots managed by this queue.

Source

pub fn get_produce_slots( &mut self, num_slots: u16, best_effort: bool, ) -> Result<Vec<YCQueueProduceSlot<'a>>, YCQueueError>

Reserve contiguous slots for producers, optionally in best-effort mode.

When best_effort is false, the function succeeds only if all num_slots are available; otherwise it returns as many contiguous slots as currently ready.

§Arguments
  • num_slots - Maximum number of contiguous slots to attempt to reserve.
  • best_effort - When true, grants a partial batch instead of requiring num_slots.
§Returns

Ok containing one or more slots when reservation succeeds.

§Errors

Returns YCQueueError::InvalidArgs when num_slots is zero or exceeds capacity, YCQueueError::OutOfSpace when there is no remaining capacity, and YCQueueError::SlotNotReady when the requested slots are not owned by the producer.

§Examples
use yep_coc::queue_alloc_helpers::YCQueueOwnedData;
use yep_coc::{YCQueue, YCQueueSharedMeta};

let mut owned = YCQueueOwnedData::new(4, 16);
let shared = YCQueueSharedMeta::new(&owned.meta);
let mut queue = YCQueue::new(shared, owned.data.as_mut_slice()).unwrap();

let slots = queue.get_produce_slots(2, false).unwrap();
assert_eq!(slots.len(), 2);
queue.mark_slots_produced(slots).unwrap();

let partial = queue.get_produce_slots(2, true).unwrap();
assert!(!partial.is_empty());
queue.mark_slots_produced(partial).unwrap();
Source

pub fn get_produce_slot( &mut self, ) -> Result<YCQueueProduceSlot<'a>, YCQueueError>

Reserve a single slot to produce into.

§Returns

Ok with the next available YCQueueProduceSlot when space exists.

§Errors

Propagates the same errors as get_produce_slots when no capacity is available.

§Examples
use yep_coc::queue_alloc_helpers::YCQueueOwnedData;
use yep_coc::{YCQueue, YCQueueSharedMeta};

let mut owned = YCQueueOwnedData::new(2, 16);
let shared = YCQueueSharedMeta::new(&owned.meta);
let mut queue = YCQueue::new(shared, owned.data.as_mut_slice()).unwrap();

// Reserve a slot
let slot = queue.get_produce_slot().unwrap();
// Fill it with data
slot.data.fill(0xAB);
Source

pub fn mark_slot_produced( &mut self, queue_slot: YCQueueProduceSlot<'a>, ) -> Result<(), YCQueueError>

Mark a slot as produced into. This makes it available to consumers.

§Arguments
§Returns

Ok(()) when the slot is successfully handed off to consumers.

§Errors

Returns YCQueueError::InvalidArgs when the slot has an unexpected size.

§Examples
use yep_coc::queue_alloc_helpers::YCQueueOwnedData;
use yep_coc::{YCQueue, YCQueueSharedMeta};

let mut owned = YCQueueOwnedData::new(2, 16);
let shared = YCQueueSharedMeta::new(&owned.meta);
let mut queue = YCQueue::new(shared, owned.data.as_mut_slice()).unwrap();

let slot = queue.get_produce_slot().unwrap();
queue.mark_slot_produced(slot).unwrap();
assert_eq!(queue.in_flight_count(), 1);
Source

pub fn mark_slots_produced( &mut self, queue_slots: Vec<YCQueueProduceSlot<'a>>, ) -> Result<(), YCQueueError>

Mark multiple slots as produced into. This makes them available to consumers.

§Arguments
§Returns

Ok(()) when ownership is returned to consumers.

§Errors

Returns YCQueueError::InvalidArgs when the slots are empty, non-contiguous, exceed capacity, or hold slices of the wrong length.

§Examples
use yep_coc::queue_alloc_helpers::YCQueueOwnedData;
use yep_coc::{YCQueue, YCQueueSharedMeta};

let mut owned = YCQueueOwnedData::new(4, 16);
let shared = YCQueueSharedMeta::new(&owned.meta);
let mut queue = YCQueue::new(shared, owned.data.as_mut_slice()).unwrap();

let slots = queue.get_produce_slots(4, false).unwrap();
queue.mark_slots_produced(slots).unwrap();
assert_eq!(queue.in_flight_count(), 4);
Source

pub fn get_consume_slots( &mut self, num_slots: u16, best_effort: bool, ) -> Result<Vec<YCQueueConsumeSlot<'a>>, YCQueueError>

Reserve contiguous slots for consumers, optionally in best-effort mode.

When best_effort is false, the function succeeds only if all num_slots are ready; otherwise it returns as many contiguous slots as currently published.

§Arguments
  • num_slots - Maximum number of contiguous slots to attempt to dequeue.
  • best_effort - When true, grants a partial batch instead of requiring num_slots.
§Returns

Ok containing one or more ready slots when the reservation succeeds.

§Errors

Returns YCQueueError::InvalidArgs when num_slots is zero or exceeds capacity, YCQueueError::EmptyQueue when nothing has been produced yet, and YCQueueError::SlotNotReady when the requested slots have not all been published.

§Examples
use yep_coc::queue_alloc_helpers::YCQueueOwnedData;
use yep_coc::{YCQueue, YCQueueSharedMeta};

let mut owned = YCQueueOwnedData::new(4, 16);
let shared = YCQueueSharedMeta::new(&owned.meta);
let mut queue = YCQueue::new(shared, owned.data.as_mut_slice()).unwrap();

let produce = queue.get_produce_slots(4, false).unwrap();
queue.mark_slots_produced(produce).unwrap();

let consume = queue.get_consume_slots(2, false).unwrap();
assert_eq!(consume.len(), 2);

let partial = queue.get_consume_slots(2, true).unwrap();
assert!(!partial.is_empty());
Source

pub fn get_consume_slot( &mut self, ) -> Result<YCQueueConsumeSlot<'a>, YCQueueError>

Reserve a single slot for consumption.

§Returns

Ok with the next ready YCQueueConsumeSlot when data is available.

§Errors

Propagates the same errors as get_consume_slots when no slots are ready.

§Examples
use yep_coc::queue_alloc_helpers::YCQueueOwnedData;
use yep_coc::{YCQueue, YCQueueSharedMeta};

let mut owned = YCQueueOwnedData::new(2, 16);
let shared = YCQueueSharedMeta::new(&owned.meta);
let mut queue = YCQueue::new(shared, owned.data.as_mut_slice()).unwrap();

let slot = queue.get_produce_slot().unwrap();
queue.mark_slot_produced(slot).unwrap();

let consume = queue.get_consume_slot().unwrap();
assert_eq!(consume.index, 0);
Source

pub fn mark_slot_consumed( &mut self, queue_slot: YCQueueConsumeSlot<'a>, ) -> Result<(), YCQueueError>

Return an individual consumption slot back to the producer pool.

§Arguments
§Returns

Ok(()) when the slot is successfully reclaimed for producers.

§Errors

Returns YCQueueError::InvalidArgs when the slot data length is unexpected.

§Examples
use yep_coc::queue_alloc_helpers::YCQueueOwnedData;
use yep_coc::{YCQueue, YCQueueSharedMeta};

let mut owned = YCQueueOwnedData::new(2, 16);
let shared = YCQueueSharedMeta::new(&owned.meta);
let mut queue = YCQueue::new(shared, owned.data.as_mut_slice()).unwrap();

let slot = queue.get_produce_slot().unwrap();
queue.mark_slot_produced(slot).unwrap();
let consume = queue.get_consume_slot().unwrap();
queue.mark_slot_consumed(consume).unwrap();
assert_eq!(queue.in_flight_count(), 0);
Source

pub fn mark_slots_consumed( &mut self, queue_slots: Vec<YCQueueConsumeSlot<'a>>, ) -> Result<(), YCQueueError>

Return multiple consumption slots back to the producer pool at once.

§Arguments
§Returns

Ok(()) when all slots are reclaimed for producers.

§Errors

Returns YCQueueError::InvalidArgs when any slot has an unexpected length, is out of order, or when the batch length exceeds the queue capacity.

§Examples
use yep_coc::queue_alloc_helpers::YCQueueOwnedData;
use yep_coc::{YCQueue, YCQueueSharedMeta};

let mut owned = YCQueueOwnedData::new(4, 16);
let shared = YCQueueSharedMeta::new(&owned.meta);
let mut queue = YCQueue::new(shared, owned.data.as_mut_slice()).unwrap();

let produce = queue.get_produce_slots(4, false).unwrap();
queue.mark_slots_produced(produce).unwrap();
let consume = queue.get_consume_slots(4, false).unwrap();
queue.mark_slots_consumed(consume).unwrap();
assert_eq!(queue.in_flight_count(), 0);
Source§

impl<'a> YCQueue<'a>

Auto Trait Implementations§

§

impl<'a> Freeze for YCQueue<'a>

§

impl<'a> !RefUnwindSafe for YCQueue<'a>

§

impl<'a> Send for YCQueue<'a>

§

impl<'a> !Sync for YCQueue<'a>

§

impl<'a> Unpin for YCQueue<'a>

§

impl<'a> !UnwindSafe for YCQueue<'a>

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.