Skip to content

Commit 5f81c1c

Browse files
authored
Borrow build path when emitting manifest (#90)
* Use Cow for build path * Borrow temporary build file path * Use en-GB-oxendict
1 parent 5cafab9 commit 5f81c1c

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

src/runner.rs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use crate::cli::{BuildArgs, Cli, Commands};
88
use crate::{ir::BuildGraph, manifest, ninja_gen};
99
use anyhow::{Context, Result};
1010
use serde_json;
11+
use std::borrow::Cow;
1112
use std::fs;
1213
use std::io::{self, BufRead, BufReader, Write};
1314
use std::path::{Path, PathBuf};
@@ -142,18 +143,28 @@ fn handle_build(cli: &Cli, args: &BuildArgs) -> Result<()> {
142143
let ninja = generate_ninja(cli)?;
143144
let targets = BuildTargets::new(&args.targets);
144145

145-
// Normalise the build file path and keep the temporary file alive for the
146-
// duration of the Ninja invocation.
147-
let (build_path, _tmp): (PathBuf, Option<NamedTempFile>) = if let Some(path) = &args.emit {
146+
// Normalize the build file path and keep the temporary file alive for the
147+
// duration of the Ninja invocation. Borrow the emitted path when provided
148+
// to avoid unnecessary allocation.
149+
let build_path: Cow<Path>;
150+
let mut tmp_file: Option<NamedTempFile> = None;
151+
if let Some(path) = &args.emit {
148152
write_ninja_file(path, &ninja)?;
149-
(path.clone(), None)
153+
build_path = Cow::Borrowed(path.as_path());
150154
} else {
151155
let tmp = create_temp_ninja_file(&ninja)?;
152-
(tmp.path().to_path_buf(), Some(tmp))
153-
};
156+
tmp_file = Some(tmp);
157+
build_path = Cow::Borrowed(
158+
tmp_file
159+
.as_ref()
160+
.expect("temporary Ninja file should exist")
161+
.path(),
162+
);
163+
}
154164

155165
let program = resolve_ninja_program();
156-
run_ninja(program.as_path(), cli, &build_path, &targets)?;
166+
run_ninja(program.as_path(), cli, build_path.as_ref(), &targets)?;
167+
drop(tmp_file);
157168
Ok(())
158169
}
159170

0 commit comments

Comments
 (0)