Skip to content

Commit

Permalink
Review notes on wasi port
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomasdezeeuw committed Feb 18, 2022
1 parent 32158ae commit e530779
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 51 deletions.
4 changes: 3 additions & 1 deletion src/net/tcp/listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ use std::{fmt, io};

use crate::io_source::IoSource;
use crate::net::TcpStream;
#[cfg(any(unix, target_os = "wasi"))]
#[cfg(unix)]
use crate::sys::tcp::set_reuseaddr;
#[cfg(not(target_os = "wasi"))]
use crate::sys::tcp::{bind, listen, new_for_addr};
use crate::{event, sys, Interest, Registry, Token};

Expand Down Expand Up @@ -54,6 +55,7 @@ impl TcpListener {
/// 2. Set the `SO_REUSEADDR` option on the socket on Unix.
/// 3. Bind the socket to the specified address.
/// 4. Calls `listen` on the socket to prepare it to receive new connections.
#[cfg(not(target_os = "wasi"))]
pub fn bind(addr: SocketAddr) -> io::Result<TcpListener> {
let socket = new_for_addr(addr)?;
#[cfg(any(unix, target_os = "wasi"))]
Expand Down
2 changes: 2 additions & 0 deletions src/net/tcp/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use std::os::wasi::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
use std::os::windows::io::{AsRawSocket, FromRawSocket, IntoRawSocket, RawSocket};

use crate::io_source::IoSource;
#[cfg(not(target_os = "wasi"))]
use crate::sys::tcp::{connect, new_for_addr};
use crate::{event, Interest, Registry, Token};

Expand Down Expand Up @@ -75,6 +76,7 @@ impl TcpStream {
/// 5. Now the stream can be used.
///
/// [read interest]: Interest::READABLE
#[cfg(not(target_os = "wasi"))]
pub fn connect(addr: SocketAddr) -> io::Result<TcpStream> {
let socket = new_for_addr(addr)?;
#[cfg(any(unix, target_os = "wasi"))]
Expand Down
15 changes: 12 additions & 3 deletions src/sys/wasi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,16 @@ use std::time::Duration;
use crate::{Interest, Token};

cfg_net! {
mod net;

pub(crate) use net::tcp;
pub(crate) mod tcp {
use std::io;
use std::net::{self, SocketAddr};

pub(crate) fn accept(listener: &net::TcpListener) -> io::Result<(net::TcpStream, SocketAddr)> {
let (stream, addr) = listener.accept()?;
stream.set_nonblocking(true)?;
Ok((stream, addr))
}
}
}

/// Unique id for use as `SelectorId`.
Expand Down Expand Up @@ -184,10 +191,12 @@ impl Selector {
};
if let Some(index) = subscriptions.iter().position(predicate) {
subscriptions.swap_remove(index);
/* NOTE: can't have read and write subscriptions.
// We might have two subscriptions (read and write).
if let Some(index) = subscriptions.iter().skip(index).position(predicate) {
subscriptions.swap_remove(index);
}
*/
Ok(())
} else {
Err(io::ErrorKind::NotFound.into())
Expand Down
47 changes: 0 additions & 47 deletions src/sys/wasi/net.rs

This file was deleted.

0 comments on commit e530779

Please sign in to comment.