pub struct Poller { /* private fields */ }
Expand description

Waits for I/O events.

Implementations

Creates a new poller.

Examples
use polling::Poller;

let poller = Poller::new()?;

Adds a file descriptor or socket to the poller.

A file descriptor or socket is considered readable or writable when a read or write operation on it would not block. This doesn’t mean the read or write operation will succeed, it only means the operation will return immediately.

If interest is set in both readability and writability, the two kinds of events might be delivered either separately or together.

For example, interest in Event { key: 7, readable: true, writable: true } might result in a single Event of the same form, or in two separate Events:

  • Event { key: 7, readable: true, writable: false }
  • Event { key: 7, readable: false, writable: true }

Note that interest in I/O events needs to be re-enabled using modify() again after an event is delivered if we’re interested in the next event of the same kind.

Don’t forget to delete() the file descriptor or socket when it is no longer used!

Errors

This method returns an error in the following situations:

  • If key equals usize::MAX because that key is reserved for internal use.
  • If an error is returned by the syscall.
Examples

Set interest in all events:

use polling::{Event, Poller};

let source = std::net::TcpListener::bind("127.0.0.1:0")?;
source.set_nonblocking(true)?;
let key = 7;

let poller = Poller::new()?;
poller.add(&source, Event::all(key))?;

Modifies the interest in a file descriptor or socket.

This method has the same behavior as add() except it modifies the interest of a previously added file descriptor or socket.

To use this method with a file descriptor or socket, you must first add it using add().

Note that interest in I/O events needs to be re-enabled using modify() again after an event is delivered if we’re interested in the next event of the same kind.

Errors

This method returns an error in the following situations:

  • If key equals usize::MAX because that key is reserved for internal use.
  • If an error is returned by the syscall.
Examples

To enable interest in all events:

poller.modify(&source, Event::all(key))?;

To enable interest in readable events and disable interest in writable events:

poller.modify(&source, Event::readable(key))?;

To disable interest in readable events and enable interest in writable events:

poller.modify(&source, Event::writable(key))?;

To disable interest in all events:

poller.modify(&source, Event::none(key))?;

Removes a file descriptor or socket from the poller.

Unlike add(), this method only removes the file descriptor or socket from the poller without putting it back into blocking mode.

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

let socket = TcpListener::bind("127.0.0.1:0")?;
socket.set_nonblocking(true)?;
let key = 7;

let poller = Poller::new()?;
poller.add(&socket, Event::all(key))?;
poller.delete(&socket)?;

Waits for at least one I/O event and returns the number of new events.

New events will be appended to events. If necessary, make sure to clear the Vec before calling wait()!

This method will return with no new events if a notification is delivered by the notify() method, or the timeout is reached. Sometimes it may even return with no events spuriously.

Only one thread can wait on I/O. If another thread is already in wait(), concurrent calls to this method will return immediately with no new events.

If the operating system is ready to deliver a large number of events at once, this method may decide to deliver them in smaller batches.

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

let socket = TcpListener::bind("127.0.0.1:0")?;
socket.set_nonblocking(true)?;
let key = 7;

let poller = Poller::new()?;
poller.add(&socket, Event::all(key))?;

let mut events = Vec::new();
let n = poller.wait(&mut events, Some(Duration::from_secs(1)))?;

Wakes up the current or the following invocation of wait().

If no thread is calling wait() right now, this method will cause the following call to wake up immediately.

Examples
use polling::Poller;

let poller = Poller::new()?;

// Notify the poller.
poller.notify()?;

let mut events = Vec::new();
poller.wait(&mut events, None)?; // wakes up immediately
assert!(events.is_empty());

Trait Implementations

Formats the value using the given formatter. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.