pub struct PriorityEventQueue<M: Message> { /* private fields */ }Expand description
A priority queue for events with automatic backpressure handling
This queue ensures that high priority events (like user input and quit commands) are processed before lower priority events (like ticks and resize events). It implements backpressure to prevent memory exhaustion under high load.
When the queue reaches 80% capacity, backpressure is activated. If the queue fills completely, lower priority events are dropped in favor of higher priority ones.
§Example
let mut queue = PriorityEventQueue::new(1000);
queue.push(Event::Tick)?; // Low priority
queue.push(Event::Quit)?; // High priority
// High priority events are processed first
assert_eq!(queue.pop(), Some(Event::Quit));
assert_eq!(queue.pop(), Some(Event::Tick));Implementations§
Source§impl<M: Message> PriorityEventQueue<M>
impl<M: Message> PriorityEventQueue<M>
Sourcepub fn new(max_size: usize) -> Self
pub fn new(max_size: usize) -> Self
Create a new priority event queue with the specified maximum size
§Arguments
max_size- Maximum number of events the queue can hold
§Example
use hojicha_runtime::priority_queue::PriorityEventQueue;
#[derive(Debug, Clone, PartialEq)]
struct TestMsg;
let queue: PriorityEventQueue<TestMsg> = PriorityEventQueue::new(1000);
assert_eq!(queue.len(), 0);
assert_eq!(queue.capacity(), 1000);Sourcepub fn push(&mut self, event: Event<M>) -> Result<(), Event<M>>
pub fn push(&mut self, event: Event<M>) -> Result<(), Event<M>>
Push an event into the priority queue
Events are automatically prioritized based on their type. If the queue is full, lower priority events may be dropped to make room for higher priority ones.
§Arguments
event- The event to add to the queue
§Returns
Ok(())if the event was successfully addedErr(event)if the event was dropped due to queue overflow
§Example
use hojicha_runtime::priority_queue::PriorityEventQueue;
use hojicha_core::event::Event;
#[derive(Debug, Clone, PartialEq)]
struct TestMsg;
let mut queue: PriorityEventQueue<TestMsg> = PriorityEventQueue::new(1000);
let result = queue.push(Event::Tick);
assert!(result.is_ok());
assert_eq!(queue.len(), 1);Sourcepub fn pop(&mut self) -> Option<Event<M>>
pub fn pop(&mut self) -> Option<Event<M>>
Remove and return the highest priority event from the queue
Events are returned in priority order, with high priority events returned before normal and low priority events.
§Returns
Some(event)if there are events in the queueNoneif the queue is empty
§Example
use hojicha_runtime::priority_queue::PriorityEventQueue;
use hojicha_core::event::Event;
#[derive(Debug, Clone, PartialEq)]
struct TestMsg;
let mut queue: PriorityEventQueue<TestMsg> = PriorityEventQueue::new(1000);
queue.push(Event::Tick).unwrap();
queue.push(Event::Quit).unwrap(); // Higher priority
// High priority event comes first
assert_eq!(queue.pop(), Some(Event::Quit));
assert_eq!(queue.pop(), Some(Event::Tick));
assert_eq!(queue.pop(), None);Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Check if the queue is empty
§Returns
trueif the queue contains no eventsfalseif the queue contains one or more events
Sourcepub fn is_backpressure_active(&self) -> bool
pub fn is_backpressure_active(&self) -> bool
Check if backpressure is currently active
Backpressure is activated when the queue reaches 80% of its capacity.
§Returns
trueif backpressure is activefalseif the queue is below the backpressure threshold
Sourcepub fn dropped_events(&self) -> usize
pub fn dropped_events(&self) -> usize
Get the total number of events that have been dropped
Events are dropped when the queue is full and lower priority events are evicted to make room for higher priority ones.
§Returns
The total number of events dropped since queue creation
Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clear all events from the queue
This removes all events and resets the backpressure state, but preserves the dropped event counter and capacity settings.
Sourcepub fn try_grow(&mut self, additional: usize) -> Result<usize, ResizeError>
pub fn try_grow(&mut self, additional: usize) -> Result<usize, ResizeError>
Try to grow the queue by a specified amount
Sourcepub fn try_shrink(&mut self, reduction: usize) -> Result<usize, ResizeError>
pub fn try_shrink(&mut self, reduction: usize) -> Result<usize, ResizeError>
Try to shrink the queue by a specified amount
Sourcepub fn stats(&self) -> QueueStats
pub fn stats(&self) -> QueueStats
Get current queue statistics for scaling decisions