Skip to content

Commit

Permalink
Add unix::pipe (#1356)
Browse files Browse the repository at this point in the history
Adds one new function: unix::pipe, which is a wrapper around the pipe(2)
system call, and two new types: Sender and Receiver, wrappers around the
file descriptors.

This is a port of https://github.com/Thomasdezeeuw/mio-pipe, commit
8c3025edf128e90733e95327d88493887b93fcdd.
  • Loading branch information
Thomasdezeeuw committed Oct 24, 2020
1 parent b3b36ae commit 2b7c096
Show file tree
Hide file tree
Showing 16 changed files with 743 additions and 22 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ include = [
default = []
os-poll = []
os-util = []
pipe = ["os-poll"]
tcp = []
udp = []
uds = []
Expand Down
2 changes: 1 addition & 1 deletion ci/azure-test-stable.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
- ${{ if eq(parameters.cmd, 'test') }}:
- script: |
cargo install cargo-hack
cargo hack check --feature-powerset --skip guide
cargo hack check --feature-powerset --skip guide,extra-docs
displayName: Check feature powerset
- script: cargo ${{ parameters.cmd }} --all-features
Expand Down
30 changes: 27 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,11 @@ mod waker;

pub mod event;

cfg_net! {
cfg_io_source! {
mod io_source;
}

cfg_net! {
pub mod net;
}

Expand All @@ -82,11 +84,27 @@ pub use poll::{Poll, Registry};
pub use token::Token;
pub use waker::Waker;

#[cfg(all(unix, feature = "os-util"))]
#[cfg_attr(docsrs, doc(cfg(all(unix, feature = "os-util"))))]
#[cfg(all(unix, any(feature = "os-util", feature = "pipe")))]
#[cfg_attr(
docsrs,
doc(cfg(all(unix, any(feature = "os-util", feature = "pipe"))))
)]
pub mod unix {
//! Unix only extensions.

#[cfg(feature = "os-util")]
#[cfg_attr(docsrs, doc(cfg(all(unix, feature = "os-util"))))]
pub use crate::sys::SourceFd;

cfg_pipe! {
pub mod pipe {
//! Unix pipe.
//!
//! See the [`new`] function for documentation.

pub use crate::sys::pipe::{new, Receiver, Sender};
}
}
}

#[cfg(all(windows, feature = "os-util"))]
Expand Down Expand Up @@ -121,6 +139,12 @@ pub mod features {
//! `os-util` enables additional OS specific facilities. Currently this
//! means the `unix` module (with `SourceFd`) becomes available.
//!
#![cfg_attr(feature = "pipe", doc = "## `pipe` (enabled)")]
#![cfg_attr(not(feature = "pipe"), doc = "## `pipe` (disabled)")]
//!
//! The `pipe` feature adds `unix::pipe`, and related types, a non-blocking
//! wrapper around the `pipe(2)` system call.
//!
//! ## Network types
//!
//! Mio provide three features to enable network types:
Expand Down
32 changes: 28 additions & 4 deletions src/macros/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@ macro_rules! cfg_net {
}
}

/// One of the features enabled that needs `IoSource`. That is `tcp`, or `udp`,
/// or on Unix `uds` or `pipe`.
macro_rules! cfg_io_source {
($($item:item)*) => {
$(
#[cfg(any(feature = "tcp", feature = "udp", all(unix, any(feature = "uds", feature = "pipe"))))]
#[cfg_attr(docsrs, doc(any(feature = "tcp", feature = "udp", all(unix, any(feature = "uds", feature = "pipe")))))]
$item
)*
}
}

/// One of the `tcp`, `udp` features enabled.
#[cfg(windows)]
macro_rules! cfg_net {
Expand Down Expand Up @@ -82,13 +94,25 @@ macro_rules! cfg_uds {
}
}

/// Feature `pipe` enabled.
#[cfg(unix)]
macro_rules! cfg_pipe {
($($item:item)*) => {
$(
#[cfg(feature = "pipe")]
#[cfg_attr(docsrs, doc(cfg(feature = "pipe")))]
$item
)*
}
}

/// Feature `os-util` enabled, or one of the features that need `os-util`.
#[cfg(unix)]
macro_rules! cfg_any_os_util {
($($item:item)*) => {
$(
#[cfg(any(feature = "os-util", feature = "tcp", feature = "udp", feature = "uds"))]
#[cfg_attr(docsrs, doc(cfg(any(feature = "os-util", feature = "tcp", feature = "udp", feature = "uds"))))]
#[cfg(any(feature = "os-util", feature = "tcp", feature = "udp", feature = "uds", feature = "pipe"))]
#[cfg_attr(docsrs, doc(cfg(any(feature = "os-util", feature = "tcp", feature = "udp", feature = "uds", feature = "pipe"))))]
$item
)*
}
Expand All @@ -99,8 +123,8 @@ macro_rules! cfg_any_os_util {
macro_rules! cfg_any_os_util {
($($item:item)*) => {
$(
#[cfg(any(feature = "os-util", feature = "tcp", feature = "udp"))]
#[cfg_attr(docsrs, doc(cfg(any(feature = "os-util", feature = "tcp", feature = "udp"))))]
#[cfg(any(feature = "os-util", feature = "tcp", feature = "udp", feature = "pipe"))]
#[cfg_attr(docsrs, doc(cfg(any(feature = "os-util", feature = "tcp", feature = "udp", feature = "pipe"))))]
$item
)*
}
Expand Down
6 changes: 5 additions & 1 deletion src/sys/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,11 @@ cfg_os_poll! {
pub(crate) use self::unix::uds;
}

cfg_net! {
cfg_pipe! {
pub(crate) use self::unix::pipe;
}

cfg_io_source! {
pub(crate) use self::unix::IoSourceState;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/sys/shell/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ cfg_uds! {
pub(crate) mod uds;
}

cfg_net! {
cfg_io_source! {
use std::io;
#[cfg(windows)]
use std::os::windows::io::RawSocket;
Expand Down
2 changes: 1 addition & 1 deletion src/sys/shell/selector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ cfg_any_os_util! {
}
}

cfg_net! {
cfg_io_source! {
#[cfg(debug_assertions)]
impl Selector {
pub fn id(&self) -> usize {
Expand Down
6 changes: 5 additions & 1 deletion src/sys/unix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ cfg_os_poll! {
pub use self::uds::SocketAddr;
}

cfg_net! {
cfg_io_source! {
use std::io;

// Both `kqueue` and `epoll` don't need to hold any user space state.
Expand All @@ -59,6 +59,10 @@ cfg_os_poll! {
}
}
}

cfg_pipe! {
pub(crate) mod pipe;
}
}

cfg_not_os_poll! {
Expand Down

0 comments on commit 2b7c096

Please sign in to comment.