Skip to content

Commit 9618290

Browse files
committed
remove unneeded unwraps
Note, this slightly changes the pub API, so it should require a minor version bump at least. There is still an `unwrap` in a `Drop` where it's unclear if it can happen or it can be handled better (maybe just ignored?) and an `expect` in a function where the expect should not happen (it's checked before), and I didn't consider that it's worth adding a new Error kind for this in particular. Signed-off-by: Petre Eftime <petre.eftime@gmail.com>
1 parent 3c18954 commit 9618290

File tree

3 files changed

+10
-15
lines changed

3 files changed

+10
-15
lines changed

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,5 @@ rust stable, beta and nightly on both Linux or Mac.
149149

150150
- use [nix](https://github.com/nix-rust/nix) (and avoid libc wherever possible)
151151
to keep the code safe and clean
152-
- sadly, `expect` is used in rust too prominently to unwrap `Option`s and
153-
`Result`s, use `exp_*` instead
154152

155153
Licensed under [MIT License](LICENSE)

src/process.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,10 @@ impl PtyProcess {
126126
}
127127

128128
/// Get handle to pty fork for reading/writing
129-
pub fn get_file_handle(&self) -> File {
129+
pub fn get_file_handle(&self) -> Result<File, Error> {
130130
// needed because otherwise fd is closed both by dropping process and reader/writer
131-
let fd = dup(self.pty.as_raw_fd()).unwrap();
132-
unsafe { File::from_raw_fd(fd) }
131+
let fd = dup(self.pty.as_raw_fd())?;
132+
unsafe { Ok(File::from_raw_fd(fd)) }
133133
}
134134

135135
/// At the drop of PtyProcess the running process is killed. This is blocking forever if
@@ -230,14 +230,13 @@ impl Drop for PtyProcess {
230230
mod tests {
231231
use super::*;
232232
use nix::sys::{signal, wait};
233-
use std::io::prelude::*;
234-
use std::io::{BufReader, LineWriter};
233+
use std::io::{BufRead, BufReader, LineWriter, Write};
235234

236235
#[test]
237236
/// Open cat, write string, read back string twice, send Ctrl^C and check that cat exited
238237
fn test_cat() -> std::io::Result<()> {
239238
let process = PtyProcess::new(Command::new("cat")).expect("could not execute cat");
240-
let f = process.get_file_handle();
239+
let f = process.get_file_handle().unwrap();
241240
let mut writer = LineWriter::new(&f);
242241
let mut reader = BufReader::new(&f);
243242
let _ = writer.write(b"hello cat\n")?;

src/session.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ impl DerefMut for PtySession {
192192
/// ```
193193
impl PtySession {
194194
fn new(process: PtyProcess, timeout_ms: Option<u64>) -> Result<Self, Error> {
195-
let f = process.get_file_handle();
195+
let f = process.get_file_handle()?;
196196
let reader = f.try_clone()?;
197197
let stream = StreamSession::new(reader, f, timeout_ms);
198198
Ok(Self { process, stream })
@@ -373,16 +373,14 @@ pub fn spawn_bash(timeout: Option<u64>) -> Result<PtyReplSession, Error> {
373373
// wait for the first prompt since we don't know what .bashrc
374374
// would set as PS1 and we cannot know when is the right time
375375
// to set the new PS1
376-
let mut rcfile = tempfile::NamedTempFile::new().unwrap();
377-
let _ = rcfile
378-
.write(
379-
b"include () { [[ -f \"$1\" ]] && source \"$1\"; }\n\
376+
let mut rcfile = tempfile::NamedTempFile::new()?;
377+
let _ = rcfile.write(
378+
b"include () { [[ -f \"$1\" ]] && source \"$1\"; }\n\
380379
include /etc/bash.bashrc\n\
381380
include ~/.bashrc\n\
382381
PS1=\"~~~~\"\n\
383382
unset PROMPT_COMMAND\n",
384-
)
385-
.expect("cannot write to tmpfile");
383+
)?;
386384
let mut c = Command::new("bash");
387385
c.args(&[
388386
"--rcfile",

0 commit comments

Comments
 (0)