#![cfg_attr(nightly_tls_active, feature(thread_local))]
#![deny(missing_docs)]
pub mod fs;
pub mod net;
pub mod reactor;
pub mod timer;
#[cfg(unix)]
pub mod unix;
#[cfg(windows)]
pub mod windows;
#[cfg(target_arch = "wasm32")]
pub mod wasm;
use std::io;
pub trait Reactor: Send + Sync + 'static {
fn register_fd(&self, fd: RawFd, interest: Interest) -> io::Result<()>;
fn unregister_fd(&self, fd: RawFd) -> io::Result<()>;
fn poll_events(&self, timeout: Option<std::time::Duration>) -> io::Result<Vec<Event>>;
fn wake(&self) -> io::Result<()>;
}
#[cfg(unix)]
pub type RawFd = std::os::unix::io::RawFd;
#[cfg(windows)]
pub type RawFd = std::os::windows::io::RawHandle;
#[cfg(target_arch = "wasm32")]
pub type RawFd = u32;
#[cfg(not(any(unix, windows, target_arch = "wasm32")))]
pub type RawFd = usize;
#[cfg(target_os = "linux")]
pub type PlatformReactor = unix::epoll::EpollReactor;
#[cfg(any(
target_os = "macos",
target_os = "freebsd",
target_os = "openbsd",
target_os = "netbsd"
))]
pub type PlatformReactor = unix::kqueue::KqueueReactor;
#[cfg(windows)]
pub type PlatformReactor = windows::poll::WsaPollReactor;
#[cfg(target_arch = "wasm32")]
pub type PlatformReactor = wasm::WebReactor;
#[cfg(not(any(
target_os = "linux",
target_os = "macos",
target_os = "freebsd",
target_os = "openbsd",
target_os = "netbsd",
windows,
target_arch = "wasm32"
)))]
pub struct PlatformReactor;
#[derive(Debug, Clone, Copy)]
pub struct Interest {
pub readable: bool,
pub writable: bool,
pub error: bool,
}
impl Interest {
pub const READABLE: Self = Self {
readable: true,
writable: false,
error: true,
};
pub const WRITABLE: Self = Self {
readable: false,
writable: true,
error: true,
};
pub const READ_WRITE: Self = Self {
readable: true,
writable: true,
error: true,
};
}
#[derive(Debug, Clone)]
pub struct Event {
pub fd: RawFd,
pub readable: bool,
pub writable: bool,
pub error: bool,
pub hangup: bool,
}
#[cfg(not(any(
target_os = "linux",
target_os = "macos",
target_os = "freebsd",
target_os = "openbsd",
target_os = "netbsd",
windows,
target_arch = "wasm32"
)))]
impl Reactor for PlatformReactor {
fn register_fd(&self, _fd: RawFd, _interest: Interest) -> io::Result<()> {
Err(unsupported_reactor_error())
}
fn unregister_fd(&self, _fd: RawFd) -> io::Result<()> {
Err(unsupported_reactor_error())
}
fn poll_events(&self, _timeout: Option<std::time::Duration>) -> io::Result<Vec<Event>> {
Err(unsupported_reactor_error())
}
fn wake(&self) -> io::Result<()> {
Err(unsupported_reactor_error())
}
}
#[cfg(not(any(
target_os = "linux",
target_os = "macos",
target_os = "freebsd",
target_os = "openbsd",
target_os = "netbsd",
windows,
target_arch = "wasm32"
)))]
fn unsupported_reactor_error() -> io::Error {
io::Error::new(
io::ErrorKind::Unsupported,
"Platform not supported for native async I/O",
)
}
pub fn create_reactor() -> io::Result<PlatformReactor> {
#[cfg(target_os = "linux")]
return unix::epoll::EpollReactor::new();
#[cfg(any(
target_os = "macos",
target_os = "freebsd",
target_os = "openbsd",
target_os = "netbsd"
))]
return unix::kqueue::KqueueReactor::new();
#[cfg(windows)]
return windows::poll::WsaPollReactor::new();
#[cfg(target_arch = "wasm32")]
return wasm::WebReactor::new();
#[cfg(not(any(
target_os = "linux",
target_os = "macos",
target_os = "freebsd",
target_os = "openbsd",
target_os = "netbsd",
windows,
target_arch = "wasm32"
)))]
return Err(io::Error::new(
io::ErrorKind::Unsupported,
"Platform not supported for native async I/O",
));
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_interest_flags() {
let read_only = Interest::READABLE;
assert!(read_only.readable);
assert!(!read_only.writable);
assert!(read_only.error);
let write_only = Interest::WRITABLE;
assert!(!write_only.readable);
assert!(write_only.writable);
assert!(write_only.error);
let read_write = Interest::READ_WRITE;
assert!(read_write.readable);
assert!(read_write.writable);
assert!(read_write.error);
}
}