Crates.io | io-pipe |
lib.rs | io-pipe |
version | 0.6.2 |
source | src |
created_at | 2024-08-07 21:52:25.246723 |
updated_at | 2024-08-15 21:29:39.273016 |
description | A fast and thread-safe library for creating multi-writer and single-reader pipelines in Rust. |
homepage | |
repository | https://github.com/Mnwa/io-pipe |
max_upload_size | |
id | 1328853 |
size | 35,496 |
IO Pipe is a thread-safe Rust library for creating multi-writer and single-reader pipelines. It's ideal for scenarios where you need to write bytes from multiple threads and read them from a single thread.
Add this to your Cargo.toml
:
[dependencies]
io-pipe = "0.x.x"
For async support, enable the async
feature:
[dependencies]
io-pipe = { version = "0.x.x", features = ["async"] }
Single-thread example:
use std::io::{read_to_string, Write};
use io_pipe::pipe;
fn main() {
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;
fn main() {
let (mut writer, reader) = pipe();
spawn(move || {
writer.write_all("hello".as_bytes()).unwrap();
});
assert_eq!("hello".len(), read_to_string(reader).unwrap().len());
}
use io_pipe::async_pipe;
use futures::io::{AsyncWriteExt, AsyncReadExt};
use futures::executor::block_on;
fn main() {
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);
});
}
use io_pipe::async_reader_pipe;
use std::io::Write;
use futures::io::AsyncReadExt;
use futures::executor::block_on;
fn main() {
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);
})
}
use io_pipe::async_writer_pipe;
use std::io::Read;
use futures::io::AsyncWriteExt;
use futures::executor::block_on;
fn main() {
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);
}
For detailed API documentation, please refer to docs.rs.
Contributions are welcome! Please feel free to submit a Pull Request.
Guidelines:
rustfmt
to format your code.clippy
and address any lints before submitting your PR.This project is licensed under MIT - see the LICENSE file for details.