heim_process/process/
command.rs

1use std::ffi::{OsStr, OsString};
2use std::fmt;
3
4use crate::sys;
5use heim_common::prelude::wrap;
6
7/// Process command line.
8pub struct Command(sys::Command);
9
10wrap!(Command, sys::Command);
11
12impl Command {
13    /// Create an `OsString` containing the process command line.
14    ///
15    /// Spaces are used as a delimiters in the returned `OsString`.
16    ///
17    /// ## Notes
18    ///
19    /// This method will always allocate memory on all OSes.
20    pub fn to_os_string(&self) -> OsString {
21        self.as_ref().to_os_string()
22    }
23
24    /// Consumes `self` and returns the underline process command line.
25    ///
26    /// Spaces are used as a delimiters in the returned `OsString`.
27    ///
28    /// ## Notes
29    ///
30    /// This method might allocate on some OSes, depending on the implementation.
31    pub fn into_os_string(self) -> OsString {
32        self.0.into_os_string()
33    }
34}
35
36impl fmt::Debug for Command {
37    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
38        f.debug_tuple("Command")
39            .field(&self.to_os_string())
40            .finish()
41    }
42}
43
44/// Iterator over process command line arguments.
45#[derive(Debug)]
46pub struct CommandIter<'a>(sys::CommandIter<'a>);
47
48wrap!('a, CommandIter<'a>, sys::CommandIter<'a>);
49
50impl<'a> IntoIterator for &'a Command {
51    type Item = &'a OsStr;
52    type IntoIter = CommandIter<'a>;
53
54    fn into_iter(self) -> Self::IntoIter {
55        self.0.into_iter().into()
56    }
57}
58
59impl<'a> Iterator for CommandIter<'a> {
60    type Item = &'a OsStr;
61
62    fn next(&mut self) -> Option<Self::Item> {
63        self.0.next()
64    }
65}