Expand description
Minimal non-blocking I/O library.
§Example: reading from stdin
use std::{io, io::prelude::*, process, time};
fn main() -> io::Result<()> {
// Create a registry to hold I/O sources.
let mut sources = popol::Sources::with_capacity(1);
// Create an events buffer to hold readiness events.
let mut events = Vec::with_capacity(1);
// Register the program's standard input as a source of "read" readiness events.
sources.register((), &io::stdin(), popol::interest::READ);
// Wait on our event sources for at most 6 seconds. If an event source is
// ready before then, process its events. Otherwise, timeout.
match sources.poll(&mut events, popol::Timeout::from_secs(6)) {
Ok(_) => {}
Err(err) if err.kind() == io::ErrorKind::TimedOut => process::exit(1),
Err(err) => return Err(err),
}
// Iterate over source events. Since we only have one source
// registered, this will only iterate once.
for event in events.drain(..) {
// The standard input has data ready to be read.
if event.is_readable() || event.is_hangup() {
let mut buf = [0; 1024];
// Read what we can from standard input and echo it.
match io::stdin().read(&mut buf[..]) {
Ok(n) => io::stdout().write_all(&buf[..n])?,
Err(err) => panic!("{}", err),
}
}
}
Ok(())
}
Re-exports§
pub use interest::Interest;
Modules§
- interest
- Source readiness interest.
Structs§
- Event
- An I/O ready event.
- Source
- A source of readiness events, eg. a
net::TcpStream
. - Sources
- Keeps track of sources to poll.
- Waker
- Wakers are used to wake up
wait
.
Enums§
- Timeout
- Optional timeout.
Functions§
- set_
nonblocking - Set non-blocking mode on a stream.
Type Aliases§
- Events
- Raw input or output events.