crossio-core 0.1.0

Core abstractions for the crossio async I/O backend
Documentation
//! Event types produced by backends when sources become ready.
use crate::{interest::Interest, token::Token};

/// A single readiness notification produced by a backend.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Event {
    token: Token,
    readiness: Interest,
}

impl Event {
    /// Creates an event with the provided token and readiness flags.
    pub const fn new(token: Token, readiness: Interest) -> Self {
        Self { token, readiness }
    }

    /// Returns the token that was associated with the ready source.
    pub const fn token(&self) -> Token {
        self.token
    }

    /// Returns the readiness information reported by the backend.
    pub const fn readiness(&self) -> Interest {
        self.readiness
    }
}

/// Collection of events returned from a poll operation.
#[derive(Debug, Default, Clone)]
pub struct Events {
    entries: Vec<Event>,
}

impl Events {
    /// Constructs an empty events list.
    pub fn new() -> Self {
        Self { entries: Vec::new() }
    }

    /// Pre-allocates storage for `capacity` events.
    pub fn with_capacity(capacity: usize) -> Self {
        Self {
            entries: Vec::with_capacity(capacity),
        }
    }

    /// Returns the number of events currently stored.
    pub fn len(&self) -> usize {
        self.entries.len()
    }

    /// Returns true when no events are stored.
    pub fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }

    /// Clears all collected events without releasing allocation.
    pub fn clear(&mut self) {
        self.entries.clear();
    }

    /// Iterator over the collected events.
    pub fn iter(&self) -> impl Iterator<Item = &Event> {
        self.entries.iter()
    }

    /// Adds a new event to the collection. Intended for backend implementations.
    pub fn push(&mut self, event: Event) {
        self.entries.push(event);
    }
}

impl IntoIterator for Events {
    type Item = Event;
    type IntoIter = std::vec::IntoIter<Event>;

    fn into_iter(self) -> Self::IntoIter {
        self.entries.into_iter()
    }
}

impl<'a> IntoIterator for &'a Events {
    type Item = &'a Event;
    type IntoIter = std::slice::Iter<'a, Event>;

    fn into_iter(self) -> Self::IntoIter {
        self.entries.iter()
    }
}