Skip to content

Commit 7d37a31

Browse files
committed
Add comments to dup2_to_replace_stdio.
And while here, simplify the code to use `println!` to print "hello world", which we can do now that it's no longer running within the test harness process.
1 parent dd291ef commit 7d37a31

File tree

1 file changed

+30
-16
lines changed

1 file changed

+30
-16
lines changed

examples/dup2_to_replace_stdio.rs

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,49 @@
33
44
#[cfg(not(windows))]
55
fn main() {
6-
use io_lifetimes::AsFilelike;
76
use rustix::io::{dup2, pipe};
8-
use std::io::{BufRead, BufReader, Write};
7+
use std::io::{BufRead, BufReader};
98
use std::mem::forget;
109

10+
// Create some new file descriptors that we'll use to replace stdio's file
11+
// descriptors with.
1112
let (reader, writer) = pipe().unwrap();
13+
14+
// Acquire `OwnedFd` instances for stdin and stdout. These APIs are `unsafe`
15+
// because in general, with low-level APIs like this, libraries can't assume
16+
// that stdin and stdout will be open or safe to use. It's ok here, because
17+
// we're directly inside `main`, so we know that stdin and stdout haven't
18+
// been closed and aren't being used for other purposes.
1219
let (stdin, stdout) = unsafe { (rustix::io::take_stdin(), rustix::io::take_stdout()) };
20+
21+
// Use `dup2` to copy our new file descriptors over the stdio file descriptors.
22+
//
23+
// These take their second argument as an `&OwnedFd` rather than the usual
24+
// `impl AsFd` because they conceptually do a `close` on the original file
25+
// descriptor, which one shouldn't be able to do with just a `BorrowedFd`.
1326
dup2(&reader, &stdin).unwrap();
1427
dup2(&writer, &stdout).unwrap();
28+
29+
// Then, forget the stdio `OwnedFd`s, because actually dropping them would
30+
// close them. Here, we want stdin and stdout to remain open for the rest
31+
// of the program.
1532
forget(stdin);
1633
forget(stdout);
1734

35+
// We can also drop the original file descriptors now, since `dup2` creates
36+
// new file descriptors with independent lifetimes.
1837
drop(reader);
1938
drop(writer);
2039

21-
// Don't use `std::io::stdout()` because in tests it's captured.
22-
unsafe {
23-
writeln!(
24-
rustix::io::stdout().as_filelike_view::<std::fs::File>(),
25-
"hello, world!"
26-
)
27-
.unwrap();
28-
29-
let mut s = String::new();
30-
BufReader::new(&*rustix::io::stdin().as_filelike_view::<std::fs::File>())
31-
.read_line(&mut s)
32-
.unwrap();
33-
assert_eq!(s, "hello, world!\n");
34-
}
40+
// Now we can print to "stdout" in the usual way, and it'll go to our pipe.
41+
println!("hello, world!");
42+
43+
// And we can read from stdin, and it'll read from our pipe. It's a little
44+
// silly that we connected our stdout to our own stdin, but it's just an
45+
// example :-).
46+
let mut s = String::new();
47+
BufReader::new(std::io::stdin()).read_line(&mut s).unwrap();
48+
assert_eq!(s, "hello, world!\n");
3549
}
3650

3751
#[cfg(windows)]

0 commit comments

Comments
 (0)