#![allow(unused)]fnmain(){usestd::borrow::Cow;lets="foo".to_string();letmutcow=Cow::Borrowed(&s);cow.to_mut().make_ascii_uppercase();println!("s is : {}",s);println!("cow is : {}",cow);assert_eq!(cow,Cow::Owned(String::from("FOO"))asCow<'_,str>);}
#![allow(unused)]fnmain(){usestd::borrow::Cow;fnabs_all(input:&mutCow<'_,[i32]>){foriin0..input.len(){letv=input[i];ifv<0{// Clones into a vector if not already owned.input.to_mut()[i]=-v;}}println!("input is : {:?}",input);}// No clone occurs because `input` doesn't need to be mutated.letslice=[0,1,2];letmutinput=Cow::from(&slice[..]);abs_all(&mutinput);println!("slice 1 is : {:?}",slice);// Clone occurs because `input` needs to be mutated.letslice=[-1,0,1];letmutinput=Cow::from(&slice[..]);abs_all(&mutinput);println!("slice 2 is : {:?}",slice);// No clone occurs because `input` is already owned.letmutinput=Cow::from(vec![-1,0,1]);abs_all(&mutinput);println!("input 3 is : {:?}",input);}
/// Take a platform specific shell command and insert the actual task command via templating.
pub fn compile_shell_command(settings: &Settings, command: &str) -> Command {
let shell_command = get_shell_command(settings);
let mut handlebars = handlebars::Handlebars::new();
handlebars.set_strict_mode(true);
handlebars.register_escape_fn(handlebars::no_escape);
// Make the command available to the template engine.
let mut parameters = HashMap::new();
parameters.insert("pueue_command_string", command);
// We allow users to provide their own shell command.
// They should use the `{{ pueue_command_string }}` placeholder.
let mut compiled_command = Vec::new();
for part in shell_command {
let compiled_part = handlebars
.render_template(&part, ¶meters)
.unwrap_or_else(|_| {
panic!("Failed to render shell command for template: {part} and parameters: {parameters:?}")
});
compiled_command.push(compiled_part);
}
let executable = compiled_command.remove(0);
// Chain two `powershell` commands, one that sets the output encoding to utf8 and then the user provided one.
let mut command = Command::new(executable);
for arg in compiled_command {
command.arg(&arg);
}
// Inject custom environment variables.
if !settings.daemon.env_vars.is_empty() {
log::info!(
"Inject environment variables: {:?}",
&settings.daemon.env_vars
);
command.envs(&settings.daemon.env_vars);
}
command
}