From b2619df6e047a86889835fcb821c6b3d2086540b Mon Sep 17 00:00:00 2001 From: oech3 <79379754+oech3@users.noreply.github.com> Date: Tue, 14 Apr 2026 20:48:07 +0900 Subject: [PATCH] unexpand: remove Box dyn --- src/uu/unexpand/src/unexpand.rs | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/src/uu/unexpand/src/unexpand.rs b/src/uu/unexpand/src/unexpand.rs index 9c963f7ae..c4f090ed7 100644 --- a/src/uu/unexpand/src/unexpand.rs +++ b/src/uu/unexpand/src/unexpand.rs @@ -8,7 +8,7 @@ use clap::{Arg, ArgAction, Command}; use std::ffi::OsString; use std::fs::File; -use std::io::{BufReader, BufWriter, Read, Stdout, Write, stdin, stdout}; +use std::io::{self, BufReader, BufWriter, Read, Stdin, Stdout, Write, stdin, stdout}; use std::num::IntErrorKind; use std::path::Path; use std::str::from_utf8; @@ -279,8 +279,21 @@ pub fn uu_app() -> Command { ) } -fn open(path: &OsString) -> UResult>> { - let file_buf; +enum Input { + Stdin(Stdin), + File(File), +} + +impl Read for Input { + fn read(&mut self, buf: &mut [u8]) -> io::Result { + match self { + Self::Stdin(s) => s.read(buf), + Self::File(f) => f.read(buf), + } + } +} + +fn open(path: &OsString) -> UResult> { let filename = Path::new(path); if filename.is_dir() { Err(USimpleError::new( @@ -288,10 +301,10 @@ fn open(path: &OsString) -> UResult>> { translate!("unexpand-error-is-directory", "path" => filename.maybe_quote()), )) } else if path == "-" { - Ok(BufReader::new(Box::new(stdin()) as Box)) + Ok(BufReader::new(Input::Stdin(stdin()))) } else { - file_buf = File::open(path).map_err_context(|| path.maybe_quote().to_string())?; - Ok(BufReader::new(Box::new(file_buf) as Box)) + let f = File::open(path).map_err_context(|| path.maybe_quote().to_string())?; + Ok(BufReader::new(Input::File(f))) } }