1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
//! # IO Pipe Library
//!
//! A thread-safe library for creating multi-writer and single-reader pipelines.
//! This library is ideal for scenarios where you need to write bytes from multiple threads and read them from a single thread.
//!
//! ## Features
//!
//! - Thread-safe communication between writers and readers
//! - Support for both synchronous and asynchronous operations (via feature flags)
//! - Easy-to-use API for creating pipes
//!
//! ## Usage
//!
//! ### Synchronous API
//!
//! Enabled by default. To disable it, disable default features in your `Cargo.toml`:
//!
//! ```toml
//! [dependencies]
//! io_pipe = { version = "*", default-features = false }
//! ```
//!
//! #### Single-thread Example
//!
//! ```rust
//! use std::io::{read_to_string, Write};
//! use io_pipe::pipe;
//!
//! let (mut writer, reader) = pipe();
//! writer.write_all("hello".as_bytes()).unwrap();
//! drop(writer);
//!
//! assert_eq!("hello".to_string(), read_to_string(reader).unwrap());
//! ```
//!
//! #### Multi-thread Example
//!
//! ```rust
//! use std::io::{read_to_string, Write};
//! use std::thread::spawn;
//! use io_pipe::pipe;
//!
//! let (mut writer, reader) = pipe();
//! spawn(move || {
//!     writer.write_all("hello".as_bytes()).unwrap();
//! });
//!
//! assert_eq!("hello".len(), read_to_string(reader).unwrap().len());
//! ```
//!
//! ### Asynchronous API
//!
//! Enable the `async` feature in your `Cargo.toml`:
//!
//! ```toml
//! [dependencies]
//! io_pipe = { version = "*", features = ["async"] }
//! ```
//!
//! #### Async Example
//!
//! ```rust
//! use io_pipe::async_pipe;
//! use futures::io::AsyncWriteExt;
//! use futures::io::AsyncReadExt;
//! use futures::executor::block_on;
//!
//! block_on(async {
//!     let (mut writer, mut reader) = async_pipe();
//!     
//!     writer.write_all(b"hello").await.unwrap();
//!     drop(writer);
//!
//!     let mut buffer = String::new();
//!     reader.read_to_string(&mut buffer).await.unwrap();
//!     assert_eq!("hello", buffer);
//! })
//! ```
//!
//! #### Sync to Async Example
//!
//! ```rust
//! use io_pipe::async_reader_pipe;
//! use std::io::Write;
//! use futures::io::AsyncReadExt;
//! use futures::executor::block_on;
//!
//! let (mut writer, mut reader) = async_reader_pipe();
//! writer.write_all(b"hello").unwrap();
//! drop(writer);
//!
//! block_on(async {
//!     let mut buffer = String::new();
//!     reader.read_to_string(&mut buffer).await.unwrap();
//!     assert_eq!("hello", buffer);
//! })
//! ```
//!
//! #### Async to Sync Example
//!
//! ```rust
//! use io_pipe::async_writer_pipe;
//! use std::io::Read;
//! use futures::io::AsyncWriteExt;
//! use futures::executor::block_on;
//!
//! let (mut writer, mut reader) = async_writer_pipe();
//!
//! block_on(async {
//!     writer.write_all(b"hello").await.unwrap();
//!     drop(writer);
//! });
//!
//! let mut buffer = String::new();
//! reader.read_to_string(&mut buffer).unwrap();
//! assert_eq!("hello", buffer);
//! ```
//!
//! ## API Documentation
//!
//! For detailed API documentation, please refer to the individual module documentation:
//!
//! - [`pipe`], [`Writer`], and [`Reader`] for synchronous operations
//! - [`async_pipe`], [`AsyncWriter`], and [`AsyncReader`] for asynchronous operations

#[cfg(feature = "async")]
pub use async_pipe::{async_pipe, AsyncReader, AsyncWriter};
#[cfg(feature = "async")]
#[cfg(feature = "sync")]
pub use async_pipe::{async_reader_pipe, async_writer_pipe};
#[cfg(feature = "sync")]
pub use sync_pipe::{pipe, Reader, Writer};

#[cfg(feature = "async")]
mod async_pipe;
mod state;
#[cfg(feature = "sync")]
mod sync_pipe;