39 releases (stable)

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

#20 in Asynchronous

Download history 591555/week @ 2024-01-16 645898/week @ 2024-01-23 772175/week @ 2024-01-30 622358/week @ 2024-02-06 638296/week @ 2024-02-13 664358/week @ 2024-02-20 685319/week @ 2024-02-27 690763/week @ 2024-03-05 684046/week @ 2024-03-12 718432/week @ 2024-03-19 695171/week @ 2024-03-26 754996/week @ 2024-04-02 724484/week @ 2024-04-09 758449/week @ 2024-04-16 760475/week @ 2024-04-23 568186/week @ 2024-04-30

2,942,301 downloads per month
Used in 4,460 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
~139K SLoC