diff --git a/src/lib.rs b/src/lib.rs index ab0be37d..17049c17 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -147,7 +147,8 @@ pub(crate) mod private { } pub mod local_socket; -#[cfg(feature = "nonblocking")] +#[cfg(any(doc, feature = "nonblocking"))] +#[cfg_attr(feature = "doc_cfg", doc(cfg(feature = "nonblocking")))] pub mod nonblocking; pub mod unnamed_pipe; //pub mod shared_memory; diff --git a/src/nonblocking/imports.rs b/src/nonblocking/imports.rs new file mode 100644 index 00000000..d8cb5822 --- /dev/null +++ b/src/nonblocking/imports.rs @@ -0,0 +1,14 @@ +#![allow(unused_imports)] +use cfg_if::cfg_if; + +cfg_if! { + if #[cfg(feature = "nonblocking")] { + pub use blocking::{unblock, Unblock}; + pub use futures::{ + stream::{FusedStream, Stream}, + AsyncRead, AsyncWrite, + }; + } else { + pub type Unblock = std::marker::PhantomData; + } +} diff --git a/src/nonblocking/local_socket.rs b/src/nonblocking/local_socket.rs index d2e5f474..3a2032c0 100644 --- a/src/nonblocking/local_socket.rs +++ b/src/nonblocking/local_socket.rs @@ -4,27 +4,25 @@ //! //! [blocking version of this module]: ../../local_socket/index.html " " -use blocking::{unblock, Unblock}; -use futures::{ - stream::{FusedStream, Stream}, - AsyncRead, AsyncWrite, -}; -use std::{io, sync::Arc}; - +use std::{io, sync::Arc, pin::Pin, task::{Context, Poll}}; +use super::imports::*; use crate::local_socket::{self as sync, ToLocalSocketName}; /// An asynchronous local socket server, listening for connections. /// /// # Example /// ```no_run +/// # #[cfg(feature = "nonblocking")] /// use futures::{ /// io::{BufReader, AsyncBufReadExt, AsyncWriteExt}, /// stream::TryStreamExt, /// }; +/// # #[cfg(feature = "nonblocking")] /// use interprocess::nonblocking::local_socket::*; /// /// # #[tokio::main] /// # async fn main() -> Result<(), Box> { +/// # #[cfg(feature = "nonblocking")] { /// let listener = LocalSocketListener::bind("/tmp/example.sock") /// .await?; /// listener @@ -38,6 +36,7 @@ use crate::local_socket::{self as sync, ToLocalSocketName}; /// Ok(()) /// }) /// .await?; +/// # } /// # Ok(()) } /// ``` #[derive(Debug)] @@ -92,6 +91,7 @@ impl LocalSocketListener { pub struct Incoming { inner: Unblock, } +#[cfg(feature = "nonblocking")] impl Stream for Incoming { type Item = Result; #[inline] @@ -111,6 +111,7 @@ impl Stream for Incoming { } } } +#[cfg(feature = "nonblocking")] impl FusedStream for Incoming { #[inline] fn is_terminated(&self) -> bool { @@ -134,11 +135,14 @@ impl Iterator for SyncArcIncoming { /// /// # Example /// ```no_run +/// # #[cfg(feature = "nonblocking")] /// use futures::io::{BufReader, AsyncBufReadExt, AsyncWriteExt}; +/// # #[cfg(feature = "nonblocking")] /// use interprocess::nonblocking::local_socket::*; /// /// # #[tokio::main] /// # async fn main() -> Result<(), Box> { +/// # #[cfg(feature = "nonblocking")] { /// // Replace the path as necessary on Windows. /// let mut conn = LocalSocketStream::connect("/tmp/example.sock") /// .await?; @@ -147,6 +151,7 @@ impl Iterator for SyncArcIncoming { /// let mut buffer = String::new(); /// conn.read_line(&mut buffer).await?; /// println!("Server answered: {}", buffer); +/// # } /// # Ok(()) } /// ``` /// @@ -166,35 +171,35 @@ impl LocalSocketStream { } } -use futures::task::{Context, Poll}; -use std::pin::Pin; +#[cfg(feature = "nonblocking")] impl AsyncRead for LocalSocketStream { fn poll_read( mut self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &mut [u8], - ) -> Poll> { + ) -> Poll> { AsyncRead::poll_read(Pin::new(&mut self.inner), cx, buf) } } +#[cfg(feature = "nonblocking")] impl AsyncWrite for LocalSocketStream { fn poll_write( mut self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8], - ) -> Poll> { + ) -> Poll> { AsyncWrite::poll_write(Pin::new(&mut self.inner), cx, buf) } fn poll_flush( mut self: Pin<&mut Self>, cx: &mut Context<'_>, - ) -> Poll> { + ) -> Poll> { AsyncWrite::poll_flush(Pin::new(&mut self.inner), cx) } fn poll_close( mut self: Pin<&mut Self>, cx: &mut Context<'_>, - ) -> Poll> { + ) -> Poll> { AsyncWrite::poll_close(Pin::new(&mut self.inner), cx) } } diff --git a/src/nonblocking/mod.rs b/src/nonblocking/mod.rs index c028d6ea..503e4cdb 100644 --- a/src/nonblocking/mod.rs +++ b/src/nonblocking/mod.rs @@ -4,4 +4,5 @@ //! //! The layout of this module aims to closely resemble the crate root, in that all the modules here mirror their blocking counterparts — check them out for usage examples and details about the differences you may encounter when porting blocking code to an async architecture. +mod imports; pub mod local_socket;