command_group/tokio/
windows.rs

1use tokio::process::Command;
2use winapi::um::winbase::CREATE_SUSPENDED;
3
4use crate::{builder::CommandGroupBuilder, winres::*, AsyncGroupChild};
5
6impl CommandGroupBuilder<'_, Command> {
7	/// Executes the command as a child process group, returning a handle to it.
8	///
9	/// By default, stdin, stdout and stderr are inherited from the parent.
10	///
11	/// On Windows, this creates a job object instead of a POSIX process group.
12	///
13	/// # Examples
14	///
15	/// Basic usage:
16	///
17	/// ```no_run
18	/// use tokio::process::Command;
19	/// use command_group::CommandGroup;
20	///
21	/// Command::new("ls")
22	///         .group()
23	/// 		.spawn()
24	///         .expect("ls command failed to start");
25	/// ```
26	pub fn spawn(&mut self) -> std::io::Result<AsyncGroupChild> {
27		let (job, completion_port) = job_object(self.kill_on_drop)?;
28		self.command
29			.creation_flags(self.creation_flags | CREATE_SUSPENDED);
30
31		let child = self.command.spawn()?;
32		assign_child(
33			child
34				.raw_handle()
35				.expect("child has exited but it has not even started"),
36			job,
37		)?;
38
39		Ok(AsyncGroupChild::new(child, job, completion_port))
40	}
41}