Skip to content

Commit

Permalink
Use Cow to avoid clone() of static ref
Browse files Browse the repository at this point in the history
  • Loading branch information
dandavison committed Nov 22, 2021
1 parent b90346b commit e108629
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
4 changes: 3 additions & 1 deletion src/handlers/git_show_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ impl<'a> StateMachine<'a> {
self.painter.emit()?;
let mut handled_line = false;
if matches!(self.state, State::Unknown) {
if let Some(process::CallingProcess::GitShow(extension)) = process::calling_process() {
if let Some(process::CallingProcess::GitShow(extension)) =
process::calling_process().as_deref()
{
self.state = State::GitShowFile;
self.painter.set_syntax(Some(extension));
self.painter.set_highlighter();
Expand Down
4 changes: 2 additions & 2 deletions src/handlers/grep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ fn get_code_style_sections<'b>(
}

fn make_output_config() -> GrepOutputConfig {
match process::calling_process() {
match process::calling_process().as_deref() {
Some(process::CallingProcess::GitGrep((longs, shorts)))
if shorts.contains("-W") || longs.contains("--function-context") =>
{
Expand Down Expand Up @@ -375,7 +375,7 @@ pub fn parse_grep_line(line: &str) -> Option<GrepLine> {
if line.starts_with('{') {
ripgrep_json::parse_line(line)
} else {
match process::calling_process() {
match process::calling_process().as_deref() {
Some(process::CallingProcess::GitGrep(_))
| Some(process::CallingProcess::OtherGrep) => [
&*GREP_LINE_REGEX_ASSUMING_FILE_EXTENSION,
Expand Down
9 changes: 6 additions & 3 deletions src/utils/process.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::borrow::Cow;
use std::collections::{HashMap, HashSet};
use std::path::Path;
use sysinfo::{Pid, Process, ProcessExt, SystemExt};
Expand All @@ -11,14 +12,16 @@ pub enum CallingProcess {
OtherGrep, // rg, grep, ag, ack, etc
}

pub fn calling_process() -> Option<CallingProcess> {
pub fn calling_process() -> Option<Cow<'static, CallingProcess>> {
#[cfg(not(test))]
{
CACHED_CALLING_PROCESS.clone()
CACHED_CALLING_PROCESS
.as_ref()
.map(|proc| Cow::Borrowed(proc))
}
#[cfg(test)]
{
determine_calling_process()
determine_calling_process().map(|proc| Cow::Owned(proc))
}
}

Expand Down

0 comments on commit e108629

Please sign in to comment.