unix_fifo_async/lib.rs
1/*!
2**\[WIP\]** Eases working with Unix named pipes (FIFOs) anywhere on the filesystem.
3
4Because of the way this works currently, there's no real way to get a
5lock on the pipe, but there are convenience methods on both `NamedPipePath`
6and `NamedPipeReader`/`NamedPipeWriter` to ensure the pipe exists.
7
8# Example
9
10Create a pipe, write to it in one async task and read from it in another:
11
12```
13# fn main() -> async_std::io::Result<()> { async_std::task::block_on(async {
14
15use unix_fifo_async::NamedPipePath;
16use async_std::task;
17
18// Create a new pipe at the given path
19let pipe = NamedPipePath::new("./my_pipe");
20// This creates the path if it doesn't exist; it may return a nix::Error
21// You can also use the `ensure_pipe_exists` convenience method on
22// readers/writers, but calling it on both at the same time results
23// in a race condition so it can never succeed.
24pipe.ensure_exists().unwrap();
25// Create a writer and a reader on the path
26let writer = pipe.open_write();
27let reader = pipe.open_read();
28
29// Some data we can send over the pipe
30let data_to_send = "Hello, pipes!";
31
32// Spawn two tasks, one for writing to and one for reading from the pipe.
33let t1 = task::spawn(async move { writer.write_str(data_to_send).await });
34let t2 = task::spawn(async move { reader.read_string().await });
35
36// `.await` both tasks and compare the result with the original
37t1.await?;
38let read_result = t2.await?;
39assert_eq!(read_result, data_to_send);
40
41// Delete the pipe
42pipe.delete().await?;
43# Ok(())
44# })}
45```
46
47Note that in practice, you'll probably want to read the pipe from a different
48process or have it read by an entirely different program.
49*/
50
51mod named_pipe;
52
53pub mod util;
54pub use named_pipe::{NamedPipePath, NamedPipeReader, NamedPipeWriter};
55pub use util::{create_pipe, remove_pipe};