43 releases (stable)

new 3.7.4 Nov 7, 2024
3.7.2 Jun 15, 2024
3.6.0 Mar 24, 2024
3.3.1 Nov 24, 2023
0.1.9 Sep 6, 2020

#31 in Asynchronous

Download history 899147/week @ 2024-07-18 903462/week @ 2024-07-25 895853/week @ 2024-08-01 979055/week @ 2024-08-08 922962/week @ 2024-08-15 984341/week @ 2024-08-22 965315/week @ 2024-08-29 1006716/week @ 2024-09-05 939781/week @ 2024-09-12 935715/week @ 2024-09-19 989303/week @ 2024-09-26 938811/week @ 2024-10-03 882246/week @ 2024-10-10 917099/week @ 2024-10-17 891528/week @ 2024-10-24 890673/week @ 2024-10-31

3,757,707 downloads per month
Used in 5,300 crates (33 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, visionOS, 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–11MB
~138K SLoC