mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 03:27:44 +00:00
Add the 'dyn' keyword where necessary
The keyword is needed for trait objects and is intended to improve readability. https://doc.rust-lang.org/edition-guide/rust-2018/trait-system/dyn-trait-for-trait-objects.html
This commit is contained in:
parent
17035666b8
commit
3511aa987a
31 changed files with 148 additions and 135 deletions
|
@ -100,7 +100,7 @@ struct OutputOptions {
|
||||||
|
|
||||||
/// Represents an open file handle, stream, or other device
|
/// Represents an open file handle, stream, or other device
|
||||||
struct InputHandle {
|
struct InputHandle {
|
||||||
reader: Box<Read>,
|
reader: Box<dyn Read>,
|
||||||
is_interactive: bool,
|
is_interactive: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -241,7 +241,7 @@ fn open(path: &str) -> CatResult<InputHandle> {
|
||||||
if path == "-" {
|
if path == "-" {
|
||||||
let stdin = stdin();
|
let stdin = stdin();
|
||||||
return Ok(InputHandle {
|
return Ok(InputHandle {
|
||||||
reader: Box::new(stdin) as Box<Read>,
|
reader: Box::new(stdin) as Box<dyn Read>,
|
||||||
is_interactive: is_stdin_interactive(),
|
is_interactive: is_stdin_interactive(),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -253,14 +253,14 @@ fn open(path: &str) -> CatResult<InputHandle> {
|
||||||
let socket = UnixStream::connect(path).context(path)?;
|
let socket = UnixStream::connect(path).context(path)?;
|
||||||
socket.shutdown(Shutdown::Write).context(path)?;
|
socket.shutdown(Shutdown::Write).context(path)?;
|
||||||
Ok(InputHandle {
|
Ok(InputHandle {
|
||||||
reader: Box::new(socket) as Box<Read>,
|
reader: Box::new(socket) as Box<dyn Read>,
|
||||||
is_interactive: false,
|
is_interactive: false,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
let file = File::open(path).context(path)?;
|
let file = File::open(path).context(path)?;
|
||||||
Ok(InputHandle {
|
Ok(InputHandle {
|
||||||
reader: Box::new(file) as Box<Read>,
|
reader: Box::new(file) as Box<dyn Read>,
|
||||||
is_interactive: false,
|
is_interactive: false,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -55,7 +55,7 @@ fn cksum(fname: &str) -> io::Result<(u32, usize)> {
|
||||||
let mut size = 0usize;
|
let mut size = 0usize;
|
||||||
|
|
||||||
let file;
|
let file;
|
||||||
let mut rd: Box<Read> = match fname {
|
let mut rd: Box<dyn Read> = match fname {
|
||||||
"-" => Box::new(stdin()),
|
"-" => Box::new(stdin()),
|
||||||
_ => {
|
_ => {
|
||||||
file = File::open(&Path::new(fname))?;
|
file = File::open(&Path::new(fname))?;
|
||||||
|
|
|
@ -137,7 +137,7 @@ pub fn uumain(args: Vec<String>) -> i32 {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Iterate over all dates - whether it's a single date or a file.
|
// 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) => {
|
DateSource::Custom(ref input) => {
|
||||||
let date = parse_date(input.clone());
|
let date = parse_date(input.clone());
|
||||||
let iter = std::iter::once(date);
|
let iter = std::iter::once(date);
|
||||||
|
|
|
@ -142,7 +142,7 @@ fn du(
|
||||||
options: &Options,
|
options: &Options,
|
||||||
depth: usize,
|
depth: usize,
|
||||||
inodes: &mut HashSet<u64>,
|
inodes: &mut HashSet<u64>,
|
||||||
) -> Box<DoubleEndedIterator<Item = Stat>> {
|
) -> Box<dyn DoubleEndedIterator<Item = Stat>> {
|
||||||
let mut stats = vec![];
|
let mut stats = vec![];
|
||||||
let mut futures = vec![];
|
let mut futures = vec![];
|
||||||
|
|
||||||
|
|
|
@ -128,16 +128,16 @@ pub fn uumain(args: Vec<String>) -> i32 {
|
||||||
0
|
0
|
||||||
}
|
}
|
||||||
|
|
||||||
fn open(path: String) -> BufReader<Box<Read + 'static>> {
|
fn open(path: String) -> BufReader<Box<dyn Read + 'static>> {
|
||||||
let file_buf;
|
let file_buf;
|
||||||
if path == "-" {
|
if path == "-" {
|
||||||
BufReader::new(Box::new(stdin()) as Box<Read>)
|
BufReader::new(Box::new(stdin()) as Box<dyn Read>)
|
||||||
} else {
|
} else {
|
||||||
file_buf = match File::open(&path[..]) {
|
file_buf = match File::open(&path[..]) {
|
||||||
Ok(a) => a,
|
Ok(a) => a,
|
||||||
Err(e) => crash!(1, "{}: {}\n", &path[..], e),
|
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>)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -38,7 +38,7 @@ static SYNTAX: &str = "[OPTION]... [FILE]...";
|
||||||
static SUMMARY: &str = "Reformat paragraphs from input files (or stdin) to stdout.";
|
static SUMMARY: &str = "Reformat paragraphs from input files (or stdin) to stdout.";
|
||||||
static LONG_HELP: &str = "";
|
static LONG_HELP: &str = "";
|
||||||
|
|
||||||
pub type FileOrStdReader = BufReader<Box<Read + 'static>>;
|
pub type FileOrStdReader = BufReader<Box<dyn Read + 'static>>;
|
||||||
pub struct FmtOptions {
|
pub struct FmtOptions {
|
||||||
crown: bool,
|
crown: bool,
|
||||||
tagged: bool,
|
tagged: bool,
|
||||||
|
@ -179,9 +179,9 @@ pub fn uumain(args: Vec<String>) -> i32 {
|
||||||
|
|
||||||
for i in files.iter().map(|x| &x[..]) {
|
for i in files.iter().map(|x| &x[..]) {
|
||||||
let mut fp = match i {
|
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) {
|
_ => 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) => {
|
Err(e) => {
|
||||||
show_warning!("{}: {}", i, e);
|
show_warning!("{}: {}", i, e);
|
||||||
continue;
|
continue;
|
||||||
|
|
|
@ -89,10 +89,10 @@ fn fold(filenames: Vec<String>, bytes: bool, spaces: bool, width: usize) {
|
||||||
let mut file_buf;
|
let mut file_buf;
|
||||||
let buffer = BufReader::new(if filename == "-" {
|
let buffer = BufReader::new(if filename == "-" {
|
||||||
stdin_buf = stdin();
|
stdin_buf = stdin();
|
||||||
&mut stdin_buf as &mut Read
|
&mut stdin_buf as &mut dyn Read
|
||||||
} else {
|
} else {
|
||||||
file_buf = safe_unwrap!(File::open(Path::new(filename)));
|
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);
|
fold_file(buffer, bytes, spaces, width);
|
||||||
}
|
}
|
||||||
|
|
|
@ -51,23 +51,23 @@ fn is_custom_binary(program: &str) -> bool {
|
||||||
fn detect_algo(
|
fn detect_algo(
|
||||||
program: &str,
|
program: &str,
|
||||||
matches: &getopts::Matches,
|
matches: &getopts::Matches,
|
||||||
) -> (&'static str, Box<Digest + 'static>, usize) {
|
) -> (&'static str, Box<dyn Digest + 'static>, usize) {
|
||||||
let mut alg: Option<Box<Digest>> = None;
|
let mut alg: Option<Box<dyn Digest>> = None;
|
||||||
let mut name: &'static str = "";
|
let mut name: &'static str = "";
|
||||||
let mut output_bits = 0;
|
let mut output_bits = 0;
|
||||||
match program {
|
match program {
|
||||||
"md5sum" => ("MD5", Box::new(Md5::new()) as Box<Digest>, 128),
|
"md5sum" => ("MD5", Box::new(Md5::new()) as Box<dyn Digest>, 128),
|
||||||
"sha1sum" => ("SHA1", Box::new(Sha1::new()) as Box<Digest>, 160),
|
"sha1sum" => ("SHA1", Box::new(Sha1::new()) as Box<dyn Digest>, 160),
|
||||||
"sha224sum" => ("SHA224", Box::new(Sha224::new()) as Box<Digest>, 224),
|
"sha224sum" => ("SHA224", Box::new(Sha224::new()) as Box<dyn Digest>, 224),
|
||||||
"sha256sum" => ("SHA256", Box::new(Sha256::new()) as Box<Digest>, 256),
|
"sha256sum" => ("SHA256", Box::new(Sha256::new()) as Box<dyn Digest>, 256),
|
||||||
"sha384sum" => ("SHA384", Box::new(Sha384::new()) as Box<Digest>, 384),
|
"sha384sum" => ("SHA384", Box::new(Sha384::new()) as Box<dyn Digest>, 384),
|
||||||
"sha512sum" => ("SHA512", Box::new(Sha512::new()) as Box<Digest>, 512),
|
"sha512sum" => ("SHA512", Box::new(Sha512::new()) as Box<dyn Digest>, 512),
|
||||||
"sha3sum" => match matches.opt_str("bits") {
|
"sha3sum" => match matches.opt_str("bits") {
|
||||||
Some(bits_str) => match usize::from_str_radix(&bits_str, 10) {
|
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(224) => ("SHA3-224", Box::new(Sha3_224::new()) as Box<dyn Digest>, 224),
|
||||||
Ok(256) => ("SHA3-256", Box::new(Sha3_256::new()) as Box<Digest>, 256),
|
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<Digest>, 384),
|
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<Digest>, 512),
|
Ok(512) => ("SHA3-512", Box::new(Sha3_512::new()) as Box<dyn Digest>, 512),
|
||||||
Ok(_) => crash!(
|
Ok(_) => crash!(
|
||||||
1,
|
1,
|
||||||
"Invalid output size for SHA3 (expected 224, 256, 384, or 512)"
|
"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"),
|
None => crash!(1, "--bits required for SHA3"),
|
||||||
},
|
},
|
||||||
"sha3-224sum" => ("SHA3-224", Box::new(Sha3_224::new()) as Box<Digest>, 224),
|
"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<Digest>, 256),
|
"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<Digest>, 384),
|
"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<Digest>, 512),
|
"sha3-512sum" => ("SHA3-512", Box::new(Sha3_512::new()) as Box<dyn Digest>, 512),
|
||||||
"shake128sum" => match matches.opt_str("bits") {
|
"shake128sum" => match matches.opt_str("bits") {
|
||||||
Some(bits_str) => match usize::from_str_radix(&bits_str, 10) {
|
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),
|
Err(err) => crash!(1, "{}", err),
|
||||||
},
|
},
|
||||||
None => crash!(1, "--bits required for SHAKE-128"),
|
None => crash!(1, "--bits required for SHAKE-128"),
|
||||||
},
|
},
|
||||||
"shake256sum" => match matches.opt_str("bits") {
|
"shake256sum" => match matches.opt_str("bits") {
|
||||||
Some(bits_str) => match usize::from_str_radix(&bits_str, 10) {
|
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),
|
Err(err) => crash!(1, "{}", err),
|
||||||
},
|
},
|
||||||
None => crash!(1, "--bits required for SHAKE-256"),
|
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) {
|
Some(bits_str) => match usize::from_str_radix(&bits_str, 10) {
|
||||||
Ok(224) => set_or_crash(
|
Ok(224) => set_or_crash(
|
||||||
"SHA3-224",
|
"SHA3-224",
|
||||||
Box::new(Sha3_224::new()) as Box<Digest>,
|
Box::new(Sha3_224::new()) as Box<dyn Digest>,
|
||||||
224,
|
224,
|
||||||
),
|
),
|
||||||
Ok(256) => set_or_crash(
|
Ok(256) => set_or_crash(
|
||||||
"SHA3-256",
|
"SHA3-256",
|
||||||
Box::new(Sha3_256::new()) as Box<Digest>,
|
Box::new(Sha3_256::new()) as Box<dyn Digest>,
|
||||||
256,
|
256,
|
||||||
),
|
),
|
||||||
Ok(384) => set_or_crash(
|
Ok(384) => set_or_crash(
|
||||||
"SHA3-384",
|
"SHA3-384",
|
||||||
Box::new(Sha3_384::new()) as Box<Digest>,
|
Box::new(Sha3_384::new()) as Box<dyn Digest>,
|
||||||
384,
|
384,
|
||||||
),
|
),
|
||||||
Ok(512) => set_or_crash(
|
Ok(512) => set_or_crash(
|
||||||
"SHA3-512",
|
"SHA3-512",
|
||||||
Box::new(Sha3_512::new()) as Box<Digest>,
|
Box::new(Sha3_512::new()) as Box<dyn Digest>,
|
||||||
512,
|
512,
|
||||||
),
|
),
|
||||||
Ok(_) => crash!(
|
Ok(_) => crash!(
|
||||||
|
@ -369,7 +369,7 @@ Compute and check message digests.",
|
||||||
|
|
||||||
fn hashsum(
|
fn hashsum(
|
||||||
algoname: &str,
|
algoname: &str,
|
||||||
mut digest: Box<Digest>,
|
mut digest: Box<dyn Digest>,
|
||||||
files: Vec<String>,
|
files: Vec<String>,
|
||||||
binary: bool,
|
binary: bool,
|
||||||
check: bool,
|
check: bool,
|
||||||
|
@ -389,10 +389,10 @@ fn hashsum(
|
||||||
let file_buf;
|
let file_buf;
|
||||||
let mut file = BufReader::new(if filename == "-" {
|
let mut file = BufReader::new(if filename == "-" {
|
||||||
stdin_buf = stdin();
|
stdin_buf = stdin();
|
||||||
Box::new(stdin_buf) as Box<Read>
|
Box::new(stdin_buf) as Box<dyn Read>
|
||||||
} else {
|
} else {
|
||||||
file_buf = safe_unwrap!(File::open(filename));
|
file_buf = safe_unwrap!(File::open(filename));
|
||||||
Box::new(file_buf) as Box<Read>
|
Box::new(file_buf) as Box<dyn Read>
|
||||||
});
|
});
|
||||||
if check {
|
if check {
|
||||||
// Set up Regexes for line validation and parsing
|
// Set up Regexes for line validation and parsing
|
||||||
|
@ -440,7 +440,7 @@ fn hashsum(
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
let f = safe_unwrap!(File::open(ck_filename));
|
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(
|
let real_sum = safe_unwrap!(digest_reader(
|
||||||
&mut digest,
|
&mut digest,
|
||||||
&mut ckf,
|
&mut ckf,
|
||||||
|
@ -482,7 +482,7 @@ fn hashsum(
|
||||||
}
|
}
|
||||||
|
|
||||||
fn digest_reader<'a, T: Read>(
|
fn digest_reader<'a, T: Read>(
|
||||||
digest: &mut Box<Digest + 'a>,
|
digest: &mut Box<dyn Digest + 'a>,
|
||||||
reader: &mut BufReader<T>,
|
reader: &mut BufReader<T>,
|
||||||
binary: bool,
|
binary: bool,
|
||||||
output_bits: usize,
|
output_bits: usize,
|
||||||
|
|
|
@ -235,7 +235,7 @@ struct State<'a> {
|
||||||
file_name: &'a str,
|
file_name: &'a str,
|
||||||
file_num: FileNum,
|
file_num: FileNum,
|
||||||
print_unpaired: bool,
|
print_unpaired: bool,
|
||||||
lines: Lines<Box<BufRead + 'a>>,
|
lines: Lines<Box<dyn BufRead + 'a>>,
|
||||||
seq: Vec<Line>,
|
seq: Vec<Line>,
|
||||||
max_fields: Option<usize>,
|
max_fields: Option<usize>,
|
||||||
line_num: usize,
|
line_num: usize,
|
||||||
|
@ -251,10 +251,10 @@ impl<'a> State<'a> {
|
||||||
print_unpaired: FileNum,
|
print_unpaired: FileNum,
|
||||||
) -> State<'a> {
|
) -> State<'a> {
|
||||||
let f = if name == "-" {
|
let f = if name == "-" {
|
||||||
Box::new(stdin.lock()) as Box<BufRead>
|
Box::new(stdin.lock()) as Box<dyn BufRead>
|
||||||
} else {
|
} else {
|
||||||
match File::open(name) {
|
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),
|
Err(err) => crash!(1, "{}: {}", name, err),
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -8,13 +8,13 @@ pub enum InputSource<'a> {
|
||||||
FileName(&'a str),
|
FileName(&'a str),
|
||||||
Stdin,
|
Stdin,
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
Stream(Box<io::Read>),
|
Stream(Box<dyn io::Read>),
|
||||||
}
|
}
|
||||||
|
|
||||||
// MultifileReader - concatenate all our input, file or stdin.
|
// MultifileReader - concatenate all our input, file or stdin.
|
||||||
pub struct MultifileReader<'a> {
|
pub struct MultifileReader<'a> {
|
||||||
ni: Vec<InputSource<'a>>,
|
ni: Vec<InputSource<'a>>,
|
||||||
curr_file: Option<Box<io::Read>>,
|
curr_file: Option<Box<dyn io::Read>>,
|
||||||
any_err: bool,
|
any_err: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -138,7 +138,7 @@ impl OutputInfo {
|
||||||
/// This algorithm assumes the size of all types is a power of 2 (1, 2, 4, 8, 16, ...)
|
/// 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.
|
/// Increase MAX_BYTES_PER_UNIT to allow larger types.
|
||||||
fn calculate_alignment(
|
fn calculate_alignment(
|
||||||
sf: &TypeSizeInfo,
|
sf: &dyn TypeSizeInfo,
|
||||||
byte_size_block: usize,
|
byte_size_block: usize,
|
||||||
print_width_block: usize,
|
print_width_block: usize,
|
||||||
) -> [usize; MAX_BYTES_PER_UNIT] {
|
) -> [usize; MAX_BYTES_PER_UNIT] {
|
||||||
|
|
|
@ -38,7 +38,7 @@ pub enum CommandLineInputs {
|
||||||
/// Offset and label are specified in bytes.
|
/// Offset and label are specified in bytes.
|
||||||
/// '-' is used as filename if stdin is meant. This is also returned if
|
/// '-' is used as filename if stdin is meant. This is also returned if
|
||||||
/// there is no input, as stdin is the default input.
|
/// 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();
|
let mut input_strings: Vec<String> = matches.inputs();
|
||||||
|
|
||||||
if matches.opts_present(&["traditional"]) {
|
if matches.opts_present(&["traditional"]) {
|
||||||
|
|
|
@ -68,14 +68,14 @@ FILE, separated by TABs, to standard output.",
|
||||||
}
|
}
|
||||||
|
|
||||||
fn paste(filenames: Vec<String>, serial: bool, delimiters: String) {
|
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()
|
.into_iter()
|
||||||
.map(|name| {
|
.map(|name| {
|
||||||
BufReader::new(if name == "-" {
|
BufReader::new(if name == "-" {
|
||||||
Box::new(stdin()) as Box<Read>
|
Box::new(stdin()) as Box<dyn Read>
|
||||||
} else {
|
} else {
|
||||||
let r = crash_if_err!(1, File::open(Path::new(&name)));
|
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();
|
.collect();
|
||||||
|
|
|
@ -14,7 +14,7 @@ use tokenize::unescaped_text::UnescapedText;
|
||||||
use tokenize::sub::Sub;
|
use tokenize::sub::Sub;
|
||||||
|
|
||||||
pub struct Memo {
|
pub struct Memo {
|
||||||
tokens: Vec<Box<Token>>,
|
tokens: Vec<Box<dyn Token>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn warn_excess_args(first_arg: &str) {
|
fn warn_excess_args(first_arg: &str) {
|
||||||
|
@ -27,7 +27,7 @@ fn warn_excess_args(first_arg: &str) {
|
||||||
impl Memo {
|
impl Memo {
|
||||||
pub fn new(pf_string: &String, pf_args_it: &mut Peekable<Iter<String>>) -> Memo {
|
pub fn new(pf_string: &String, pf_args_it: &mut Peekable<Iter<String>>) -> Memo {
|
||||||
let mut pm = Memo { tokens: Vec::new() };
|
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 it = put_back_n(pf_string.chars());
|
||||||
let mut has_sub = false;
|
let mut has_sub = false;
|
||||||
loop {
|
loop {
|
||||||
|
|
|
@ -240,7 +240,7 @@ pub fn base_conv_float(src: &Vec<u8>, radix_src: u8, radix_dest: u8) -> f64 {
|
||||||
r
|
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();
|
let mut intermed_in: Vec<u8> = Vec::new();
|
||||||
for c in src.chars() {
|
for c in src.chars() {
|
||||||
match radix_def_src.from_char(c) {
|
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
|
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();
|
let mut str_out = String::new();
|
||||||
for u in src.iter() {
|
for u in src.iter() {
|
||||||
match radix_def_dest.from_u8(u.clone()) {
|
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)]
|
#[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_in: Vec<u8> = str_to_arrnum(src, radix_def_src);
|
||||||
let intermed_out = base_conv_vec(
|
let intermed_out = base_conv_vec(
|
||||||
&intermed_in,
|
&intermed_in,
|
||||||
|
|
|
@ -210,7 +210,7 @@ pub fn num_format(field: &FormatField, in_str_opt: Option<&String>) -> Option<St
|
||||||
// see formatter.rs for more details
|
// see formatter.rs for more details
|
||||||
|
|
||||||
// to do switch to static dispatch
|
// 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::Intf => Box::new(Intf::new()),
|
||||||
FieldType::Floatf => Box::new(Floatf::new()),
|
FieldType::Floatf => Box::new(Floatf::new()),
|
||||||
FieldType::CninetyNineHexFloatf => Box::new(CninetyNineHexFloatf::new()),
|
FieldType::CninetyNineHexFloatf => Box::new(CninetyNineHexFloatf::new()),
|
||||||
|
|
|
@ -116,20 +116,20 @@ impl SubParser {
|
||||||
fn from_it(
|
fn from_it(
|
||||||
it: &mut PutBackN<Chars>,
|
it: &mut PutBackN<Chars>,
|
||||||
args: &mut Peekable<Iter<String>>,
|
args: &mut Peekable<Iter<String>>,
|
||||||
) -> Option<Box<token::Token>> {
|
) -> Option<Box<dyn token::Token>> {
|
||||||
let mut parser = SubParser::new();
|
let mut parser = SubParser::new();
|
||||||
if parser.sub_vals_retrieved(it) {
|
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);
|
t.print(args);
|
||||||
Some(t)
|
Some(t)
|
||||||
} else {
|
} else {
|
||||||
None
|
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.
|
// not a self method so as to allow move of subparser vals.
|
||||||
// return new Sub struct as token
|
// 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 {
|
if parser.min_width_is_asterisk {
|
||||||
CanAsterisk::Asterisk
|
CanAsterisk::Asterisk
|
||||||
} else {
|
} else {
|
||||||
|
@ -317,7 +317,7 @@ impl token::Tokenizer for Sub {
|
||||||
fn from_it(
|
fn from_it(
|
||||||
it: &mut PutBackN<Chars>,
|
it: &mut PutBackN<Chars>,
|
||||||
args: &mut Peekable<Iter<String>>,
|
args: &mut Peekable<Iter<String>>,
|
||||||
) -> Option<Box<token::Token>> {
|
) -> Option<Box<dyn token::Token>> {
|
||||||
SubParser::from_it(it, args)
|
SubParser::from_it(it, args)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,10 +1,9 @@
|
||||||
//! Traits and enums dealing with Tokenization of printf Format String
|
//! 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;
|
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
|
// A token object is an object that can print the expected output
|
||||||
// of a contiguous segment of the format string, and
|
// 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
|
// a number of arguments equal to the number of argument-using tokens
|
||||||
|
|
||||||
pub trait Tokenizer {
|
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>>;
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,14 +3,14 @@
|
||||||
//! and escaped character literals (of allowed escapes),
|
//! and escaped character literals (of allowed escapes),
|
||||||
//! into an unescaped text byte array
|
//! into an unescaped text byte array
|
||||||
|
|
||||||
use std::iter::Peekable;
|
use super::token;
|
||||||
use std::slice::Iter;
|
|
||||||
use std::str::Chars;
|
|
||||||
use std::char::from_u32;
|
|
||||||
use std::process::exit;
|
|
||||||
use cli;
|
use cli;
|
||||||
use itertools::PutBackN;
|
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>);
|
pub struct UnescapedText(Vec<u8>);
|
||||||
impl UnescapedText {
|
impl UnescapedText {
|
||||||
|
@ -173,7 +173,10 @@ impl UnescapedText {
|
||||||
// and return a wrapper around a Vec<u8> of unescaped bytes
|
// and return a wrapper around a Vec<u8> of unescaped bytes
|
||||||
// break on encounter of sub symbol ('%[^%]') unless called
|
// break on encounter of sub symbol ('%[^%]') unless called
|
||||||
// through %b subst.
|
// 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 addchar = false;
|
||||||
let mut new_text = UnescapedText::new();
|
let mut new_text = UnescapedText::new();
|
||||||
let mut tmp_str = String::new();
|
let mut tmp_str = String::new();
|
||||||
|
@ -241,7 +244,7 @@ impl token::Tokenizer for UnescapedText {
|
||||||
fn from_it(
|
fn from_it(
|
||||||
it: &mut PutBackN<Chars>,
|
it: &mut PutBackN<Chars>,
|
||||||
args: &mut Peekable<Iter<String>>,
|
args: &mut Peekable<Iter<String>>,
|
||||||
) -> Option<Box<token::Token>> {
|
) -> Option<Box<dyn token::Token>> {
|
||||||
UnescapedText::from_it_core(it, false)
|
UnescapedText::from_it_core(it, false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -200,7 +200,7 @@ fn read_input(input_files: &[String], config: &Config) -> HashMap<String, (Vec<S
|
||||||
}
|
}
|
||||||
let mut lines_so_far: usize = 0;
|
let mut lines_so_far: usize = 0;
|
||||||
for filename in files {
|
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())
|
Box::new(stdin())
|
||||||
} else {
|
} else {
|
||||||
let file = crash_if_err!(1, File::open(filename));
|
let file = crash_if_err!(1, File::open(filename));
|
||||||
|
@ -470,7 +470,7 @@ fn write_traditional_output(
|
||||||
words: &BTreeSet<WordRef>,
|
words: &BTreeSet<WordRef>,
|
||||||
output_filename: &str,
|
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())
|
Box::new(stdout())
|
||||||
} else {
|
} else {
|
||||||
let file = crash_if_err!(1, File::create(output_filename));
|
let file = crash_if_err!(1, File::create(output_filename));
|
||||||
|
|
|
@ -147,10 +147,10 @@ With no FILE, or when FILE is -, read standard input.",
|
||||||
|
|
||||||
fn read_input_file(filename: &str) -> Vec<u8> {
|
fn read_input_file(filename: &str) -> Vec<u8> {
|
||||||
let mut file = BufReader::new(if filename == "-" {
|
let mut file = BufReader::new(if filename == "-" {
|
||||||
Box::new(stdin()) as Box<Read>
|
Box::new(stdin()) as Box<dyn Read>
|
||||||
} else {
|
} else {
|
||||||
match File::open(filename) {
|
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),
|
Err(e) => crash!(1, "failed to open '{}': {}", filename, e),
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -206,9 +206,9 @@ fn shuf_bytes(
|
||||||
random: Option<String>,
|
random: Option<String>,
|
||||||
) {
|
) {
|
||||||
let mut output = BufWriter::new(match output {
|
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[..]) {
|
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),
|
Err(e) => crash!(1, "failed to open '{}' for writing: {}", &s[..], e),
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
|
@ -69,7 +69,7 @@ impl Default for Settings {
|
||||||
}
|
}
|
||||||
|
|
||||||
struct MergeableFile<'a> {
|
struct MergeableFile<'a> {
|
||||||
lines: Lines<BufReader<Box<Read>>>,
|
lines: Lines<BufReader<Box<dyn Read>>>,
|
||||||
current_line: String,
|
current_line: String,
|
||||||
settings: &'a Settings,
|
settings: &'a Settings,
|
||||||
}
|
}
|
||||||
|
@ -109,7 +109,7 @@ impl<'a> FileMerger<'a> {
|
||||||
settings: settings,
|
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() {
|
match lines.next() {
|
||||||
Some(Ok(next_line)) => {
|
Some(Ok(next_line)) => {
|
||||||
let mergeable_file = MergeableFile {
|
let mergeable_file = MergeableFile {
|
||||||
|
@ -313,7 +313,7 @@ fn exec(files: Vec<String>, settings: &Settings) -> i32 {
|
||||||
0
|
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,
|
// errors yields the line before each disorder,
|
||||||
// plus the last line (quirk of .coalesce())
|
// plus the last line (quirk of .coalesce())
|
||||||
let unwrapped_lines = lines.filter_map(|maybe_line| {
|
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
|
where
|
||||||
S: std::fmt::Display,
|
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)) {
|
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) => {
|
Err(e) => {
|
||||||
show_error!("sort: {0}: {1}", filename, e.to_string());
|
show_error!("sort: {0}: {1}", filename, e.to_string());
|
||||||
panic!("Could not open output file");
|
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 {
|
for line in iter {
|
||||||
|
@ -531,14 +531,14 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
// from cat.rs
|
// from cat.rs
|
||||||
fn open(path: &str) -> Option<(Box<Read>, bool)> {
|
fn open(path: &str) -> Option<(Box<dyn Read>, bool)> {
|
||||||
if path == "-" {
|
if path == "-" {
|
||||||
let stdin = stdin();
|
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)) {
|
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) => {
|
Err(e) => {
|
||||||
show_error!("sort: {0}: {1}", path, e.to_string());
|
show_error!("sort: {0}: {1}", path, e.to_string());
|
||||||
None
|
None
|
||||||
|
|
|
@ -159,7 +159,7 @@ struct LineSplitter {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl LineSplitter {
|
impl LineSplitter {
|
||||||
fn new(settings: &Settings) -> Box<Splitter> {
|
fn new(settings: &Settings) -> Box<dyn Splitter> {
|
||||||
let n = match settings.strategy_param.parse() {
|
let n = match settings.strategy_param.parse() {
|
||||||
Ok(a) => a,
|
Ok(a) => a,
|
||||||
Err(e) => crash!(1, "invalid number of lines: {}", e),
|
Err(e) => crash!(1, "invalid number of lines: {}", e),
|
||||||
|
@ -167,7 +167,7 @@ impl LineSplitter {
|
||||||
Box::new(LineSplitter {
|
Box::new(LineSplitter {
|
||||||
saved_lines_to_write: n,
|
saved_lines_to_write: n,
|
||||||
lines_to_write: n,
|
lines_to_write: n,
|
||||||
}) as Box<Splitter>
|
}) as Box<dyn Splitter>
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -190,7 +190,7 @@ struct ByteSplitter {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl 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 mut strategy_param: Vec<char> = settings.strategy_param.chars().collect();
|
||||||
let suffix = strategy_param.pop().unwrap();
|
let suffix = strategy_param.pop().unwrap();
|
||||||
let multiplier = match suffix {
|
let multiplier = match suffix {
|
||||||
|
@ -221,7 +221,7 @@ impl ByteSplitter {
|
||||||
bytes_to_write: n * multiplier,
|
bytes_to_write: n * multiplier,
|
||||||
break_on_line_end: settings.strategy == "b",
|
break_on_line_end: settings.strategy == "b",
|
||||||
require_whole_line: false,
|
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 {
|
fn split(settings: &Settings) -> i32 {
|
||||||
let mut reader = BufReader::new(if settings.input == "-" {
|
let mut reader = BufReader::new(if settings.input == "-" {
|
||||||
Box::new(stdin()) as Box<Read>
|
Box::new(stdin()) as Box<dyn Read>
|
||||||
} else {
|
} else {
|
||||||
let r = match File::open(Path::new(&settings.input)) {
|
let r = match File::open(Path::new(&settings.input)) {
|
||||||
Ok(a) => a,
|
Ok(a) => a,
|
||||||
|
@ -289,10 +289,10 @@ fn split(settings: &Settings) -> i32 {
|
||||||
settings.input
|
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),
|
"l" => LineSplitter::new(settings),
|
||||||
"b" | "C" => ByteSplitter::new(settings),
|
"b" | "C" => ByteSplitter::new(settings),
|
||||||
a => crash!(1, "strategy {} not supported", a),
|
a => crash!(1, "strategy {} not supported", a),
|
||||||
|
@ -303,7 +303,7 @@ fn split(settings: &Settings) -> i32 {
|
||||||
request_new_file: true, // Request new file
|
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;
|
let mut fileno = 0;
|
||||||
loop {
|
loop {
|
||||||
if control.current_line.chars().count() == 0 {
|
if control.current_line.chars().count() == 0 {
|
||||||
|
@ -333,7 +333,7 @@ fn split(settings: &Settings) -> i32 {
|
||||||
.create(true)
|
.create(true)
|
||||||
.open(Path::new(&filename))
|
.open(Path::new(&filename))
|
||||||
.unwrap(),
|
.unwrap(),
|
||||||
) as Box<Write>);
|
) as Box<dyn Write>);
|
||||||
control.request_new_file = false;
|
control.request_new_file = false;
|
||||||
if settings.verbose {
|
if settings.verbose {
|
||||||
println!("creating file '{}'", filename);
|
println!("creating file '{}'", filename);
|
||||||
|
|
|
@ -21,7 +21,7 @@ use std::path::Path;
|
||||||
static NAME: &str = "sum";
|
static NAME: &str = "sum";
|
||||||
static VERSION: &str = env!("CARGO_PKG_VERSION");
|
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 buf = [0; 1024];
|
||||||
let mut blocks_read = 0;
|
let mut blocks_read = 0;
|
||||||
let mut checksum: u16 = 0;
|
let mut checksum: u16 = 0;
|
||||||
|
@ -41,7 +41,7 @@ fn bsd_sum(mut reader: Box<Read>) -> (usize, u16) {
|
||||||
(blocks_read, checksum)
|
(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 buf = [0; 512];
|
||||||
let mut blocks_read = 0;
|
let mut blocks_read = 0;
|
||||||
let mut ret = 0u32;
|
let mut ret = 0u32;
|
||||||
|
@ -64,12 +64,12 @@ fn sysv_sum(mut reader: Box<Read>) -> (usize, u16) {
|
||||||
(blocks_read, ret as u16)
|
(blocks_read, ret as u16)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn open(name: &str) -> Result<Box<Read>> {
|
fn open(name: &str) -> Result<Box<dyn Read>> {
|
||||||
match name {
|
match name {
|
||||||
"-" => Ok(Box::new(stdin()) as Box<Read>),
|
"-" => Ok(Box::new(stdin()) as Box<dyn Read>),
|
||||||
_ => {
|
_ => {
|
||||||
let f = File::open(&Path::new(name))?;
|
let f = File::open(&Path::new(name))?;
|
||||||
Ok(Box::new(f) as Box<Read>)
|
Ok(Box::new(f) as Box<dyn Read>)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -91,10 +91,10 @@ fn tac(filenames: Vec<String>, before: bool, _: bool, separator: &str) {
|
||||||
|
|
||||||
for filename in &filenames {
|
for filename in &filenames {
|
||||||
let mut file = BufReader::new(if filename == "-" {
|
let mut file = BufReader::new(if filename == "-" {
|
||||||
Box::new(stdin()) as Box<Read>
|
Box::new(stdin()) as Box<dyn Read>
|
||||||
} else {
|
} else {
|
||||||
match File::open(filename) {
|
match File::open(filename) {
|
||||||
Ok(f) => Box::new(f) as Box<Read>,
|
Ok(f) => Box::new(f) as Box<dyn Read>,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
show_warning!("failed to open '{}' for reading: {}", filename, e);
|
show_warning!("failed to open '{}' for reading: {}", filename, e);
|
||||||
continue;
|
continue;
|
||||||
|
|
|
@ -86,7 +86,7 @@ fn exec(options: Options) -> Result<()> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn tee(options: Options) -> Result<()> {
|
fn tee(options: Options) -> Result<()> {
|
||||||
let mut writers: Vec<Box<Write>> = options
|
let mut writers: Vec<Box<dyn Write>> = options
|
||||||
.files
|
.files
|
||||||
.clone()
|
.clone()
|
||||||
.into_iter()
|
.into_iter()
|
||||||
|
@ -95,7 +95,7 @@ fn tee(options: Options) -> Result<()> {
|
||||||
writers.push(Box::new(stdout()));
|
writers.push(Box::new(stdout()));
|
||||||
let output = &mut MultiWriter { writers };
|
let output = &mut MultiWriter { writers };
|
||||||
let input = &mut NamedReader {
|
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() {
|
if copy(input, output).is_err() || output.flush().is_err() {
|
||||||
Err(Error::new(ErrorKind::Other, ""))
|
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 path = PathBuf::from(name);
|
||||||
let inner: Box<Write> = {
|
let inner: Box<dyn Write> = {
|
||||||
let mut options = OpenOptions::new();
|
let mut options = OpenOptions::new();
|
||||||
let mode = if append {
|
let mode = if append {
|
||||||
options.append(true)
|
options.append(true)
|
||||||
|
@ -121,11 +121,11 @@ fn open(name: String, append: bool) -> Box<Write> {
|
||||||
Box::new(NamedWriter {
|
Box::new(NamedWriter {
|
||||||
inner: inner,
|
inner: inner,
|
||||||
path: path,
|
path: path,
|
||||||
}) as Box<Write>
|
}) as Box<dyn Write>
|
||||||
}
|
}
|
||||||
|
|
||||||
struct MultiWriter {
|
struct MultiWriter {
|
||||||
writers: Vec<Box<Write>>,
|
writers: Vec<Box<dyn Write>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Write for MultiWriter {
|
impl Write for MultiWriter {
|
||||||
|
@ -145,7 +145,7 @@ impl Write for MultiWriter {
|
||||||
}
|
}
|
||||||
|
|
||||||
struct NamedWriter {
|
struct NamedWriter {
|
||||||
inner: Box<Write>,
|
inner: Box<dyn Write>,
|
||||||
path: PathBuf,
|
path: PathBuf,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -153,7 +153,7 @@ impl Write for NamedWriter {
|
||||||
fn write(&mut self, buf: &[u8]) -> Result<usize> {
|
fn write(&mut self, buf: &[u8]) -> Result<usize> {
|
||||||
match self.inner.write(buf) {
|
match self.inner.write(buf) {
|
||||||
Err(f) => {
|
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());
|
warn(format!("{}: {}", self.path.display(), f.to_string()).as_ref());
|
||||||
Err(f)
|
Err(f)
|
||||||
}
|
}
|
||||||
|
@ -164,7 +164,7 @@ impl Write for NamedWriter {
|
||||||
fn flush(&mut self) -> Result<()> {
|
fn flush(&mut self) -> Result<()> {
|
||||||
match self.inner.flush() {
|
match self.inner.flush() {
|
||||||
Err(f) => {
|
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());
|
warn(format!("{}: {}", self.path.display(), f.to_string()).as_ref());
|
||||||
Err(f)
|
Err(f)
|
||||||
}
|
}
|
||||||
|
@ -174,7 +174,7 @@ impl Write for NamedWriter {
|
||||||
}
|
}
|
||||||
|
|
||||||
struct NamedReader {
|
struct NamedReader {
|
||||||
inner: Box<Read>,
|
inner: Box<dyn Read>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Read for NamedReader {
|
impl Read for NamedReader {
|
||||||
|
|
|
@ -20,9 +20,9 @@ extern crate getopts;
|
||||||
extern crate uucore;
|
extern crate uucore;
|
||||||
|
|
||||||
use bit_set::BitSet;
|
use bit_set::BitSet;
|
||||||
|
use fnv::FnvHashMap;
|
||||||
use getopts::Options;
|
use getopts::Options;
|
||||||
use std::io::{stdin, stdout, BufRead, BufWriter, Write};
|
use std::io::{stdin, stdout, BufRead, BufWriter, Write};
|
||||||
use fnv::FnvHashMap;
|
|
||||||
|
|
||||||
use expand::ExpandSet;
|
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 buf = String::with_capacity(BUFFER_LEN + 4);
|
||||||
let mut output_buf = String::with_capacity(BUFFER_LEN + 4);
|
let mut output_buf = String::with_capacity(BUFFER_LEN + 4);
|
||||||
|
|
||||||
|
|
|
@ -62,7 +62,7 @@ pub fn uumain(args: Vec<String>) -> i32 {
|
||||||
let mut file_buf;
|
let mut file_buf;
|
||||||
let mut reader = BufReader::new(if input == "-" {
|
let mut reader = BufReader::new(if input == "-" {
|
||||||
stdin_buf = stdin();
|
stdin_buf = stdin();
|
||||||
&mut stdin_buf as &mut Read
|
&mut stdin_buf as &mut dyn Read
|
||||||
} else {
|
} else {
|
||||||
file_buf = match File::open(Path::new(&input)) {
|
file_buf = match File::open(Path::new(&input)) {
|
||||||
Ok(a) => a,
|
Ok(a) => a,
|
||||||
|
@ -71,7 +71,7 @@ pub fn uumain(args: Vec<String>) -> i32 {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
&mut file_buf as &mut Read
|
&mut file_buf as &mut dyn Read
|
||||||
});
|
});
|
||||||
|
|
||||||
let mut g = Graph::new();
|
let mut g = Graph::new();
|
||||||
|
|
|
@ -145,16 +145,16 @@ pub fn uumain(args: Vec<String>) -> i32 {
|
||||||
0
|
0
|
||||||
}
|
}
|
||||||
|
|
||||||
fn open(path: String) -> BufReader<Box<Read + 'static>> {
|
fn open(path: String) -> BufReader<Box<dyn Read + 'static>> {
|
||||||
let file_buf;
|
let file_buf;
|
||||||
if path == "-" {
|
if path == "-" {
|
||||||
BufReader::new(Box::new(stdin()) as Box<Read>)
|
BufReader::new(Box::new(stdin()) as Box<dyn Read>)
|
||||||
} else {
|
} else {
|
||||||
file_buf = match File::open(&path[..]) {
|
file_buf = match File::open(&path[..]) {
|
||||||
Ok(a) => a,
|
Ok(a) => a,
|
||||||
Err(e) => crash!(1, "{}: {}", &path[..], e),
|
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>)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -111,7 +111,7 @@ impl Uniq {
|
||||||
|
|
||||||
fn cmp_key<F>(&self, line: &str, mut closure: F) -> bool
|
fn cmp_key<F>(&self, line: &str, mut closure: F) -> bool
|
||||||
where
|
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 fields_to_check = self.skip_fields(line);
|
||||||
let len = fields_to_check.len();
|
let len = fields_to_check.len();
|
||||||
|
@ -307,26 +307,26 @@ pub fn uumain(args: Vec<String>) -> i32 {
|
||||||
0
|
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 == "-" {
|
let in_file = if in_file_name == "-" {
|
||||||
Box::new(stdin()) as Box<Read>
|
Box::new(stdin()) as Box<dyn Read>
|
||||||
} else {
|
} else {
|
||||||
let path = Path::new(&in_file_name[..]);
|
let path = Path::new(&in_file_name[..]);
|
||||||
let in_file = File::open(&path);
|
let in_file = File::open(&path);
|
||||||
let r = crash_if_err!(1, in_file);
|
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)
|
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 == "-" {
|
let out_file = if out_file_name == "-" {
|
||||||
Box::new(stdout()) as Box<Write>
|
Box::new(stdout()) as Box<dyn Write>
|
||||||
} else {
|
} else {
|
||||||
let path = Path::new(&out_file_name[..]);
|
let path = Path::new(&out_file_name[..]);
|
||||||
let in_file = File::create(&path);
|
let in_file = File::create(&path);
|
||||||
let w = crash_if_err!(1, in_file);
|
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)
|
BufWriter::new(out_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 {
|
if "-" == path {
|
||||||
let reader = Box::new(stdin()) as Box<Read>;
|
let reader = Box::new(stdin()) as Box<dyn Read>;
|
||||||
return Ok(BufReader::new(reader));
|
return Ok(BufReader::new(reader));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -270,7 +270,7 @@ fn open(path: &str) -> StdResult<BufReader<Box<Read + 'static>>, i32> {
|
||||||
}
|
}
|
||||||
match File::open(&fpath) {
|
match File::open(&fpath) {
|
||||||
Ok(fd) => {
|
Ok(fd) => {
|
||||||
let reader = Box::new(fd) as Box<Read>;
|
let reader = Box::new(fd) as Box<dyn Read>;
|
||||||
Ok(BufReader::new(reader))
|
Ok(BufReader::new(reader))
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue