1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Copyright © 2016 Bart Massey
// This work is made available under the "MIT License".
// Please see the file COPYING in this distribution for
// license terms.
//! Create a UNIX pipe with read and write endpoints.
extern crate libc;
use File;
use ;
use c_int;
use pipe as raw_pipe;
use FromRawFd;
/// A pipe has two ends and no middle.
/// Make a new pipe.
///
/// # Examples
///
/// ```
/// use std::io::{Write, BufReader, BufRead};
///
/// let p = pipefile::pipe().expect("couldn't create pipe");
/// // Start the write.
/// let mut writer = p.write_end;
/// let write_thread = std::thread::spawn(move || {
/// writer.write("hello world".as_bytes())
/// .expect("couldn't write message");
/// });
/// // Do the read.
/// let mut reader = BufReader::new(&p.read_end);
/// let mut message = String::new();
/// reader.read_line(&mut message)
/// .expect("couldn't read message");
/// // Clean up and check.
/// write_thread.join()
/// .expect("couldn't finish writer");
/// assert_eq!(message, "hello world");
/// ```