#pipe #ipc #fifo

ipipe

Cross-platform named-pipe API

31 releases

0.11.0 Jan 12, 2022
0.9.0 Nov 27, 2021
0.8.2 Apr 8, 2021
0.8.0 Mar 31, 2021

#121 in Filesystem

Download history 7/week @ 2021-10-01 37/week @ 2021-10-08 5/week @ 2021-10-15 13/week @ 2021-10-22 3/week @ 2021-10-29 47/week @ 2021-11-05 11/week @ 2021-11-12 15/week @ 2021-11-19 27/week @ 2021-11-26 68/week @ 2021-12-03 28/week @ 2021-12-10 11/week @ 2021-12-17 5/week @ 2021-12-24 43/week @ 2021-12-31 114/week @ 2022-01-07 39/week @ 2022-01-14

202 downloads per month
Used in 3 crates

MIT/Apache

39KB
984 lines

ipipe - A cross-platform named-pipe library for Rust

This library allows the creation of platform-independant named pipes. Standard Read/Write traits are implemented. APIs and performance will be improved in future versions. Issues and PRs welcome.

Example:


use ipipe::Pipe;
use std::thread;
use std::io::{BufRead, BufWriter};

const CANCEL: u8 = 24;

fn main()
{
    let mut pipe = Pipe::create().unwrap();
    println!("Name: {}", pipe.path().display());

    let writer = pipe.clone();
    thread::spawn(move || print_nums(writer));
    for line in BufReader::new(pipe).lines()
    {
        println!("{}", line.unwrap());
    }
}

fn print_nums(mut pipe: Pipe)
{
    for i in 1..=10
    {
        writeln!(&mut pipe, "{}", i).unwrap();
    }
    write!(&mut pipe, "{}", CANCEL as char).unwrap();
}

Running the above example program will output:

1
2
3
4
5
6
7
8
9
10

Pipe::create generates a random pipe name in a temporary location. Example path (Windows): \\.\pipe\pipe_23676_xMvclVhNKcg6iGf Example path (Unix): /tmp/pipe_1230_mFP8dx8uVl

Pipe::with_name allows a pipe name to be specified.

Features

  • static_pipe

The static_pipe default feature allows the creation of mutex-protected static pipes that can be written to from anywhere in a way that mimics stdout. Here's an example:

use ipipe::*;
use std::io::{BufRead, BufWriter};

let mut reader = ipipe::init("my_out").unwrap();

// You can get a handle to an already-initialized pipe like this:
// let mut reader = ipipe::get("my_pipe");
let s = BufReader::new(pipe).lines().next().unwrap();
println!("String received: {}", s);

// Drops the static pipe. Can also call `ipipe::close_all()` to drop all static pipes.
ipipe::close("my_out");

Then anywhere your program (or another program with enough permission to access the pipe) can write code like this:

pprintln!("my_pipe", "This text will be sent over the pipe!");

Lower level as well as more complete/intuitive APIs to the static pipes are also planned for a future release.

  • rand

The rand default feature will allow calling Pipe::create() to open a pipe with a randomly-generated name. The generated name will have the following format: pipe_[process pid]_[15 random alphnumeric characters]. Equivalent to Pipe::with_name(&str) in every other way.

  • channels

The channels feature will allow calling pipe.receiver() and pipe.sender() to generate a channel. One end of the channel will be sent to a thread to watch either input or output from the pipe, and the other end of the channel will be returned.

  • tokio_channels

Equivalent to the channels feature, but uses tokio::task in place of std::thread.

Dependencies

~0.3–1.4MB
~24K SLoC

6a