Skip to content
This repository has been archived by the owner on Oct 12, 2022. It is now read-only.

Commit

Permalink
Prevent unnecessary copy
Browse files Browse the repository at this point in the history
  • Loading branch information
r-darwish committed Jun 27, 2018
1 parent 5d1ec98 commit d7e2db1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ fn run() -> Result<(), Error> {
let git = Git::new();
let mut git_repos = Repositories::new(&git);
let terminal = Terminal::new();
let mut reports = Report::new();
let config = Config::read()?;
let mut reports = Report::new();

let sudo = if cfg!(target_os = "linux") {
utils::which("sudo")
Expand Down Expand Up @@ -232,7 +232,7 @@ fn run() -> Result<(), Error> {
if let Some(commands) = config.commands() {
for (name, command) in commands {
terminal.print_separator(name);
run_custom_command(&command).report(name.as_ref(), &mut reports);
run_custom_command(&command).report(name.as_str(), &mut reports);
}
}

Expand Down
19 changes: 10 additions & 9 deletions src/report.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
use std::borrow::Cow;

pub type Report = Vec<(String, bool)>;
type CowString<'a> = Cow<'a, str>;
pub type Report<'a> = Vec<(CowString<'a>, bool)>;

pub trait Reporter {
fn report<'a, M: Into<Cow<'a, str>>>(&self, key: M, report: &mut Report);
fn report<'a, M: Into<CowString<'a>>>(&self, key: M, report: &mut Report<'a>);
}

impl<T, E> Reporter for Result<T, E>
where
T: Reporter,
{
fn report<'a, M: Into<Cow<'a, str>>>(&self, key: M, report: &mut Report) {
fn report<'a, M: Into<CowString<'a>>>(&self, key: M, report: &mut Report<'a>) {
match self {
Err(_) => {
report.push((key.into().into_owned(), false));
report.push((key.into(), false));
}
Ok(item) => {
item.report(key, report);
Expand All @@ -26,21 +27,21 @@ impl<T> Reporter for Option<T>
where
T: Reporter,
{
fn report<'a, M: Into<Cow<'a, str>>>(&self, key: M, report: &mut Report) {
fn report<'a, M: Into<CowString<'a>>>(&self, key: M, report: &mut Report<'a>) {
if let Some(item) = self {
item.report(key, report);
}
}
}

impl Reporter for bool {
fn report<'a, M: Into<Cow<'a, str>>>(&self, key: M, report: &mut Report) {
report.push((key.into().into_owned(), *self));
fn report<'a, M: Into<CowString<'a>>>(&self, key: M, report: &mut Report<'a>) {
report.push((key.into(), *self));
}
}

impl Reporter for () {
fn report<'a, M: Into<Cow<'a, str>>>(&self, key: M, report: &mut Report) {
report.push((key.into().into_owned(), true));
fn report<'a, M: Into<CowString<'a>>>(&self, key: M, report: &mut Report<'a>) {
report.push((key.into(), true));
}
}

0 comments on commit d7e2db1

Please sign in to comment.