Skip to main content

wireshift_core/strategy/
blocking.rs

1//! Strategy for blocking synchronously on the ring for results.
2use std::time::Duration;
3
4use crate::completion::CompletionEvent;
5use crate::error::Result;
6use crate::{BackendFactory, Ring};
7
8/// Waits for completions using a blocking call.
9///
10/// ```no_run
11/// use wireshift::{Ring, RingConfig, strategy::BlockingDrain};
12///
13/// let ring = Ring::new(RingConfig::default())?;
14/// let drain = BlockingDrain::new(ring);
15/// let _ = drain.wait(None);
16/// # Ok::<(), Box<dyn std::error::Error>>(())
17/// ```
18#[derive(Clone)]
19pub struct BlockingDrain<F: BackendFactory> {
20    ring: Ring<F>,
21}
22
23impl<F: BackendFactory> BlockingDrain<F> {
24    /// Creates a blocking completion adapter.
25    #[must_use]
26    pub fn new(ring: Ring<F>) -> Self {
27        Self { ring }
28    }
29
30    /// Waits for exactly one completion.
31    pub fn wait(&self, timeout: Option<Duration>) -> Result<CompletionEvent> {
32        self.ring.complete(timeout)
33    }
34}