Skip to content

Commit

Permalink
Hooked up the nonblocking module to doc_cfg
Browse files Browse the repository at this point in the history
  • Loading branch information
kotauskas committed Mar 5, 2021
1 parent 0cf4891 commit e921fc4
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 14 deletions.
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
14 changes: 14 additions & 0 deletions src/nonblocking/imports.rs
Original file line number Diff line number Diff line change
@@ -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<T> = std::marker::PhantomData<T>;
}
}
31 changes: 18 additions & 13 deletions src/nonblocking/local_socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::error::Error>> {
/// # #[cfg(feature = "nonblocking")] {
/// let listener = LocalSocketListener::bind("/tmp/example.sock")
/// .await?;
/// listener
Expand All @@ -38,6 +36,7 @@ use crate::local_socket::{self as sync, ToLocalSocketName};
/// Ok(())
/// })
/// .await?;
/// # }
/// # Ok(()) }
/// ```
#[derive(Debug)]
Expand Down Expand Up @@ -92,6 +91,7 @@ impl LocalSocketListener {
pub struct Incoming {
inner: Unblock<SyncArcIncoming>,
}
#[cfg(feature = "nonblocking")]
impl Stream for Incoming {
type Item = Result<LocalSocketStream, io::Error>;
#[inline]
Expand All @@ -111,6 +111,7 @@ impl Stream for Incoming {
}
}
}
#[cfg(feature = "nonblocking")]
impl FusedStream for Incoming {
#[inline]
fn is_terminated(&self) -> bool {
Expand All @@ -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<std::error::Error>> {
/// # #[cfg(feature = "nonblocking")] {
/// // Replace the path as necessary on Windows.
/// let mut conn = LocalSocketStream::connect("/tmp/example.sock")
/// .await?;
Expand All @@ -147,6 +151,7 @@ impl Iterator for SyncArcIncoming {
/// let mut buffer = String::new();
/// conn.read_line(&mut buffer).await?;
/// println!("Server answered: {}", buffer);
/// # }
/// # Ok(()) }
/// ```
///
Expand All @@ -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<Result<usize, futures::io::Error>> {
) -> Poll<Result<usize, io::Error>> {
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<Result<usize, futures::io::Error>> {
) -> Poll<Result<usize, io::Error>> {
AsyncWrite::poll_write(Pin::new(&mut self.inner), cx, buf)
}
fn poll_flush(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Result<(), futures::io::Error>> {
) -> Poll<Result<(), io::Error>> {
AsyncWrite::poll_flush(Pin::new(&mut self.inner), cx)
}
fn poll_close(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Result<(), futures::io::Error>> {
) -> Poll<Result<(), io::Error>> {
AsyncWrite::poll_close(Pin::new(&mut self.inner), cx)
}
}
1 change: 1 addition & 0 deletions src/nonblocking/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

0 comments on commit e921fc4

Please sign in to comment.