1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-27 19:17:43 +00:00

Merge pull request #1430 from bippityboppity/add-dyn

Add the 'dyn' keyword where necessary
This commit is contained in:
cnd 2019-10-02 10:31:03 +04:00 committed by GitHub
commit 91899b34b9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
31 changed files with 148 additions and 135 deletions

View file

@ -100,7 +100,7 @@ struct OutputOptions {
/// Represents an open file handle, stream, or other device
struct InputHandle {
reader: Box<Read>,
reader: Box<dyn Read>,
is_interactive: bool,
}
@ -241,7 +241,7 @@ fn open(path: &str) -> CatResult<InputHandle> {
if path == "-" {
let stdin = stdin();
return Ok(InputHandle {
reader: Box::new(stdin) as Box<Read>,
reader: Box::new(stdin) as Box<dyn Read>,
is_interactive: is_stdin_interactive(),
});
}
@ -253,14 +253,14 @@ fn open(path: &str) -> CatResult<InputHandle> {
let socket = UnixStream::connect(path).context(path)?;
socket.shutdown(Shutdown::Write).context(path)?;
Ok(InputHandle {
reader: Box::new(socket) as Box<Read>,
reader: Box::new(socket) as Box<dyn Read>,
is_interactive: false,
})
}
_ => {
let file = File::open(path).context(path)?;
Ok(InputHandle {
reader: Box::new(file) as Box<Read>,
reader: Box::new(file) as Box<dyn Read>,
is_interactive: false,
})
}

View file

@ -55,7 +55,7 @@ fn cksum(fname: &str) -> io::Result<(u32, usize)> {
let mut size = 0usize;
let file;
let mut rd: Box<Read> = match fname {
let mut rd: Box<dyn Read> = match fname {
"-" => Box::new(stdin()),
_ => {
file = File::open(&Path::new(fname))?;

View file

@ -137,7 +137,7 @@ pub fn uumain(args: Vec<String>) -> i32 {
}
// Iterate over all dates - whether it's a single date or a file.
let dates: Box<Iterator<Item = _>> = match settings.date_source {
let dates: Box<dyn Iterator<Item = _>> = match settings.date_source {
DateSource::Custom(ref input) => {
let date = parse_date(input.clone());
let iter = std::iter::once(date);

View file

@ -142,7 +142,7 @@ fn du(
options: &Options,
depth: usize,
inodes: &mut HashSet<u64>,
) -> Box<DoubleEndedIterator<Item = Stat>> {
) -> Box<dyn DoubleEndedIterator<Item = Stat>> {
let mut stats = vec![];
let mut futures = vec![];

View file

@ -128,16 +128,16 @@ pub fn uumain(args: Vec<String>) -> i32 {
0
}
fn open(path: String) -> BufReader<Box<Read + 'static>> {
fn open(path: String) -> BufReader<Box<dyn Read + 'static>> {
let file_buf;
if path == "-" {
BufReader::new(Box::new(stdin()) as Box<Read>)
BufReader::new(Box::new(stdin()) as Box<dyn Read>)
} else {
file_buf = match File::open(&path[..]) {
Ok(a) => a,
Err(e) => crash!(1, "{}: {}\n", &path[..], e),
};
BufReader::new(Box::new(file_buf) as Box<Read>)
BufReader::new(Box::new(file_buf) as Box<dyn Read>)
}
}

View file

@ -38,7 +38,7 @@ static SYNTAX: &str = "[OPTION]... [FILE]...";
static SUMMARY: &str = "Reformat paragraphs from input files (or stdin) to stdout.";
static LONG_HELP: &str = "";
pub type FileOrStdReader = BufReader<Box<Read + 'static>>;
pub type FileOrStdReader = BufReader<Box<dyn Read + 'static>>;
pub struct FmtOptions {
crown: bool,
tagged: bool,
@ -179,9 +179,9 @@ pub fn uumain(args: Vec<String>) -> i32 {
for i in files.iter().map(|x| &x[..]) {
let mut fp = match i {
"-" => BufReader::new(Box::new(stdin()) as Box<Read + 'static>),
"-" => BufReader::new(Box::new(stdin()) as Box<dyn Read + 'static>),
_ => match File::open(i) {
Ok(f) => BufReader::new(Box::new(f) as Box<Read + 'static>),
Ok(f) => BufReader::new(Box::new(f) as Box<dyn Read + 'static>),
Err(e) => {
show_warning!("{}: {}", i, e);
continue;

View file

@ -89,10 +89,10 @@ fn fold(filenames: Vec<String>, bytes: bool, spaces: bool, width: usize) {
let mut file_buf;
let buffer = BufReader::new(if filename == "-" {
stdin_buf = stdin();
&mut stdin_buf as &mut Read
&mut stdin_buf as &mut dyn Read
} else {
file_buf = safe_unwrap!(File::open(Path::new(filename)));
&mut file_buf as &mut Read
&mut file_buf as &mut dyn Read
});
fold_file(buffer, bytes, spaces, width);
}

View file

@ -51,23 +51,23 @@ fn is_custom_binary(program: &str) -> bool {
fn detect_algo(
program: &str,
matches: &getopts::Matches,
) -> (&'static str, Box<Digest + 'static>, usize) {
let mut alg: Option<Box<Digest>> = None;
) -> (&'static str, Box<dyn Digest + 'static>, usize) {
let mut alg: Option<Box<dyn Digest>> = None;
let mut name: &'static str = "";
let mut output_bits = 0;
match program {
"md5sum" => ("MD5", Box::new(Md5::new()) as Box<Digest>, 128),
"sha1sum" => ("SHA1", Box::new(Sha1::new()) as Box<Digest>, 160),
"sha224sum" => ("SHA224", Box::new(Sha224::new()) as Box<Digest>, 224),
"sha256sum" => ("SHA256", Box::new(Sha256::new()) as Box<Digest>, 256),
"sha384sum" => ("SHA384", Box::new(Sha384::new()) as Box<Digest>, 384),
"sha512sum" => ("SHA512", Box::new(Sha512::new()) as Box<Digest>, 512),
"md5sum" => ("MD5", Box::new(Md5::new()) as Box<dyn Digest>, 128),
"sha1sum" => ("SHA1", Box::new(Sha1::new()) as Box<dyn Digest>, 160),
"sha224sum" => ("SHA224", Box::new(Sha224::new()) as Box<dyn Digest>, 224),
"sha256sum" => ("SHA256", Box::new(Sha256::new()) as Box<dyn Digest>, 256),
"sha384sum" => ("SHA384", Box::new(Sha384::new()) as Box<dyn Digest>, 384),
"sha512sum" => ("SHA512", Box::new(Sha512::new()) as Box<dyn Digest>, 512),
"sha3sum" => match matches.opt_str("bits") {
Some(bits_str) => match usize::from_str_radix(&bits_str, 10) {
Ok(224) => ("SHA3-224", Box::new(Sha3_224::new()) as Box<Digest>, 224),
Ok(256) => ("SHA3-256", Box::new(Sha3_256::new()) as Box<Digest>, 256),
Ok(384) => ("SHA3-384", Box::new(Sha3_384::new()) as Box<Digest>, 384),
Ok(512) => ("SHA3-512", Box::new(Sha3_512::new()) as Box<Digest>, 512),
Ok(224) => ("SHA3-224", Box::new(Sha3_224::new()) as Box<dyn Digest>, 224),
Ok(256) => ("SHA3-256", Box::new(Sha3_256::new()) as Box<dyn Digest>, 256),
Ok(384) => ("SHA3-384", Box::new(Sha3_384::new()) as Box<dyn Digest>, 384),
Ok(512) => ("SHA3-512", Box::new(Sha3_512::new()) as Box<dyn Digest>, 512),
Ok(_) => crash!(
1,
"Invalid output size for SHA3 (expected 224, 256, 384, or 512)"
@ -76,20 +76,20 @@ fn detect_algo(
},
None => crash!(1, "--bits required for SHA3"),
},
"sha3-224sum" => ("SHA3-224", Box::new(Sha3_224::new()) as Box<Digest>, 224),
"sha3-256sum" => ("SHA3-256", Box::new(Sha3_256::new()) as Box<Digest>, 256),
"sha3-384sum" => ("SHA3-384", Box::new(Sha3_384::new()) as Box<Digest>, 384),
"sha3-512sum" => ("SHA3-512", Box::new(Sha3_512::new()) as Box<Digest>, 512),
"sha3-224sum" => ("SHA3-224", Box::new(Sha3_224::new()) as Box<dyn Digest>, 224),
"sha3-256sum" => ("SHA3-256", Box::new(Sha3_256::new()) as Box<dyn Digest>, 256),
"sha3-384sum" => ("SHA3-384", Box::new(Sha3_384::new()) as Box<dyn Digest>, 384),
"sha3-512sum" => ("SHA3-512", Box::new(Sha3_512::new()) as Box<dyn Digest>, 512),
"shake128sum" => match matches.opt_str("bits") {
Some(bits_str) => match usize::from_str_radix(&bits_str, 10) {
Ok(bits) => ("SHAKE128", Box::new(Shake128::new()) as Box<Digest>, bits),
Ok(bits) => ("SHAKE128", Box::new(Shake128::new()) as Box<dyn Digest>, bits),
Err(err) => crash!(1, "{}", err),
},
None => crash!(1, "--bits required for SHAKE-128"),
},
"shake256sum" => match matches.opt_str("bits") {
Some(bits_str) => match usize::from_str_radix(&bits_str, 10) {
Ok(bits) => ("SHAKE256", Box::new(Shake256::new()) as Box<Digest>, bits),
Ok(bits) => ("SHAKE256", Box::new(Shake256::new()) as Box<dyn Digest>, bits),
Err(err) => crash!(1, "{}", err),
},
None => crash!(1, "--bits required for SHAKE-256"),
@ -127,22 +127,22 @@ fn detect_algo(
Some(bits_str) => match usize::from_str_radix(&bits_str, 10) {
Ok(224) => set_or_crash(
"SHA3-224",
Box::new(Sha3_224::new()) as Box<Digest>,
Box::new(Sha3_224::new()) as Box<dyn Digest>,
224,
),
Ok(256) => set_or_crash(
"SHA3-256",
Box::new(Sha3_256::new()) as Box<Digest>,
Box::new(Sha3_256::new()) as Box<dyn Digest>,
256,
),
Ok(384) => set_or_crash(
"SHA3-384",
Box::new(Sha3_384::new()) as Box<Digest>,
Box::new(Sha3_384::new()) as Box<dyn Digest>,
384,
),
Ok(512) => set_or_crash(
"SHA3-512",
Box::new(Sha3_512::new()) as Box<Digest>,
Box::new(Sha3_512::new()) as Box<dyn Digest>,
512,
),
Ok(_) => crash!(
@ -369,7 +369,7 @@ Compute and check message digests.",
fn hashsum(
algoname: &str,
mut digest: Box<Digest>,
mut digest: Box<dyn Digest>,
files: Vec<String>,
binary: bool,
check: bool,
@ -389,10 +389,10 @@ fn hashsum(
let file_buf;
let mut file = BufReader::new(if filename == "-" {
stdin_buf = stdin();
Box::new(stdin_buf) as Box<Read>
Box::new(stdin_buf) as Box<dyn Read>
} else {
file_buf = safe_unwrap!(File::open(filename));
Box::new(file_buf) as Box<Read>
Box::new(file_buf) as Box<dyn Read>
});
if check {
// Set up Regexes for line validation and parsing
@ -440,7 +440,7 @@ fn hashsum(
},
};
let f = safe_unwrap!(File::open(ck_filename));
let mut ckf = BufReader::new(Box::new(f) as Box<Read>);
let mut ckf = BufReader::new(Box::new(f) as Box<dyn Read>);
let real_sum = safe_unwrap!(digest_reader(
&mut digest,
&mut ckf,
@ -482,7 +482,7 @@ fn hashsum(
}
fn digest_reader<'a, T: Read>(
digest: &mut Box<Digest + 'a>,
digest: &mut Box<dyn Digest + 'a>,
reader: &mut BufReader<T>,
binary: bool,
output_bits: usize,

View file

@ -235,7 +235,7 @@ struct State<'a> {
file_name: &'a str,
file_num: FileNum,
print_unpaired: bool,
lines: Lines<Box<BufRead + 'a>>,
lines: Lines<Box<dyn BufRead + 'a>>,
seq: Vec<Line>,
max_fields: Option<usize>,
line_num: usize,
@ -251,10 +251,10 @@ impl<'a> State<'a> {
print_unpaired: FileNum,
) -> State<'a> {
let f = if name == "-" {
Box::new(stdin.lock()) as Box<BufRead>
Box::new(stdin.lock()) as Box<dyn BufRead>
} else {
match File::open(name) {
Ok(file) => Box::new(BufReader::new(file)) as Box<BufRead>,
Ok(file) => Box::new(BufReader::new(file)) as Box<dyn BufRead>,
Err(err) => crash!(1, "{}: {}", name, err),
}
};

View file

@ -8,13 +8,13 @@ pub enum InputSource<'a> {
FileName(&'a str),
Stdin,
#[allow(dead_code)]
Stream(Box<io::Read>),
Stream(Box<dyn io::Read>),
}
// MultifileReader - concatenate all our input, file or stdin.
pub struct MultifileReader<'a> {
ni: Vec<InputSource<'a>>,
curr_file: Option<Box<io::Read>>,
curr_file: Option<Box<dyn io::Read>>,
any_err: bool,
}

View file

@ -138,7 +138,7 @@ impl OutputInfo {
/// This algorithm assumes the size of all types is a power of 2 (1, 2, 4, 8, 16, ...)
/// Increase MAX_BYTES_PER_UNIT to allow larger types.
fn calculate_alignment(
sf: &TypeSizeInfo,
sf: &dyn TypeSizeInfo,
byte_size_block: usize,
print_width_block: usize,
) -> [usize; MAX_BYTES_PER_UNIT] {

View file

@ -38,7 +38,7 @@ pub enum CommandLineInputs {
/// Offset and label are specified in bytes.
/// '-' is used as filename if stdin is meant. This is also returned if
/// there is no input, as stdin is the default input.
pub fn parse_inputs(matches: &CommandLineOpts) -> Result<CommandLineInputs, String> {
pub fn parse_inputs(matches: &dyn CommandLineOpts) -> Result<CommandLineInputs, String> {
let mut input_strings: Vec<String> = matches.inputs();
if matches.opts_present(&["traditional"]) {

View file

@ -68,14 +68,14 @@ FILE, separated by TABs, to standard output.",
}
fn paste(filenames: Vec<String>, serial: bool, delimiters: String) {
let mut files: Vec<BufReader<Box<Read>>> = filenames
let mut files: Vec<BufReader<Box<dyn Read>>> = filenames
.into_iter()
.map(|name| {
BufReader::new(if name == "-" {
Box::new(stdin()) as Box<Read>
Box::new(stdin()) as Box<dyn Read>
} else {
let r = crash_if_err!(1, File::open(Path::new(&name)));
Box::new(r) as Box<Read>
Box::new(r) as Box<dyn Read>
})
})
.collect();

View file

@ -14,7 +14,7 @@ use tokenize::unescaped_text::UnescapedText;
use tokenize::sub::Sub;
pub struct Memo {
tokens: Vec<Box<Token>>,
tokens: Vec<Box<dyn Token>>,
}
fn warn_excess_args(first_arg: &str) {
@ -27,7 +27,7 @@ fn warn_excess_args(first_arg: &str) {
impl Memo {
pub fn new(pf_string: &String, pf_args_it: &mut Peekable<Iter<String>>) -> Memo {
let mut pm = Memo { tokens: Vec::new() };
let mut tmp_token: Option<Box<Token>>;
let mut tmp_token: Option<Box<dyn Token>>;
let mut it = put_back_n(pf_string.chars());
let mut has_sub = false;
loop {

View file

@ -240,7 +240,7 @@ pub fn base_conv_float(src: &Vec<u8>, radix_src: u8, radix_dest: u8) -> f64 {
r
}
pub fn str_to_arrnum(src: &str, radix_def_src: &RadixDef) -> Vec<u8> {
pub fn str_to_arrnum(src: &str, radix_def_src: &dyn RadixDef) -> Vec<u8> {
let mut intermed_in: Vec<u8> = Vec::new();
for c in src.chars() {
match radix_def_src.from_char(c) {
@ -253,7 +253,7 @@ pub fn str_to_arrnum(src: &str, radix_def_src: &RadixDef) -> Vec<u8> {
intermed_in
}
pub fn arrnum_to_str(src: &Vec<u8>, radix_def_dest: &RadixDef) -> String {
pub fn arrnum_to_str(src: &Vec<u8>, radix_def_dest: &dyn RadixDef) -> String {
let mut str_out = String::new();
for u in src.iter() {
match radix_def_dest.from_u8(u.clone()) {
@ -267,7 +267,11 @@ pub fn arrnum_to_str(src: &Vec<u8>, radix_def_dest: &RadixDef) -> String {
}
#[allow(unused_variables)]
pub fn base_conv_str(src: &str, radix_def_src: &RadixDef, radix_def_dest: &RadixDef) -> String {
pub fn base_conv_str(
src: &str,
radix_def_src: &dyn RadixDef,
radix_def_dest: &dyn RadixDef,
) -> String {
let intermed_in: Vec<u8> = str_to_arrnum(src, radix_def_src);
let intermed_out = base_conv_vec(
&intermed_in,

View file

@ -210,7 +210,7 @@ pub fn num_format(field: &FormatField, in_str_opt: Option<&String>) -> Option<St
// see formatter.rs for more details
// to do switch to static dispatch
let fmtr: Box<Formatter> = match *field.field_type {
let fmtr: Box<dyn Formatter> = match *field.field_type {
FieldType::Intf => Box::new(Intf::new()),
FieldType::Floatf => Box::new(Floatf::new()),
FieldType::CninetyNineHexFloatf => Box::new(CninetyNineHexFloatf::new()),

View file

@ -116,20 +116,20 @@ impl SubParser {
fn from_it(
it: &mut PutBackN<Chars>,
args: &mut Peekable<Iter<String>>,
) -> Option<Box<token::Token>> {
) -> Option<Box<dyn token::Token>> {
let mut parser = SubParser::new();
if parser.sub_vals_retrieved(it) {
let t: Box<token::Token> = SubParser::build_token(parser);
let t: Box<dyn token::Token> = SubParser::build_token(parser);
t.print(args);
Some(t)
} else {
None
}
}
fn build_token(parser: SubParser) -> Box<token::Token> {
fn build_token(parser: SubParser) -> Box<dyn token::Token> {
// not a self method so as to allow move of subparser vals.
// return new Sub struct as token
let t: Box<token::Token> = Box::new(Sub::new(
let t: Box<dyn token::Token> = Box::new(Sub::new(
if parser.min_width_is_asterisk {
CanAsterisk::Asterisk
} else {
@ -317,7 +317,7 @@ impl token::Tokenizer for Sub {
fn from_it(
it: &mut PutBackN<Chars>,
args: &mut Peekable<Iter<String>>,
) -> Option<Box<token::Token>> {
) -> Option<Box<dyn token::Token>> {
SubParser::from_it(it, args)
}
}

View file

@ -1,10 +1,9 @@
//! Traits and enums dealing with Tokenization of printf Format String
#[allow(unused_must_use)]
use std::iter::Peekable;
use std::str::Chars;
use std::slice::Iter;
use itertools::PutBackN;
#[allow(unused_must_use)]
use std::iter::Peekable;
use std::slice::Iter;
use std::str::Chars;
// A token object is an object that can print the expected output
// of a contiguous segment of the format string, and
@ -26,5 +25,8 @@ pub trait Token {
// a number of arguments equal to the number of argument-using tokens
pub trait Tokenizer {
fn from_it(it: &mut PutBackN<Chars>, args: &mut Peekable<Iter<String>>) -> Option<Box<Token>>;
fn from_it(
it: &mut PutBackN<Chars>,
args: &mut Peekable<Iter<String>>,
) -> Option<Box<dyn Token>>;
}

View file

@ -3,14 +3,14 @@
//! and escaped character literals (of allowed escapes),
//! into an unescaped text byte array
use std::iter::Peekable;
use std::slice::Iter;
use std::str::Chars;
use std::char::from_u32;
use std::process::exit;
use super::token;
use cli;
use itertools::PutBackN;
use super::token;
use std::char::from_u32;
use std::iter::Peekable;
use std::process::exit;
use std::slice::Iter;
use std::str::Chars;
pub struct UnescapedText(Vec<u8>);
impl UnescapedText {
@ -173,7 +173,10 @@ impl UnescapedText {
// and return a wrapper around a Vec<u8> of unescaped bytes
// break on encounter of sub symbol ('%[^%]') unless called
// through %b subst.
pub fn from_it_core(it: &mut PutBackN<Chars>, subs_mode: bool) -> Option<Box<token::Token>> {
pub fn from_it_core(
it: &mut PutBackN<Chars>,
subs_mode: bool,
) -> Option<Box<dyn token::Token>> {
let mut addchar = false;
let mut new_text = UnescapedText::new();
let mut tmp_str = String::new();
@ -241,7 +244,7 @@ impl token::Tokenizer for UnescapedText {
fn from_it(
it: &mut PutBackN<Chars>,
args: &mut Peekable<Iter<String>>,
) -> Option<Box<token::Token>> {
) -> Option<Box<dyn token::Token>> {
UnescapedText::from_it_core(it, false)
}
}

View file

@ -200,7 +200,7 @@ fn read_input(input_files: &[String], config: &Config) -> HashMap<String, (Vec<S
}
let mut lines_so_far: usize = 0;
for filename in files {
let reader: BufReader<Box<Read>> = BufReader::new(if filename == "-" {
let reader: BufReader<Box<dyn Read>> = BufReader::new(if filename == "-" {
Box::new(stdin())
} else {
let file = crash_if_err!(1, File::open(filename));
@ -470,7 +470,7 @@ fn write_traditional_output(
words: &BTreeSet<WordRef>,
output_filename: &str,
) {
let mut writer: BufWriter<Box<Write>> = BufWriter::new(if output_filename == "-" {
let mut writer: BufWriter<Box<dyn Write>> = BufWriter::new(if output_filename == "-" {
Box::new(stdout())
} else {
let file = crash_if_err!(1, File::create(output_filename));

View file

@ -147,10 +147,10 @@ With no FILE, or when FILE is -, read standard input.",
fn read_input_file(filename: &str) -> Vec<u8> {
let mut file = BufReader::new(if filename == "-" {
Box::new(stdin()) as Box<Read>
Box::new(stdin()) as Box<dyn Read>
} else {
match File::open(filename) {
Ok(f) => Box::new(f) as Box<Read>,
Ok(f) => Box::new(f) as Box<dyn Read>,
Err(e) => crash!(1, "failed to open '{}': {}", filename, e),
}
});
@ -206,9 +206,9 @@ fn shuf_bytes(
random: Option<String>,
) {
let mut output = BufWriter::new(match output {
None => Box::new(stdout()) as Box<Write>,
None => Box::new(stdout()) as Box<dyn Write>,
Some(s) => match File::create(&s[..]) {
Ok(f) => Box::new(f) as Box<Write>,
Ok(f) => Box::new(f) as Box<dyn Write>,
Err(e) => crash!(1, "failed to open '{}' for writing: {}", &s[..], e),
},
});

View file

@ -69,7 +69,7 @@ impl Default for Settings {
}
struct MergeableFile<'a> {
lines: Lines<BufReader<Box<Read>>>,
lines: Lines<BufReader<Box<dyn Read>>>,
current_line: String,
settings: &'a Settings,
}
@ -109,7 +109,7 @@ impl<'a> FileMerger<'a> {
settings: settings,
}
}
fn push_file(&mut self, mut lines: Lines<BufReader<Box<Read>>>) {
fn push_file(&mut self, mut lines: Lines<BufReader<Box<dyn Read>>>) {
match lines.next() {
Some(Ok(next_line)) => {
let mergeable_file = MergeableFile {
@ -313,7 +313,7 @@ fn exec(files: Vec<String>, settings: &Settings) -> i32 {
0
}
fn exec_check_file(lines: Lines<BufReader<Box<Read>>>, settings: &Settings) -> i32 {
fn exec_check_file(lines: Lines<BufReader<Box<dyn Read>>>, settings: &Settings) -> i32 {
// errors yields the line before each disorder,
// plus the last line (quirk of .coalesce())
let unwrapped_lines = lines.filter_map(|maybe_line| {
@ -507,15 +507,15 @@ fn print_sorted<S, T: Iterator<Item = S>>(iter: T, outfile: &Option<String>)
where
S: std::fmt::Display,
{
let mut file: Box<Write> = match *outfile {
let mut file: Box<dyn Write> = match *outfile {
Some(ref filename) => match File::create(Path::new(&filename)) {
Ok(f) => Box::new(BufWriter::new(f)) as Box<Write>,
Ok(f) => Box::new(BufWriter::new(f)) as Box<dyn Write>,
Err(e) => {
show_error!("sort: {0}: {1}", filename, e.to_string());
panic!("Could not open output file");
}
},
None => Box::new(stdout()) as Box<Write>,
None => Box::new(stdout()) as Box<dyn Write>,
};
for line in iter {
@ -531,14 +531,14 @@ where
}
// from cat.rs
fn open(path: &str) -> Option<(Box<Read>, bool)> {
fn open(path: &str) -> Option<(Box<dyn Read>, bool)> {
if path == "-" {
let stdin = stdin();
return Some((Box::new(stdin) as Box<Read>, is_stdin_interactive()));
return Some((Box::new(stdin) as Box<dyn Read>, is_stdin_interactive()));
}
match File::open(Path::new(path)) {
Ok(f) => Some((Box::new(f) as Box<Read>, false)),
Ok(f) => Some((Box::new(f) as Box<dyn Read>, false)),
Err(e) => {
show_error!("sort: {0}: {1}", path, e.to_string());
None

View file

@ -159,7 +159,7 @@ struct LineSplitter {
}
impl LineSplitter {
fn new(settings: &Settings) -> Box<Splitter> {
fn new(settings: &Settings) -> Box<dyn Splitter> {
let n = match settings.strategy_param.parse() {
Ok(a) => a,
Err(e) => crash!(1, "invalid number of lines: {}", e),
@ -167,7 +167,7 @@ impl LineSplitter {
Box::new(LineSplitter {
saved_lines_to_write: n,
lines_to_write: n,
}) as Box<Splitter>
}) as Box<dyn Splitter>
}
}
@ -190,7 +190,7 @@ struct ByteSplitter {
}
impl ByteSplitter {
fn new(settings: &Settings) -> Box<Splitter> {
fn new(settings: &Settings) -> Box<dyn Splitter> {
let mut strategy_param: Vec<char> = settings.strategy_param.chars().collect();
let suffix = strategy_param.pop().unwrap();
let multiplier = match suffix {
@ -221,7 +221,7 @@ impl ByteSplitter {
bytes_to_write: n * multiplier,
break_on_line_end: settings.strategy == "b",
require_whole_line: false,
}) as Box<Splitter>
}) as Box<dyn Splitter>
}
}
@ -279,7 +279,7 @@ fn num_prefix(i: usize, width: usize) -> String {
fn split(settings: &Settings) -> i32 {
let mut reader = BufReader::new(if settings.input == "-" {
Box::new(stdin()) as Box<Read>
Box::new(stdin()) as Box<dyn Read>
} else {
let r = match File::open(Path::new(&settings.input)) {
Ok(a) => a,
@ -289,10 +289,10 @@ fn split(settings: &Settings) -> i32 {
settings.input
),
};
Box::new(r) as Box<Read>
Box::new(r) as Box<dyn Read>
});
let mut splitter: Box<Splitter> = match settings.strategy.as_ref() {
let mut splitter: Box<dyn Splitter> = match settings.strategy.as_ref() {
"l" => LineSplitter::new(settings),
"b" | "C" => ByteSplitter::new(settings),
a => crash!(1, "strategy {} not supported", a),
@ -303,7 +303,7 @@ fn split(settings: &Settings) -> i32 {
request_new_file: true, // Request new file
};
let mut writer = BufWriter::new(Box::new(stdout()) as Box<Write>);
let mut writer = BufWriter::new(Box::new(stdout()) as Box<dyn Write>);
let mut fileno = 0;
loop {
if control.current_line.chars().count() == 0 {
@ -333,7 +333,7 @@ fn split(settings: &Settings) -> i32 {
.create(true)
.open(Path::new(&filename))
.unwrap(),
) as Box<Write>);
) as Box<dyn Write>);
control.request_new_file = false;
if settings.verbose {
println!("creating file '{}'", filename);

View file

@ -21,7 +21,7 @@ use std::path::Path;
static NAME: &str = "sum";
static VERSION: &str = env!("CARGO_PKG_VERSION");
fn bsd_sum(mut reader: Box<Read>) -> (usize, u16) {
fn bsd_sum(mut reader: Box<dyn Read>) -> (usize, u16) {
let mut buf = [0; 1024];
let mut blocks_read = 0;
let mut checksum: u16 = 0;
@ -41,7 +41,7 @@ fn bsd_sum(mut reader: Box<Read>) -> (usize, u16) {
(blocks_read, checksum)
}
fn sysv_sum(mut reader: Box<Read>) -> (usize, u16) {
fn sysv_sum(mut reader: Box<dyn Read>) -> (usize, u16) {
let mut buf = [0; 512];
let mut blocks_read = 0;
let mut ret = 0u32;
@ -64,12 +64,12 @@ fn sysv_sum(mut reader: Box<Read>) -> (usize, u16) {
(blocks_read, ret as u16)
}
fn open(name: &str) -> Result<Box<Read>> {
fn open(name: &str) -> Result<Box<dyn Read>> {
match name {
"-" => Ok(Box::new(stdin()) as Box<Read>),
"-" => Ok(Box::new(stdin()) as Box<dyn Read>),
_ => {
let f = File::open(&Path::new(name))?;
Ok(Box::new(f) as Box<Read>)
Ok(Box::new(f) as Box<dyn Read>)
}
}
}

View file

@ -91,10 +91,10 @@ fn tac(filenames: Vec<String>, before: bool, _: bool, separator: &str) {
for filename in &filenames {
let mut file = BufReader::new(if filename == "-" {
Box::new(stdin()) as Box<Read>
Box::new(stdin()) as Box<dyn Read>
} else {
match File::open(filename) {
Ok(f) => Box::new(f) as Box<Read>,
Ok(f) => Box::new(f) as Box<dyn Read>,
Err(e) => {
show_warning!("failed to open '{}' for reading: {}", filename, e);
continue;

View file

@ -86,7 +86,7 @@ fn exec(options: Options) -> Result<()> {
}
fn tee(options: Options) -> Result<()> {
let mut writers: Vec<Box<Write>> = options
let mut writers: Vec<Box<dyn Write>> = options
.files
.clone()
.into_iter()
@ -95,7 +95,7 @@ fn tee(options: Options) -> Result<()> {
writers.push(Box::new(stdout()));
let output = &mut MultiWriter { writers };
let input = &mut NamedReader {
inner: Box::new(stdin()) as Box<Read>,
inner: Box::new(stdin()) as Box<dyn Read>,
};
if copy(input, output).is_err() || output.flush().is_err() {
Err(Error::new(ErrorKind::Other, ""))
@ -104,9 +104,9 @@ fn tee(options: Options) -> Result<()> {
}
}
fn open(name: String, append: bool) -> Box<Write> {
fn open(name: String, append: bool) -> Box<dyn Write> {
let path = PathBuf::from(name);
let inner: Box<Write> = {
let inner: Box<dyn Write> = {
let mut options = OpenOptions::new();
let mode = if append {
options.append(true)
@ -121,11 +121,11 @@ fn open(name: String, append: bool) -> Box<Write> {
Box::new(NamedWriter {
inner: inner,
path: path,
}) as Box<Write>
}) as Box<dyn Write>
}
struct MultiWriter {
writers: Vec<Box<Write>>,
writers: Vec<Box<dyn Write>>,
}
impl Write for MultiWriter {
@ -145,7 +145,7 @@ impl Write for MultiWriter {
}
struct NamedWriter {
inner: Box<Write>,
inner: Box<dyn Write>,
path: PathBuf,
}
@ -153,7 +153,7 @@ impl Write for NamedWriter {
fn write(&mut self, buf: &[u8]) -> Result<usize> {
match self.inner.write(buf) {
Err(f) => {
self.inner = Box::new(sink()) as Box<Write>;
self.inner = Box::new(sink()) as Box<dyn Write>;
warn(format!("{}: {}", self.path.display(), f.to_string()).as_ref());
Err(f)
}
@ -164,7 +164,7 @@ impl Write for NamedWriter {
fn flush(&mut self) -> Result<()> {
match self.inner.flush() {
Err(f) => {
self.inner = Box::new(sink()) as Box<Write>;
self.inner = Box::new(sink()) as Box<dyn Write>;
warn(format!("{}: {}", self.path.display(), f.to_string()).as_ref());
Err(f)
}
@ -174,7 +174,7 @@ impl Write for NamedWriter {
}
struct NamedReader {
inner: Box<Read>,
inner: Box<dyn Read>,
}
impl Read for NamedReader {

View file

@ -20,9 +20,9 @@ extern crate getopts;
extern crate uucore;
use bit_set::BitSet;
use fnv::FnvHashMap;
use getopts::Options;
use std::io::{stdin, stdout, BufRead, BufWriter, Write};
use fnv::FnvHashMap;
use expand::ExpandSet;
@ -145,7 +145,11 @@ impl SymbolTranslator for TranslateOperation {
}
}
fn translate_input<T: SymbolTranslator>(input: &mut BufRead, output: &mut Write, translator: T) {
fn translate_input<T: SymbolTranslator>(
input: &mut dyn BufRead,
output: &mut dyn Write,
translator: T,
) {
let mut buf = String::with_capacity(BUFFER_LEN + 4);
let mut output_buf = String::with_capacity(BUFFER_LEN + 4);

View file

@ -62,7 +62,7 @@ pub fn uumain(args: Vec<String>) -> i32 {
let mut file_buf;
let mut reader = BufReader::new(if input == "-" {
stdin_buf = stdin();
&mut stdin_buf as &mut Read
&mut stdin_buf as &mut dyn Read
} else {
file_buf = match File::open(Path::new(&input)) {
Ok(a) => a,
@ -71,7 +71,7 @@ pub fn uumain(args: Vec<String>) -> i32 {
return 1;
}
};
&mut file_buf as &mut Read
&mut file_buf as &mut dyn Read
});
let mut g = Graph::new();

View file

@ -145,16 +145,16 @@ pub fn uumain(args: Vec<String>) -> i32 {
0
}
fn open(path: String) -> BufReader<Box<Read + 'static>> {
fn open(path: String) -> BufReader<Box<dyn Read + 'static>> {
let file_buf;
if path == "-" {
BufReader::new(Box::new(stdin()) as Box<Read>)
BufReader::new(Box::new(stdin()) as Box<dyn Read>)
} else {
file_buf = match File::open(&path[..]) {
Ok(a) => a,
Err(e) => crash!(1, "{}: {}", &path[..], e),
};
BufReader::new(Box::new(file_buf) as Box<Read>)
BufReader::new(Box::new(file_buf) as Box<dyn Read>)
}
}

View file

@ -111,7 +111,7 @@ impl Uniq {
fn cmp_key<F>(&self, line: &str, mut closure: F) -> bool
where
F: FnMut(&mut Iterator<Item = char>) -> bool,
F: FnMut(&mut dyn Iterator<Item = char>) -> bool,
{
let fields_to_check = self.skip_fields(line);
let len = fields_to_check.len();
@ -307,26 +307,26 @@ pub fn uumain(args: Vec<String>) -> i32 {
0
}
fn open_input_file(in_file_name: String) -> BufReader<Box<Read + 'static>> {
fn open_input_file(in_file_name: String) -> BufReader<Box<dyn Read + 'static>> {
let in_file = if in_file_name == "-" {
Box::new(stdin()) as Box<Read>
Box::new(stdin()) as Box<dyn Read>
} else {
let path = Path::new(&in_file_name[..]);
let in_file = File::open(&path);
let r = crash_if_err!(1, in_file);
Box::new(r) as Box<Read>
Box::new(r) as Box<dyn Read>
};
BufReader::new(in_file)
}
fn open_output_file(out_file_name: String) -> BufWriter<Box<Write + 'static>> {
fn open_output_file(out_file_name: String) -> BufWriter<Box<dyn Write + 'static>> {
let out_file = if out_file_name == "-" {
Box::new(stdout()) as Box<Write>
Box::new(stdout()) as Box<dyn Write>
} else {
let path = Path::new(&out_file_name[..]);
let in_file = File::create(&path);
let w = crash_if_err!(1, in_file);
Box::new(w) as Box<Write>
Box::new(w) as Box<dyn Write>
};
BufWriter::new(out_file)
}

View file

@ -258,9 +258,9 @@ fn print_stats(settings: &Settings, result: &Result, max_width: usize) {
}
}
fn open(path: &str) -> StdResult<BufReader<Box<Read + 'static>>, i32> {
fn open(path: &str) -> StdResult<BufReader<Box<dyn Read + 'static>>, i32> {
if "-" == path {
let reader = Box::new(stdin()) as Box<Read>;
let reader = Box::new(stdin()) as Box<dyn Read>;
return Ok(BufReader::new(reader));
}
@ -270,7 +270,7 @@ fn open(path: &str) -> StdResult<BufReader<Box<Read + 'static>>, i32> {
}
match File::open(&fpath) {
Ok(fd) => {
let reader = Box::new(fd) as Box<Read>;
let reader = Box::new(fd) as Box<dyn Read>;
Ok(BufReader::new(reader))
}
Err(e) => {