Expand description
§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:
[dependencies]
io_pipe = { version = "*", default-features = false }§Single-thread Example
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
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:
[dependencies]
io_pipe = { version = "*", features = ["async"] }§Async Example
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
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
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, andReaderfor synchronous operationsasync_pipe,AsyncWriter, andAsyncReaderfor asynchronous operations
Structs§
- Async
Reader - An asynchronous reader that implements
AsyncReadandAsyncBufRead. - Async
Writer - An asynchronous writer that implements
AsyncWrite. - Reader
- A synchronous reader that implements
ReadandBufRead. - Writer
- A synchronous writer that implements
Write.
Functions§
- async_
pipe - Creates a pair of asynchronous writer and reader objects.
- async_
reader_ pipe - Creates a pair of synchronous writer and asynchronous reader objects.
- async_
writer_ pipe - Creates a pair of synchronous writer and asynchronous reader objects.
- pipe
- Creates a pair of synchronous writer and reader objects.