39 releases (stable)

new 3.7.0 Apr 22, 2024
3.6.0 Mar 24, 2024
3.5.0 Feb 18, 2024
3.3.1 Nov 24, 2023
0.1.9 Sep 6, 2020

#29 in Asynchronous

Download history 488194/week @ 2024-01-05 552042/week @ 2024-01-12 641423/week @ 2024-01-19 746753/week @ 2024-01-26 646681/week @ 2024-02-02 634933/week @ 2024-02-09 647917/week @ 2024-02-16 678143/week @ 2024-02-23 699111/week @ 2024-03-01 686969/week @ 2024-03-08 693752/week @ 2024-03-15 721367/week @ 2024-03-22 729494/week @ 2024-03-29 718311/week @ 2024-04-05 752614/week @ 2024-04-12 629258/week @ 2024-04-19

2,961,139 downloads per month
Used in 4,368 crates (26 directly)

Apache-2.0 OR MIT

200KB
4K SLoC

polling

Build License Cargo Documentation

Portable interface to epoll, kqueue, event ports, and IOCP.

Supported platforms:

  • epoll: Linux, Android, RedoxOS
  • kqueue: macOS, iOS, tvOS, watchOS, FreeBSD, NetBSD, OpenBSD, DragonFly BSD
  • event ports: illumos, Solaris
  • poll: VxWorks, Fuchsia, HermitOS, other Unix systems
  • IOCP: Windows, Wine (version 7.13+)

Polling is done in oneshot mode, which means interest in I/O events needs to be reset after an event is delivered if we're interested in the next event of the same kind.

Only one thread can be waiting for I/O events at a time.

Examples

use polling::{Event, Poller};
use std::net::TcpListener;

// Create a TCP listener.
let socket = TcpListener::bind("127.0.0.1:8000")?;
socket.set_nonblocking(true)?;
let key = 7; // Arbitrary key identifying the socket.

// Create a poller and register interest in readability on the socket.
let poller = Poller::new()?;
poller.add(&socket, Event::readable(key))?;

// The event loop.
let mut events = Vec::new();
loop {
    // Wait for at least one I/O event.
    events.clear();
    poller.wait(&mut events, None)?;

    for ev in &events {
        if ev.key == key {
            // Perform a non-blocking accept operation.
            socket.accept()?;
            // Set interest in the next readability event.
            poller.modify(&socket, Event::readable(key))?;
        }
    }
}

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~2–12MB
~142K SLoC