crossio-core 0.1.0

Core abstractions for the crossio async I/O backend
Documentation
//! Backend configuration knobs shared across platforms.
use std::time::Duration;

/// Configuration passed to every backend on creation.
#[derive(Debug, Clone)]
pub struct BackendConfig {
    queue_depth: usize,
    event_batch_size: usize,
    spin_before_park: Duration,
    default_timeout: Option<Duration>,
}

impl BackendConfig {
    /// Returns a configuration with sensible defaults for most workloads.
    pub fn new() -> Self {
        Self::default()
    }

    /// Maximum number of descriptors that can be registered with the backend.
    pub fn queue_depth(&self) -> usize {
        self.queue_depth
    }

    /// Number of events a backend should attempt to process per poll call.
    pub fn event_batch_size(&self) -> usize {
        self.event_batch_size
    }

    /// Duration the backend may spin prior to parking the thread.
    pub fn spin_before_park(&self) -> Duration {
        self.spin_before_park
    }

    /// Default timeout applied when callers pass `None` to [`Backend::poll`](crate::Backend::poll).
    pub fn default_timeout(&self) -> Option<Duration> {
        self.default_timeout
    }

    /// Overrides the queue depth.
    pub fn with_queue_depth(mut self, queue_depth: usize) -> Self {
        self.queue_depth = queue_depth.max(1);
        self
    }

    /// Overrides the event batch size.
    pub fn with_event_batch_size(mut self, batch: usize) -> Self {
        self.event_batch_size = batch.max(1);
        self
    }

    /// Overrides the spin duration.
    pub fn with_spin_before_park(mut self, duration: Duration) -> Self {
        self.spin_before_park = duration;
        self
    }

    /// Overrides the default timeout value.
    pub fn with_default_timeout(mut self, timeout: Option<Duration>) -> Self {
        self.default_timeout = timeout;
        self
    }
}

impl Default for BackendConfig {
    fn default() -> Self {
        Self {
            queue_depth: 4096,
            event_batch_size: 512,
            spin_before_park: Duration::from_micros(50),
            default_timeout: None,
        }
    }
}