From 64c1f164211d6f7bb147cf2be9c14e963aad2cf2 Mon Sep 17 00:00:00 2001 From: Michael Debertol Date: Fri, 7 May 2021 23:40:07 +0200 Subject: [PATCH] sort: allow some functions to be called with OsStr --- src/uu/sort/src/sort.rs | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/uu/sort/src/sort.rs b/src/uu/sort/src/sort.rs index d8978cb2b..730be0039 100644 --- a/src/uu/sort/src/sort.rs +++ b/src/uu/sort/src/sort.rs @@ -20,8 +20,8 @@ mod external_sort; mod numeric_str_cmp; use clap::{App, Arg}; -use external_sort::ext_sort; use custom_str_cmp::custom_str_cmp; +use external_sort::ext_sort; use fnv::FnvHasher; use itertools::Itertools; use numeric_str_cmp::{numeric_str_cmp, NumInfo, NumInfoParseSettings}; @@ -33,6 +33,7 @@ use smallvec::SmallVec; use std::cmp::Ordering; use std::collections::BinaryHeap; use std::env; +use std::ffi::OsStr; use std::fs::File; use std::hash::{Hash, Hasher}; use std::io::{stdin, stdout, BufRead, BufReader, BufWriter, Read, Write}; @@ -1109,10 +1110,10 @@ pub fn uumain(args: impl uucore::Args) -> i32 { exec(files, settings) } -fn file_to_lines_iter<'a>( - file: &str, - settings: &'a GlobalSettings, -) -> Option + 'a> { +fn file_to_lines_iter( + file: impl AsRef, + settings: &'_ GlobalSettings, +) -> Option + '_> { let (reader, _) = match open(file) { Some(x) => x, None => return None, @@ -1177,7 +1178,7 @@ fn exec(files: Vec, settings: GlobalSettings) -> i32 { let mut lines = vec![]; // This is duplicated from fn file_to_lines_iter, but using that function directly results in a performance regression. - for (file, _) in files.iter().map(|file| open(file)).flatten() { + for (file, _) in files.iter().map(open).flatten() { let buf_reader = BufReader::new(file); for line in buf_reader.split(if settings.zero_terminated { b'\0' @@ -1501,7 +1502,8 @@ fn print_sorted>(iter: T, settings: &GlobalSettings) { } // from cat.rs -fn open(path: &str) -> Option<(Box, bool)> { +fn open(path: impl AsRef) -> Option<(Box, bool)> { + let path = path.as_ref(); if path == "-" { let stdin = stdin(); return Some((Box::new(stdin) as Box, is_stdin_interactive())); @@ -1510,7 +1512,7 @@ fn open(path: &str) -> Option<(Box, bool)> { match File::open(Path::new(path)) { Ok(f) => Some((Box::new(f) as Box, false)), Err(e) => { - show_error!("{0}: {1}", path, e.to_string()); + show_error!("{0:?}: {1}", path, e.to_string()); None } }