Skip to content

Commit b2619df

Browse files
oech3cakebaker
authored andcommitted
unexpand: remove Box dyn
1 parent 07d6f27 commit b2619df

1 file changed

Lines changed: 19 additions & 6 deletions

File tree

src/uu/unexpand/src/unexpand.rs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use clap::{Arg, ArgAction, Command};
99
use std::ffi::OsString;
1010
use std::fs::File;
11-
use std::io::{BufReader, BufWriter, Read, Stdout, Write, stdin, stdout};
11+
use std::io::{self, BufReader, BufWriter, Read, Stdin, Stdout, Write, stdin, stdout};
1212
use std::num::IntErrorKind;
1313
use std::path::Path;
1414
use std::str::from_utf8;
@@ -279,19 +279,32 @@ pub fn uu_app() -> Command {
279279
)
280280
}
281281

282-
fn open(path: &OsString) -> UResult<BufReader<Box<dyn Read + 'static>>> {
283-
let file_buf;
282+
enum Input {
283+
Stdin(Stdin),
284+
File(File),
285+
}
286+
287+
impl Read for Input {
288+
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
289+
match self {
290+
Self::Stdin(s) => s.read(buf),
291+
Self::File(f) => f.read(buf),
292+
}
293+
}
294+
}
295+
296+
fn open(path: &OsString) -> UResult<BufReader<Input>> {
284297
let filename = Path::new(path);
285298
if filename.is_dir() {
286299
Err(USimpleError::new(
287300
1,
288301
translate!("unexpand-error-is-directory", "path" => filename.maybe_quote()),
289302
))
290303
} else if path == "-" {
291-
Ok(BufReader::new(Box::new(stdin()) as Box<dyn Read>))
304+
Ok(BufReader::new(Input::Stdin(stdin())))
292305
} else {
293-
file_buf = File::open(path).map_err_context(|| path.maybe_quote().to_string())?;
294-
Ok(BufReader::new(Box::new(file_buf) as Box<dyn Read>))
306+
let f = File::open(path).map_err_context(|| path.maybe_quote().to_string())?;
307+
Ok(BufReader::new(Input::File(f)))
295308
}
296309
}
297310

0 commit comments

Comments
 (0)