Skip to content

Commit

Permalink
Add polling for target_os = "wasi"
Browse files Browse the repository at this point in the history
Based on tokio-rs#1395

Co-authored-by: Thomas de Zeeuw <thomasdezeeuw@gmail.com>
Signed-off-by: Harald Hoyer <harald@profian.com>
  • Loading branch information
haraldh and Thomasdezeeuw committed Feb 4, 2022
1 parent 0ffb287 commit 3473d06
Show file tree
Hide file tree
Showing 4 changed files with 419 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ miow = "0.3.6"
winapi = { version = "0.3", features = ["winsock2", "mswsock"] }
ntapi = "0.3"

[target.'cfg(target_os = "wasi")'.dependencies]
wasi = "0.11.0"
libc = "0.2.86"

[dev-dependencies]
env_logger = { version = "0.8.4", default-features = false }
rand = "0.8"
Expand Down
40 changes: 40 additions & 0 deletions src/io_source.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::ops::{Deref, DerefMut};
#[cfg(unix)]
use std::os::unix::io::AsRawFd;
#[cfg(target_os = "wasi")]
use std::os::wasi::io::AsRawFd;
#[cfg(windows)]
use std::os::windows::io::AsRawSocket;
#[cfg(debug_assertions)]
Expand Down Expand Up @@ -200,6 +202,44 @@ where
}
}

#[cfg(target_os = "wasi")]
impl<T> event::Source for IoSource<T>
where
T: AsRawFd,
{
fn register(
&mut self,
registry: &Registry,
token: Token,
interests: Interest,
) -> io::Result<()> {
#[cfg(debug_assertions)]
self.selector_id.associate(registry)?;
registry
.selector()
.register(self.inner.as_raw_fd() as _, token, interests)
}

fn reregister(
&mut self,
registry: &Registry,
token: Token,
interests: Interest,
) -> io::Result<()> {
#[cfg(debug_assertions)]
self.selector_id.check_association(registry)?;
registry
.selector()
.reregister(self.inner.as_raw_fd() as _, token, interests)
}

fn deregister(&mut self, registry: &Registry) -> io::Result<()> {
#[cfg(debug_assertions)]
self.selector_id.remove_association(registry)?;
registry.selector().deregister(self.inner.as_raw_fd() as _)
}
}

impl<T> fmt::Debug for IoSource<T>
where
T: fmt::Debug,
Expand Down
6 changes: 6 additions & 0 deletions src/sys/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ cfg_os_poll! {
pub use self::windows::*;
}

#[cfg(target_os = "wasi")]
cfg_os_poll! {
mod wasi;
pub(crate) use self::wasi::*;
}

cfg_not_os_poll! {
mod shell;
pub(crate) use self::shell::*;
Expand Down
Loading

0 comments on commit 3473d06

Please sign in to comment.