[关闭]
@electricface 2014-04-17T00:52:11.000000Z 字数 2671 阅读 2365

rust 进程调用

库 std::io::Process


结构 Process

  1. pub struct Process {
  2. stdin: Option<PipeStream>,
  3. stdout: Option<PipeStream>,
  4. stderr: Option<PipeStream>,
  5. extra_io: ~[Option<PipeStream>],
  6. // some fields omitted
  7. }

例子 Process::new

  1. use std::io::Process;
  2. fn main(){
  3. let mut child = match Process::new("cat" , [~"abc.rs.."]){
  4. Ok(child) => child,
  5. Err(e) => fail!("failed to execute child: {}",e)
  6. };
  7. let contents = child.stdout.get_mut_ref().read_to_str().unwrap();
  8. println!( "Cotents: {}", contents);
  9. if ! child.wait().success() {
  10. let err_info = child.stderr.get_mut_ref().read_to_str().unwrap() ;
  11. println!( "ERROR: {}", err_info);
  12. }
  13. }

new 函数原型

  1. fn new(prog: &str, args: &[~str]) -> IoResult<Process>

prog 命令地址
args 参数

IoResult 类型

  1. type IoResult<T> = Result<T, IoError>;

例子 Process::output

关心 输入输出

  1. use std::io::Process;
  2. use std::str;
  3. fn main(){
  4. let output = match Process::output("cat",[~"abc.rs"]) {
  5. Ok(o) => o,
  6. Err(e) => fail!("failed to execute process: {}",e),
  7. };
  8. println!("status: {}",output.status);
  9. println!("OUT: {}",str::from_utf8_lossy(output.output) );
  10. println!("ERROR: {}",str::from_utf8_lossy(output.error) );
  11. }

方法 str::from_utf8_lossy

将 [u8] 转换为 MaybeOwened<’a>

  1. pub fn from_utf8_lossy<'a>(v: &'a [u8]) -> MaybeOwned<'a>
  2. //枚举 MaybeOwned<'a> 它可以同时放 ~str 和 &str
  3. pub enum MaybeOwned<'a> {
  4. Slice(&'a str),
  5. Owned(~str),
  6. }
  7. fn is_owned(&self) -> bool
  8. fn into_owned(self) -> ~str
  9. fn is_slice(&self) -> bool
  10. fn as_slice<'b>(&'b self) -> &'b str
  11. //实现了 Show 特性,可用 println!() 输出

结构 ProcessOutput

  1. pub struct ProcessOutput {
  2. status: ProcessExit,
  3. output: ~[u8],
  4. error: ~[u8],
  5. }

枚举 ProcessExit

  1. pub enum ProcessExit {
  2. ExitStatus(int),
  3. ExitSignal(int),
  4. }
  5. //是否成功
  6. fn success(&self) -> bool
  7. //是否匹配退出状态
  8. fn matches_exit_status(&self, wanted: int) -> bool

例子Process::status

只关心退出状态

  1. use std::io::Process;
  2. fn main(){
  3. let status = match Process::status("ls",[~".."]) {
  4. Ok(status) => status,
  5. Err(e) => fail!("failed to execute process: {}",e )
  6. };
  7. if status.success() {
  8. println!("good end");
  9. } else {
  10. println!( "ERROR: process exited with: {}" , status );
  11. }
  12. }

例子 Process::configue

  1. use std::io::{ProcessConfig,Process};
  2. use std::str::from_utf8_lossy;
  3. fn main(){
  4. let config = ProcessConfig {
  5. program: "/bin/sh",
  6. args: &[~"-c", ~"echo 123+345|bc"],
  7. .. ProcessConfig::new()
  8. };
  9. let mut child = match Process::configure(config){
  10. Ok(o) => o,
  11. Err(e) => fail!("failed to execute process: {}",e)
  12. };
  13. let output = child.wait_with_output().output;
  14. println!("OUT: {}" , from_utf8_lossy( output) );
  15. }

结构 ProcessConfig

  1. pub struct ProcessConfig<'a> {
  2. program: &'a str,
  3. args: &'a [~str],
  4. env: Option<&'a [(~str, ~str)]>,
  5. cwd: Option<&'a Path>,
  6. stdin: StdioContainer,
  7. stdout: StdioContainer,
  8. stderr: StdioContainer,
  9. extra_io: &'a [StdioContainer],
  10. uid: Option<uint>,
  11. gid: Option<uint>,
  12. detach: bool,
  13. }

结构 Process 的其他方法

  1. fn kill(id: pid_t, signal: int) -> IoResult<()>
  2. fn id(&self) -> pid_t
  3. fn signal(&mut self, signal: int) -> IoResult<()>
  4. fn signal_exit(&mut self) -> IoResult<()>
  5. fn signal_kill(&mut self) -> IoResult<()>
  6. fn wait(&mut self) -> ProcessExit
  7. fn wait_with_output(&mut self) -> ProcessOutput
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注