#![cfg_attr(nightly_tls_active, feature(thread_local))]
#![deny(missing_docs)]
pub mod fs;
#[cfg(any(unix, windows))]
pub mod instance;
pub mod net;
pub mod reactor;
pub mod thread;
pub mod timer;
mod frame;
#[cfg(unix)]
pub mod unix;
#[cfg(windows)]
pub mod windows;
#[cfg(target_arch = "wasm32")]
pub mod wasm;
#[cfg(any(target_arch = "wasm32", test))]
#[path = "wasm/drop_validation.rs"]
mod drop_validation;
#[cfg(any(target_arch = "wasm32", test))]
#[path = "wasm/file_policy.rs"]
mod file_policy;
#[cfg(any(target_arch = "wasm32", test))]
#[path = "wasm/text_validation.rs"]
mod text_validation;
#[cfg(any(target_arch = "wasm32", test))]
#[path = "wasm/text_model.rs"]
mod text_model;
#[cfg(any(target_arch = "wasm32", test))]
#[path = "wasm/canvas_validation.rs"]
mod canvas_validation;
#[cfg(any(target_arch = "wasm32", test))]
#[path = "wasm/gpu_device_loss.rs"]
mod gpu_device_loss;
#[cfg(any(target_arch = "wasm32", test))]
#[path = "wasm/content_box.rs"]
mod content_box;
#[cfg(any(target_arch = "wasm32", test))]
#[path = "wasm/history_path.rs"]
mod history_path;
#[cfg(any(target_arch = "wasm32", test))]
#[path = "wasm/key_validation.rs"]
mod key_validation;
#[cfg(any(target_arch = "wasm32", test))]
mod local_task;
#[cfg(any(target_arch = "wasm32", test))]
#[path = "websocket_state.rs"]
mod websocket_state;
use std::io;
macro_rules! reactor_trait_items {
() => {
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(not(target_arch = "wasm32"))]
pub trait Reactor: Send + Sync + 'static {
reactor_trait_items!();
}
#[cfg(target_arch = "wasm32")]
pub trait Reactor: 'static {
reactor_trait_items!();
}
#[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);
}
}