Refactor: use some nice Rust methods in buffering code
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Niklas Mohrin · committed by David Peter · parent 45a8645 · Browse files · 1 file·+12 added−15 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+12 added−15 removed
- other:+12 added−15 removed
Provenance
- Commit
3de948a- Tree
3113c86- Author
Niklas Mohrin <dev@niklasmohrin.de>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
45a8645
1 file changed+12 added−15 removed
modified:src/walk.rs+12 added-15 removed
38 unmodified lines39 }40 41 /// Maximum size of the output buffer before flushing results to the console42 pub const MAX_BUFFER_LENGTH: usize = 1000;43+/// Default duration until output buffering switches to streaming.44+pub const DEFAULT_MAX_BUFFER_TIME: time::Duration = time::Duration::from_millis(100);45 46 /// Recursively scan the given search path for files / pathnames matching the pattern.47 ///48 /// If the `--exec` argument was supplied, this will create a thread pool for executing172 unmodified lines221 // Start in buffering mode222 let mut mode = ReceiverMode::Buffering;223 224 // Maximum time to wait before we start streaming to the console.223- let max_buffer_time = config224- .max_buffer_time225- .unwrap_or_else(|| time::Duration::from_millis(100));225+ let max_buffer_time = config.max_buffer_time.unwrap_or(DEFAULT_MAX_BUFFER_TIME);226 227 let stdout = io::stdout();228 let mut stdout = stdout.lock();229 11 unmodified lines241 buffer.push(value);242 243 // Have we reached the maximum buffer size or maximum buffering time?244 if buffer.len() > MAX_BUFFER_LENGTH245- || time::Instant::now() - start > max_buffer_time245+ || start.elapsed() > max_buffer_time246 {247 // Flush the buffer248 for v in &buffer {249 output::print_entry(14 unmodified lines264 }265 }266 267 num_results += 1;268+ if let Some(max_results) = config.max_results {269+ if num_results >= max_results {270+ break;271+ }272+ }273 }274 WorkerResult::Error(err) => {275 if show_filesystem_errors {276 print_error(err.to_string());277 }278 }279 }275-276- if let Some(max_results) = config.max_results {277- if num_results >= max_results {278- break;279- }280- }280 }281 282 // If we have finished fast enough (faster than max_buffer_time), we haven't streamed283 // anything to the console, yet. In this case, sort the results and print them:285- if !buffer.is_empty() {286- buffer.sort();287- for value in buffer {288- output::print_entry(&mut stdout, &value, &config, &wants_to_quit);284+ buffer.sort();285+ for value in buffer {286+ output::print_entry(&mut stdout, &value, &config, &wants_to_quit);289- }287 }288 289 if config.quiet {290 ExitCode::HasResults(false)209 unmodified lines