Crate mill_io

Crate mill_io 

Source
Expand description

§Mill-IO

A lightweight, production-ready event loop library for Rust that provides efficient non-blocking I/O management without relying on heavyweight async runtimes like Tokio.

Mill-IO is a modular, reactor-based event loop built on top of mio, offering cross-platform polling, configurable thread pool integration, and object pooling for high-performance applications that need fine-grained control over their I/O operations.

§Core Philosophy

Mill-IO was designed for applications that require:

  • Predictable performance with minimal runtime overhead
  • Runtime-agnostic architecture that doesn’t force async/await patterns
  • Direct control over concurrency and resource management
  • Minimal dependencies for reduced attack surface and faster builds

§Features

  • Runtime-agnostic: No dependency on Tokio or other async runtimes
  • Cross-platform: Leverages mio’s polling abstraction (epoll, kqueue, IOCP)
  • Thread pool integration: Configurable worker threads for handling I/O events
  • Object pooling: Reduces allocation overhead for frequent operations
  • Clean API: Simple registration and handler interface
  • Thread-safe: Lock-free operations in hot paths

§Architecture Overview

┌─────────────┐    ┌──────────────┐    ┌─────────────┐
│ EventLoop   │───▶│   Reactor    │───▶│ PollHandle  │
└─────────────┘    └──────────────┘    └─────────────┘
                            │
                            ▼
                   ┌──────────────┐    ┌─────────────┐
                   │ ThreadPool   │───▶│   Workers   │
                   └──────────────┘    └─────────────┘

§Quick Start

use mill_io::{EventLoop, EventHandler};
use mio::{net::TcpListener, Interest, Token};
use std::net::SocketAddr;

struct EchoHandler;

impl EventHandler for EchoHandler {
    fn handle_event(&self, event: &mio::event::Event) {
        println!("Received event: {:?}", event);
        // Handle incoming connections and data
    }
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Create event loop with default configuration
    let event_loop = EventLoop::default();
     
    // Bind to localhost
    let addr: SocketAddr = "127.0.0.1:8080".parse()?;
    let mut listener = TcpListener::bind(addr)?;

    // Register the listener with a handler
    event_loop.register(
        &mut listener,
        Token(1),
        Interest::READABLE,
        EchoHandler
    )?;

    println!("Server listening on 127.0.0.1:8080");
     
    // Start the event loop (blocks until stopped)
    event_loop.run()?;

    Ok(())
}

§Advanced Configuration

use mill_io::EventLoop;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Create event loop with custom configuration
    let event_loop = EventLoop::new(
        8,      // 8 worker threads
        1024,   // Handle up to 1024 events per poll
        100     // 100ms poll timeout
    )?;
    Ok(())
}

§Module Overview

  • EventLoop: Main entry point for registering I/O sources and running the event loop
  • EventHandler: Trait for implementing custom event handling logic
  • reactor: Core reactor implementation managing the event loop lifecycle
  • thread_pool: Configurable thread pool for distributing work
  • object_pool: Memory-efficient object pooling for buffers and resources
  • poll: Cross-platform polling abstraction and handler registry
  • error: Error types and result handling

For comprehensive examples and architectural details, see the README and Architecture Guide.

Re-exports§

pub use handler::EventHandler;
pub use object_pool::ObjectPool;
pub use object_pool::PooledObject;

Modules§

error
handler
object_pool
poll
prelude
A convenient prelude module that re-exports commonly used types and traits.
reactor
thread_pool

Structs§

EventLoop
The main event loop structure for registering I/O sources and handling events.