#io-stream #pipe #thread #filter #fifo

runnel

可插拔的 I/O 流。现在支持:标准输入输出、字符串 I/O、内存管道

25 个版本

0.3.19 2024 年 6 月 19 日
0.3.17 2023 年 2 月 12 日
0.3.11 2022 年 6 月 13 日
0.3.9 2021 年 11 月 14 日
0.3.4 2021 年 3 月 8 日

#77并发

Download history 38/week @ 2024-04-27 31/week @ 2024-05-04 41/week @ 2024-05-11 35/week @ 2024-05-18 60/week @ 2024-05-25 56/week @ 2024-06-01 247/week @ 2024-06-08 755/week @ 2024-06-15 78/week @ 2024-06-22 10/week @ 2024-06-29 53/week @ 2024-07-06 41/week @ 2024-07-13 22/week @ 2024-07-20 206/week @ 2024-07-27 42/week @ 2024-08-03 57/week @ 2024-08-10

333 每月下载量
用于 12 crates

MIT/Apache

47KB
1K SLoC

runnel

crate Docs Rust Version Apache2/MIT licensed Test ubu Test mac Test win

可插拔的 I/O 流。现在支持:标准输入输出、字符串 I/O、内存管道。

功能

  • 支持常见操作:标准输入、标准输出、标准错误、字符串输入、字符串输出、管道输入和管道输出。
  • 接口轻薄
  • 支持测试流 I/O
  • 最低支持 rustc 1.57.0 (f1edd0429 2021-11-29)

示例

stdio 示例

use runnel::RunnelIoeBuilder;
let sioe = RunnelIoeBuilder::new().build();

stringio 示例

use runnel::RunnelIoeBuilder;
use std::io::{BufRead, Write};

let sioe = RunnelIoeBuilder::new()
    .fill_stringio_with_str("ABCDE\nefgh\n")
    .build();

// pluggable stream in
let mut lines_iter = sioe.pin().lock().lines().map(|l| l.unwrap());
assert_eq!(lines_iter.next(), Some(String::from("ABCDE")));
assert_eq!(lines_iter.next(), Some(String::from("efgh")));
assert_eq!(lines_iter.next(), None);

// pluggable stream out
#[rustfmt::skip]
let res = sioe.pout().lock()
    .write_fmt(format_args!("{}\nACBDE\nefgh\n", 1234));
assert!(res.is_ok());
assert_eq!(sioe.pout().lock().buffer_str(), "1234\nACBDE\nefgh\n");

// pluggable stream err
#[rustfmt::skip]
let res = sioe.perr().lock()
    .write_fmt(format_args!("{}\nACBDE\nefgh\n", 1234));
assert!(res.is_ok());
assert_eq!(sioe.perr().lock().buffer_str(), "1234\nACBDE\nefgh\n");

pipeio 示例

use runnel::RunnelIoeBuilder;
use runnel::medium::pipeio::pipe;
use std::io::{BufRead, Write};

// create in memory pipe
let (a_out, a_in) = pipe(1);

// a working thread
let sioe = RunnelIoeBuilder::new()
    .fill_stringio_with_str("ABCDE\nefgh\n")
    .pout(a_out)    // pluggable pipe out
    .build();
let handler = std::thread::spawn(move || {
    for line in sioe.pin().lock().lines().map(|l| l.unwrap()) {
        let mut out = sioe.pout().lock();
        let _ = out.write_fmt(format_args!("{}\n", line));
        let _ = out.flush();
    }
});

// a main thread
let sioe = RunnelIoeBuilder::new()
    .fill_stringio_with_str("ABCDE\nefgh\n")
    .pin(a_in)      // pluggable pipe in
    .build();
let mut lines_iter = sioe.pin().lock().lines().map(|l| l.unwrap());
assert_eq!(lines_iter.next(), Some(String::from("ABCDE")));
assert_eq!(lines_iter.next(), Some(String::from("efgh")));
assert_eq!(lines_iter.next(), None);

assert!(handler.join().is_ok());

变更日志

此 crate 的变更日志在此。

许可

该项目受以下任一许可证的许可:

您可选择。

无运行时依赖