Skip to content
This repository was archived by the owner on Dec 15, 2021. It is now read-only.

Commit 50f92a1

Browse files
author
Alex Helfet
committed
Extract function open_read() to initialise io.
1 parent fc04180 commit 50f92a1

File tree

1 file changed

+46
-42
lines changed

1 file changed

+46
-42
lines changed

src/bin/itmdump.rs

Lines changed: 46 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#![deny(warnings)]
2+
#![feature(conservative_impl_trait)]
23

34
extern crate chrono;
45
extern crate clap;
@@ -25,7 +26,7 @@ use std::fs::File;
2526
#[cfg(unix)]
2627
use std::os::unix::ffi::OsStringExt;
2728

28-
use clap::{App, Arg};
29+
use clap::{Arg, App, ArgMatches};
2930
use log::{LogRecord, LogLevelFilter};
3031
use ref_slice::ref_slice_mut;
3132

@@ -93,56 +94,18 @@ fn run() -> Result<()> {
9394
}))
9495
.get_matches();
9596

96-
let pipe = PathBuf::from(matches.value_of("PATH").unwrap());
97-
let pipe_ = pipe.display();
98-
9997
let stim_port = matches.value_of("port")
10098
.unwrap() // We supplied a default value
10199
.parse::<u8>()
102100
.expect("Arg validator should ensure this parses");
103101

104-
if pipe.exists() {
105-
try!(fs::remove_file(&pipe)
106-
.chain_err(|| format!("couldn't remove {}", pipe_)));
107-
}
108-
109-
let mut stream = match () {
110-
#[cfg(unix)]
111-
() => {
112-
let cpipe =
113-
try!(CString::new(pipe.clone().into_os_string().into_vec())
114-
.chain_err(|| {
115-
format!("error converting {} to a C string", pipe_)
116-
}));
117-
118-
match unsafe { libc::mkfifo(cpipe.as_ptr(), 0o644) } {
119-
0 => {}
120-
e => {
121-
try!(Err(io::Error::from_raw_os_error(e)).chain_err(|| {
122-
format!("couldn't create a named pipe in {}", pipe_)
123-
}))
124-
}
125-
}
126-
127-
try!(File::open(&pipe)
128-
.chain_err(|| format!("couldn't open {}", pipe_)))
129-
}
130-
#[cfg(not(unix))]
131-
() => {
132-
try!(OpenOptions::new()
133-
.create(true)
134-
.read(true)
135-
.write(true)
136-
.open(&pipe)
137-
.chain_err(|| format!("couldn't open {}", pipe_)))
138-
}
139-
};
140-
141-
let mut header = 0;
102+
let mut stream = open_read(&matches)?;
142103

143104
let stdout = io::stdout();
144105
let mut stdout = stdout.lock();
145106
loop {
107+
let mut header = 0;
108+
146109
if let Err(e) = (|| {
147110
try!(stream.read_exact(ref_slice_mut(&mut header)));
148111
let port = header >> 3;
@@ -186,3 +149,44 @@ fn run() -> Result<()> {
186149
}
187150
}
188151
}
152+
153+
fn open_read(matches: &ArgMatches) -> Result<impl io::Read> {
154+
let pipe = PathBuf::from(matches.value_of("PATH").unwrap());
155+
let pipe_ = pipe.display();
156+
157+
if pipe.exists() {
158+
try!(fs::remove_file(&pipe)
159+
.chain_err(|| format!("couldn't remove {}", pipe_)));
160+
}
161+
162+
Ok(
163+
if cfg!(unix) {
164+
// Use a named pipe.
165+
let cpipe =
166+
try!(CString::new(pipe.clone().into_os_string().into_vec())
167+
.chain_err(|| {
168+
format!("error converting {} to a C string", pipe_)
169+
}));
170+
171+
match unsafe { libc::mkfifo(cpipe.as_ptr(), 0o644) } {
172+
0 => {}
173+
e => {
174+
try!(Err(io::Error::from_raw_os_error(e)).chain_err(|| {
175+
format!("couldn't create a named pipe in {}", pipe_)
176+
}))
177+
}
178+
}
179+
180+
try!(File::open(&pipe)
181+
.chain_err(|| format!("couldn't open {}", pipe_)))
182+
} else {
183+
// Not unix.
184+
try!(OpenOptions::new()
185+
.create(true)
186+
.read(true)
187+
.write(true)
188+
.open(&pipe)
189+
.chain_err(|| format!("couldn't open {}", pipe_)))
190+
}
191+
)
192+
}

0 commit comments

Comments
 (0)