@@ -8,6 +8,7 @@ use crate::cli::{BuildArgs, Cli, Commands};
88use crate :: { ir:: BuildGraph , manifest, ninja_gen} ;
99use anyhow:: { Context , Result } ;
1010use serde_json;
11+ use std:: borrow:: Cow ;
1112use std:: fs;
1213use std:: io:: { self , BufRead , BufReader , Write } ;
1314use 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