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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
//! # IO Pipe library
//! This library add a thread safe way to create multi writers and single reader pipeline.
//! Best way to use that library is writing bytes in few threads and reading that bytes in another single thread.
//!
//! Single thread usage example:
//! ```rust
//! use std::io::{read_to_string, Write};
//!
//! let (mut writer, reader) = io_pipe::pipe();
//! _ = writer.write("hello".as_bytes()).unwrap();
//! drop(writer);
//!
//! assert_eq!("hello".to_string(), read_to_string(reader).unwrap());
//! ```
//!
//! Multi thread usage 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("hello".as_bytes()).unwrap();
//! }
//! });
//!
//! assert_eq!("hello".len(), read_to_string(reader).unwrap().len());
//! ```
use std::{
io::{Error, ErrorKind, Read, Result as IOResult, Write},
sync::mpsc::{channel, Receiver, Sender},
};
use bytes::{Buf, BufMut, Bytes, BytesMut};
/// ## Multi writer
/// You can clone this writer to write bytes from different threads.
/// Example
/// ```rust
/// use std::io::Write;
///
/// let (mut writer, reader) = io_pipe::pipe();
/// _ = writer.write("hello".as_bytes()).unwrap();
/// ```
///
/// Write method will return an error, when reader dropped.
/// ```rust
/// use std::io::Write;
///
/// let (mut writer, reader) = io_pipe::pipe();
/// drop(reader);
/// assert!(writer.write("hello".as_bytes()).is_err());
/// ```
#[derive(Clone, Debug)]
pub struct Writer {
sender: Sender<Bytes>,
}
/// ## Single reader
/// The reader will produce bytes until all writers not dropped.
///
/// Example:
/// ```rust
/// use std::io::{read_to_string, Write};
/// use io_pipe::pipe;
/// let (mut writer, reader) = pipe();
/// _ = writer.write("hello".as_bytes()).unwrap();
/// drop(writer);
///
/// assert_eq!("hello".to_string(), read_to_string(reader).unwrap());
/// ```
///
/// Important: easies case to get deadlock is read from reader when writer is not dropped in single thread
#[derive(Debug)]
pub struct Reader {
receiver: Receiver<Bytes>,
buf: BytesMut,
}
impl Write for Writer {
fn write(&mut self, buf: &[u8]) -> IOResult<usize> {
match self.sender.send(Bytes::copy_from_slice(buf)) {
Ok(_) => Ok(buf.len()),
Err(e) => Err(Error::new(ErrorKind::BrokenPipe, e)),
}
}
fn flush(&mut self) -> IOResult<()> {
Ok(())
}
}
/// ## Create multi writer and single reader objects
///
/// Example
/// ```rust
/// use std::io::{read_to_string, Write};
///
/// let (mut writer, reader) = io_pipe::pipe();
/// _ = writer.write("hello".as_bytes()).unwrap();
/// drop(writer);
///
/// assert_eq!("hello".to_string(), read_to_string(reader).unwrap());
/// ```
pub fn pipe() -> (Writer, Reader) {
let (sender, receiver) = channel();
(
Writer { sender },
Reader {
receiver,
buf: BytesMut::new(),
},
)
}
impl Read for Reader {
fn read(&mut self, mut buf: &mut [u8]) -> IOResult<usize> {
if let Ok(data) = self.receiver.recv() {
self.buf.put(data);
}
let n = buf.write(self.buf.as_ref())?;
self.buf.advance(n);
Ok(n)
}
}
#[cfg(test)]
mod tests {
use std::io::{read_to_string, Write};
use std::thread::spawn;
use crate::pipe;
#[test]
fn base_case() {
let (mut writer, reader) = pipe();
_ = writer.write("hello ".as_bytes()).unwrap();
_ = writer.write("world".as_bytes()).unwrap();
drop(writer);
assert_eq!("hello world".to_string(), read_to_string(reader).unwrap());
}
#[test]
fn thread_case() {
let (writer, reader) = pipe();
spawn({
let mut writer = writer.clone();
move || {
_ = writer.write("hello".as_bytes()).unwrap();
}
});
spawn({
let mut writer = writer;
move || {
_ = writer.write("world".as_bytes()).unwrap();
}
});
assert_eq!("helloworld".len(), read_to_string(reader).unwrap().len());
}
#[test]
fn writer_err_case() {
let (mut writer, reader) = pipe();
drop(reader);
assert!(writer.write("hello".as_bytes()).is_err());
}
}