poisoned_queue.rs

Overview

This file defines a generic data structure named PoisonedQueue designed for temporarily holding messages or items that could not be dispatched immediately due to the absence of a valid destination. The queue uses a buffer with a fixed capacity and evicts the oldest items when the buffer exceeds this capacity. This ensures that the queue never grows beyond a predefined size, albeit with a simple and potentially inefficient eviction strategy.

Structure and Functionality

PoisonedQueue

A generic struct that internally uses a VecDeque<T> as a buffer to store items of type T. The queue has a fixed capacity and supports adding new items and selectively retaining items based on a predicate.

Fields

Methods

new(capacity: usize) -> Self

Creates a new PoisonedQueue with the specified capacity.

push(&mut self, item: T)

Adds an item to the back of the queue. If the buffer exceeds the configured capacity after insertion, the oldest item (front of the queue) is removed (evicted).

retain<F>(&mut self, f: F) where F: FnMut(&T) -> bool

Retains only the items in the queue that satisfy the predicate function f. All other items are removed.

Implementation Notes

Interaction with Other Parts of the System

This queue is intended to be used in message dispatching systems where messages might fail to be delivered immediately. It temporarily stores these "poisoned" or undeliverable messages until a destination becomes available. Other components responsible for dispatching messages can push failed messages into this queue and later attempt to re-dispatch them by applying the retain method or other logic.

Because the queue is generic over type T, it is flexible and can be integrated with various message or event types handled elsewhere in the application.

Diagram

classDiagram
class PoisonedQueue~T~ {
-buffer: VecDeque~T~
-capacity: usize
+new(capacity: usize)
+push(item: T)
+retain(f: FnMut(&T) -> bool)
}

This class diagram shows the PoisonedQueue structure with its two fields and three primary methods, highlighting its encapsulated buffer and capacity management logic.