Skip to content

Commit 87278a3

Browse files
authored
Don't use .unwrap() or unimplemented in examples. (#748)
Panicking isn't super nice for people who are using the examples to learn their way around.
1 parent cf046e7 commit 87278a3

File tree

7 files changed

+22
-20
lines changed

7 files changed

+22
-20
lines changed

examples/dup2_to_replace_stdio.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@
22
//! file descriptors.
33
44
#[cfg(all(not(windows), feature = "pipe", feature = "stdio"))]
5-
fn main() {
5+
fn main() -> std::io::Result<()> {
66
use rustix::pipe::pipe;
77
use rustix::stdio::{dup2_stdin, dup2_stdout};
88
use std::io::{BufRead, BufReader};
99

1010
// Create some new file descriptors that we'll use to replace stdio's file
1111
// descriptors with.
12-
let (reader, writer) = pipe().unwrap();
12+
let (reader, writer) = pipe()?;
1313

1414
// Use `dup2` to copy our new file descriptors over the stdio file descriptors.
1515
//
1616
// Rustix has a plain `dup2` function too, but it requires a `&mut OwnedFd`,
1717
// so these helper functions make it easier to use when replacing stdio fds.
18-
dup2_stdin(&reader).unwrap();
19-
dup2_stdout(&writer).unwrap();
18+
dup2_stdin(&reader)?;
19+
dup2_stdout(&writer)?;
2020

2121
// We can also drop the original file descriptors now, since `dup2` creates
2222
// new file descriptors with independent lifetimes.
@@ -30,11 +30,13 @@ fn main() {
3030
// silly that we connected our stdout to our own stdin, but it's just an
3131
// example :-).
3232
let mut s = String::new();
33-
BufReader::new(std::io::stdin()).read_line(&mut s).unwrap();
33+
BufReader::new(std::io::stdin()).read_line(&mut s)?;
3434
assert_eq!(s, "hello, world!\n");
35+
36+
Ok(())
3537
}
3638

3739
#[cfg(not(all(not(windows), feature = "pipe", feature = "stdio")))]
38-
fn main() {
39-
unimplemented!()
40+
fn main() -> Result<(), &'static str> {
41+
Err("This example requires --features=pipe,stdio and is not supported on Windows.")
4042
}

examples/hello.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,6 @@ fn main() -> std::io::Result<()> {
3434
}
3535

3636
#[cfg(any(not(feature = "stdio"), not(feature = "std"), windows))]
37-
fn main() {
38-
unimplemented!()
37+
fn main() -> Result<(), &'static str> {
38+
Err("This example requires --features=stdio,std and is not supported on Windows.")
3939
}

examples/kq.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,6 @@ fn main() -> std::io::Result<()> {
8484
}
8585

8686
#[cfg(not(all(bsd, feature = "event")))]
87-
fn main() {
88-
unimplemented!()
87+
fn main() -> Result<(), &'static str> {
88+
Err("This example requires --features=event and is only supported on BSD.")
8989
}

examples/process.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ fn main() -> io::Result<()> {
1616
if let Some(ppid) = getppid() {
1717
println!(
1818
"Parent Group Pid: {}",
19-
getpgid(Some(ppid)).unwrap().as_raw_nonzero()
19+
getpgid(Some(ppid))?.as_raw_nonzero()
2020
);
2121
}
2222
println!("Uid: {}", getuid().as_raw());
@@ -91,6 +91,6 @@ fn main() -> io::Result<()> {
9191
}
9292

9393
#[cfg(any(windows, not(all(feature = "process", feature = "param"))))]
94-
fn main() -> io::Result<()> {
95-
unimplemented!()
94+
fn main() -> Result<(), &'static str> {
95+
Err("This example requires --features=process,param and is not supported on Windows.")
9696
}

examples/stdio.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,6 @@ fn key(b: u8) -> String {
343343
}
344344

345345
#[cfg(any(windows, not(feature = "stdio")))]
346-
fn main() {
347-
unimplemented!()
346+
fn main() -> Result<(), &'static str> {
347+
Err("This example requires --features=stdio and is not supported on Windows.")
348348
}

examples/termios.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,6 @@ fn main() -> std::io::Result<()> {
4444
}
4545

4646
#[cfg(any(windows, not(feature = "termios")))]
47-
fn main() {
48-
unimplemented!()
47+
fn main() -> Result<(), &'static str> {
48+
Err("This example requires --features=termios and is not supported on Windows.")
4949
}

examples/time.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,6 @@ fn main() {
4444
}
4545

4646
#[cfg(any(windows, target_os = "espidf", not(feature = "time")))]
47-
fn main() {
48-
unimplemented!()
47+
fn main() -> Result<(), &'static str> {
48+
Err("This example requires --features=time and is not supported on Windows or ESP-IDF.")
4949
}

0 commit comments

Comments
 (0)