pub struct DisruptorBuilder {}Expand description
§Disruptor Builder Pattern Guide
The builder follows a type-state pattern to ensure compile-time correctness. Each step in the builder chain enforces required configuration in a specific order:
- Start with data provider (ring buffer)
- Configure waiting strategy
- Set up sequencer
- Add event handlers
- Build the final disruptor
§Example Usage
use disruptor_rs::{
DisruptorBuilder, EventHandler, EventProcessorExecutor, EventProducer,
sequence::Sequence,
};
#[derive(Default)]
struct MyEvent;
#[derive(Default)]
struct MyEventHandler;
impl EventHandler<MyEvent> for MyEventHandler {
fn on_event(&self, _event: &MyEvent, _sequence: Sequence, _end_of_batch: bool) {}
fn on_start(&self) {}
fn on_shutdown(&self) {}
}
let (executor, producer) = DisruptorBuilder::with_ring_buffer::<MyEvent>(1024)
.with_busy_spin_waiting_strategy()
.with_single_producer_sequencer()
.with_barrier(|scope| {
scope.handle_events(MyEventHandler::default());
})
.build();§Builder States
WithDataProvider: Initial state, holds the ring bufferWithWaitingStrategy: Configures how consumers wait for new eventsWithSequencer: Manages the sequencing of eventsWithEventHandlers: Configures event processing chain
§Barrier Scopes
The with_barrier method creates scopes for configuring event handlers
in a dependency chain. Handlers within the same barrier scope can run
in parallel, while different barrier scopes run sequentially.
use disruptor_rs::{
DisruptorBuilder, EventHandler, EventProcessorExecutor, EventProducer,
sequence::Sequence,
};
#[derive(Default)]
struct MyEvent;
#[derive(Default)]
struct MyEventHandler;
impl EventHandler<MyEvent> for MyEventHandler {
fn on_event(&self, _event: &MyEvent, _sequence: Sequence, _end_of_batch: bool) {}
fn on_start(&self) {}
fn on_shutdown(&self) {}
}
let (executor, producer) = DisruptorBuilder::with_ring_buffer::<MyEvent>(1024)
.with_busy_spin_waiting_strategy()
.with_single_producer_sequencer()
.with_barrier(|scope| {
// These handlers run in parallel
scope.handle_events(MyEventHandler::default());
scope.handle_events(MyEventHandler::default());
})
.with_barrier(|scope| {
// This handler waits for both previous handlers
scope.handle_events(MyEventHandler::default());
})
.build();Implementations§
Source§impl DisruptorBuilder
impl DisruptorBuilder
pub fn new<D: DataProvider<T>, T>( data_provider: Arc<D>, ) -> WithDataProvider<D, T>
pub fn with_ring_buffer<T>( capacity: usize, ) -> WithDataProvider<RingBuffer<T>, T>
Trait Implementations§
Auto Trait Implementations§
impl Freeze for DisruptorBuilder
impl RefUnwindSafe for DisruptorBuilder
impl Send for DisruptorBuilder
impl Sync for DisruptorBuilder
impl Unpin for DisruptorBuilder
impl UnwindSafe for DisruptorBuilder
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more