Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

port mio to qnx #1781

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/event/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ impl Event {
///
/// # Notes
///
/// Out-of-band (OOB) data also triggers readable events. But most
/// applications don't actually read OOB data, this could leave an
/// Out-of-band (OOB) data also triggers readable events. But must
/// application don't actually read OOB data, this could leave an
/// application open to a Denial-of-Service (Dos) attack, see
/// <https://github.com/sandstorm-io/sandstorm-website/blob/58f93346028c0576e8147627667328eaaf4be9fa/_posts/2015-04-08-osx-security-bug.md>.
/// However because Mio uses edge-triggers it will not result in an infinite
Expand Down
31 changes: 28 additions & 3 deletions src/io_source.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use std::ops::{Deref, DerefMut};
#[cfg(target_os = "hermit")]
use std::os::hermit::io::AsRawFd;
#[cfg(unix)]
use std::os::unix::io::AsRawFd;
#[cfg(target_os = "wasi")]
Expand Down Expand Up @@ -34,6 +32,33 @@ use crate::{event, Interest, Registry, Token};
///
/// [`Poll`]: crate::Poll
/// [`do_io`]: IoSource::do_io
/*
///
/// # Examples
///
/// Basic usage.
///
/// ```
/// # use std::error::Error;
/// # fn main() -> Result<(), Box<dyn Error>> {
/// use mio::{Interest, Poll, Token};
/// use mio::IoSource;
///
/// use std::net;
///
/// let poll = Poll::new()?;
///
/// // Bind a std TCP listener.
/// let listener = net::TcpListener::bind("127.0.0.1:0")?;
/// // Wrap it in the `IoSource` type.
/// let mut listener = IoSource::new(listener);
///
/// // Register the listener.
/// poll.registry().register(&mut listener, Token(0), Interest::READABLE)?;
/// # Ok(())
/// # }
/// ```
*/
pub struct IoSource<T> {
state: IoSourceState,
inner: T,
Expand Down Expand Up @@ -104,7 +129,7 @@ impl<T> DerefMut for IoSource<T> {
}
}

#[cfg(any(unix, target_os = "hermit"))]
#[cfg(unix)]
impl<T> event::Source for IoSource<T>
where
T: AsRawFd,
Expand Down
8 changes: 0 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,6 @@ pub mod unix {
pub use crate::sys::SourceFd;
}

#[cfg(all(target_os = "hermit", feature = "os-ext"))]
#[cfg_attr(docsrs, doc(cfg(all(target_os = "hermit", feature = "os-ext"))))]
pub mod hermit {
//! Hermit only extensions.

pub use crate::sys::SourceFd;
}

#[cfg(all(windows, feature = "os-ext"))]
#[cfg_attr(docsrs, doc(cfg(all(windows, feature = "os-ext"))))]
pub mod windows {
Expand Down
4 changes: 2 additions & 2 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ macro_rules! cfg_net {
macro_rules! cfg_io_source {
($($item:item)*) => {
$(
#[cfg(any(feature = "net", all(any(unix, target_os = "hermit"), feature = "os-ext")))]
#[cfg_attr(docsrs, doc(cfg(any(feature = "net", all(any(unix, target_os = "hermit"), feature = "os-ext")))))]
#[cfg(any(feature = "net", all(unix, feature = "os-ext")))]
#[cfg_attr(docsrs, doc(cfg(any(feature = "net", all(unix, feature = "os-ext")))))]
$item
)*
}
Expand Down
30 changes: 5 additions & 25 deletions src/net/tcp/listener.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use std::net::{self, SocketAddr};
#[cfg(target_os = "hermit")]
use std::os::hermit::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
#[cfg(unix)]
use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
#[cfg(target_os = "wasi")]
Expand All @@ -11,7 +9,7 @@ use std::{fmt, io};

use crate::io_source::IoSource;
use crate::net::TcpStream;
#[cfg(any(unix, target_os = "hermit"))]
#[cfg(unix)]
use crate::sys::tcp::set_reuseaddr;
#[cfg(not(target_os = "wasi"))]
use crate::sys::tcp::{bind, listen, new_for_addr};
Expand Down Expand Up @@ -60,7 +58,7 @@ impl TcpListener {
#[cfg(not(target_os = "wasi"))]
pub fn bind(addr: SocketAddr) -> io::Result<TcpListener> {
let socket = new_for_addr(addr)?;
#[cfg(any(unix, target_os = "hermit"))]
#[cfg(unix)]
let listener = unsafe { TcpListener::from_raw_fd(socket) };
#[cfg(windows)]
let listener = unsafe { TcpListener::from_raw_socket(socket as _) };
Expand Down Expand Up @@ -168,21 +166,21 @@ impl fmt::Debug for TcpListener {
}
}

#[cfg(any(unix, target_os = "hermit"))]
#[cfg(unix)]
impl IntoRawFd for TcpListener {
fn into_raw_fd(self) -> RawFd {
self.inner.into_inner().into_raw_fd()
}
}

#[cfg(any(unix, target_os = "hermit"))]
#[cfg(unix)]
impl AsRawFd for TcpListener {
fn as_raw_fd(&self) -> RawFd {
self.inner.as_raw_fd()
}
}

#[cfg(any(unix, target_os = "hermit"))]
#[cfg(unix)]
impl FromRawFd for TcpListener {
/// Converts a `RawFd` to a `TcpListener`.
///
Expand Down Expand Up @@ -248,21 +246,3 @@ impl FromRawFd for TcpListener {
TcpListener::from_std(FromRawFd::from_raw_fd(fd))
}
}

impl From<TcpListener> for net::TcpListener {
fn from(listener: TcpListener) -> Self {
// Safety: This is safe since we are extracting the raw fd from a well-constructed
// mio::net::TcpListener which ensures that we actually pass in a valid file
// descriptor/socket
unsafe {
#[cfg(any(unix, target_os = "hermit", target_os = "wasi"))]
{
net::TcpListener::from_raw_fd(listener.into_raw_fd())
}
#[cfg(windows)]
{
net::TcpListener::from_raw_socket(listener.into_raw_socket())
}
}
}
}
37 changes: 7 additions & 30 deletions src/net/tcp/stream.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use std::fmt;
use std::io::{self, IoSlice, IoSliceMut, Read, Write};
use std::net::{self, Shutdown, SocketAddr};
#[cfg(target_os = "hermit")]
use std::os::hermit::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
#[cfg(unix)]
use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
#[cfg(target_os = "wasi")]
Expand Down Expand Up @@ -70,15 +68,12 @@ impl TcpStream {
/// 1. Call `TcpStream::connect`
/// 2. Register the returned stream with at least [write interest].
/// 3. Wait for a (writable) event.
/// 4. Check `TcpStream::take_error`. If it returns an error, then
/// something went wrong. If it returns `Ok(None)`, then proceed to
/// step 5.
/// 5. Check `TcpStream::peer_addr`. If it returns `libc::EINPROGRESS` or
/// 4. Check `TcpStream::peer_addr`. If it returns `libc::EINPROGRESS` or
/// `ErrorKind::NotConnected` it means the stream is not yet connected,
/// go back to step 3. If it returns an address it means the stream is
/// connected, go to step 6. If another error is returned something
/// connected, go to step 5. If another error is returned something
/// went wrong.
/// 6. Now the stream can be used.
/// 5. Now the stream can be used.
///
/// This may return a `WouldBlock` in which case the socket connection
/// cannot be completed immediately, it usually means there are insufficient
Expand All @@ -88,7 +83,7 @@ impl TcpStream {
#[cfg(not(target_os = "wasi"))]
pub fn connect(addr: SocketAddr) -> io::Result<TcpStream> {
let socket = new_for_addr(addr)?;
#[cfg(any(unix, target_os = "hermit"))]
#[cfg(unix)]
let stream = unsafe { TcpStream::from_raw_fd(socket) };
#[cfg(windows)]
let stream = unsafe { TcpStream::from_raw_socket(socket as _) };
Expand Down Expand Up @@ -350,21 +345,21 @@ impl fmt::Debug for TcpStream {
}
}

#[cfg(any(unix, target_os = "hermit"))]
#[cfg(unix)]
impl IntoRawFd for TcpStream {
fn into_raw_fd(self) -> RawFd {
self.inner.into_inner().into_raw_fd()
}
}

#[cfg(any(unix, target_os = "hermit"))]
#[cfg(unix)]
impl AsRawFd for TcpStream {
fn as_raw_fd(&self) -> RawFd {
self.inner.as_raw_fd()
}
}

#[cfg(any(unix, target_os = "hermit"))]
#[cfg(unix)]
impl FromRawFd for TcpStream {
/// Converts a `RawFd` to a `TcpStream`.
///
Expand Down Expand Up @@ -430,21 +425,3 @@ impl FromRawFd for TcpStream {
TcpStream::from_std(FromRawFd::from_raw_fd(fd))
}
}

impl From<TcpStream> for net::TcpStream {
fn from(stream: TcpStream) -> Self {
// Safety: This is safe since we are extracting the raw fd from a well-constructed
// mio::net::TcpStream which ensures that we actually pass in a valid file
// descriptor/socket
unsafe {
#[cfg(any(unix, target_os = "hermit", target_os = "wasi"))]
{
net::TcpStream::from_raw_fd(stream.into_raw_fd())
}
#[cfg(windows)]
{
net::TcpStream::from_raw_socket(stream.into_raw_socket())
}
}
}
}
26 changes: 3 additions & 23 deletions src/net/udp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ use std::fmt;
use std::io;
use std::net;
use std::net::{Ipv4Addr, Ipv6Addr, SocketAddr};
#[cfg(target_os = "hermit")]
use std::os::hermit::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
#[cfg(unix)]
use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
#[cfg(windows)]
Expand Down Expand Up @@ -644,21 +642,21 @@ impl fmt::Debug for UdpSocket {
}
}

#[cfg(any(unix, target_os = "hermit"))]
#[cfg(unix)]
impl IntoRawFd for UdpSocket {
fn into_raw_fd(self) -> RawFd {
self.inner.into_inner().into_raw_fd()
}
}

#[cfg(any(unix, target_os = "hermit"))]
#[cfg(unix)]
impl AsRawFd for UdpSocket {
fn as_raw_fd(&self) -> RawFd {
self.inner.as_raw_fd()
}
}

#[cfg(any(unix, target_os = "hermit"))]
#[cfg(unix)]
impl FromRawFd for UdpSocket {
/// Converts a `RawFd` to a `UdpSocket`.
///
Expand Down Expand Up @@ -697,21 +695,3 @@ impl FromRawSocket for UdpSocket {
UdpSocket::from_std(FromRawSocket::from_raw_socket(socket))
}
}

impl From<UdpSocket> for net::UdpSocket {
fn from(socket: UdpSocket) -> Self {
// Safety: This is safe since we are extracting the raw fd from a well-constructed
// mio::net::UdpSocket which ensures that we actually pass in a valid file
// descriptor/socket
unsafe {
#[cfg(any(unix, target_os = "hermit", target_os = "wasi"))]
{
net::UdpSocket::from_raw_fd(socket.into_raw_fd())
}
#[cfg(windows)]
{
net::UdpSocket::from_raw_socket(socket.into_raw_socket())
}
}
}
}
9 changes: 0 additions & 9 deletions src/net/uds/datagram.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,12 +234,3 @@ impl FromRawFd for UnixDatagram {
UnixDatagram::from_std(FromRawFd::from_raw_fd(fd))
}
}

impl From<UnixDatagram> for net::UnixDatagram {
fn from(datagram: UnixDatagram) -> Self {
// Safety: This is safe since we are extracting the raw fd from a well-constructed
// mio::net::uds::UnixListener which ensures that we actually pass in a valid file
// descriptor/socket
unsafe { net::UnixDatagram::from_raw_fd(datagram.into_raw_fd()) }
}
}
9 changes: 0 additions & 9 deletions src/net/uds/listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,3 @@ impl FromRawFd for UnixListener {
UnixListener::from_std(FromRawFd::from_raw_fd(fd))
}
}

impl From<UnixListener> for net::UnixListener {
fn from(listener: UnixListener) -> Self {
// Safety: This is safe since we are extracting the raw fd from a well-constructed
// mio::net::uds::UnixListener which ensures that we actually pass in a valid file
// descriptor/socket
unsafe { net::UnixListener::from_raw_fd(listener.into_raw_fd()) }
}
}
9 changes: 0 additions & 9 deletions src/net/uds/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,12 +252,3 @@ impl FromRawFd for UnixStream {
UnixStream::from_std(FromRawFd::from_raw_fd(fd))
}
}

impl From<UnixStream> for net::UnixStream {
fn from(stream: UnixStream) -> Self {
// Safety: This is safe since we are extracting the raw fd from a well-constructed
// mio::net::uds::UnixStream which ensures that we actually pass in a valid file
// descriptor/socket
unsafe { net::UnixStream::from_raw_fd(stream.into_raw_fd()) }
}
}
8 changes: 4 additions & 4 deletions src/poll.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#[cfg(all(
unix,
not(mio_unsupported_force_poll_poll),
not(any(target_os = "solaris", target_os = "vita"))
not(any(target_os = "solaris", target_os = "vita", target_os = "nto"))
))]
use std::os::unix::io::{AsRawFd, RawFd};
#[cfg(all(debug_assertions, not(target_os = "wasi")))]
Expand Down Expand Up @@ -430,7 +430,7 @@ impl Poll {
#[cfg(all(
unix,
not(mio_unsupported_force_poll_poll),
not(any(target_os = "solaris", target_os = "vita"))
not(any(target_os = "solaris", target_os = "vita", target_os = "nto"))
))]
impl AsRawFd for Poll {
fn as_raw_fd(&self) -> RawFd {
Expand Down Expand Up @@ -721,7 +721,7 @@ impl fmt::Debug for Registry {
#[cfg(all(
unix,
not(mio_unsupported_force_poll_poll),
not(any(target_os = "solaris", target_os = "vita"))
not(any(target_os = "solaris", target_os = "vita", target_os = "nto"))
))]
impl AsRawFd for Registry {
fn as_raw_fd(&self) -> RawFd {
Expand All @@ -733,7 +733,7 @@ cfg_os_poll! {
#[cfg(all(
unix,
not(mio_unsupported_force_poll_poll),
not(any(target_os = "solaris", target_os = "vita")),
not(any(target_os = "solaris", target_os = "vita", target_os = "nto")),
))]
#[test]
pub fn as_raw_fd() {
Expand Down
2 changes: 1 addition & 1 deletion src/sys/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ cfg_os_poll! {
}
}

#[cfg(any(unix, target_os = "hermit"))]
#[cfg(unix)]
cfg_os_poll! {
mod unix;
#[allow(unused_imports)]
Expand Down