-
Notifications
You must be signed in to change notification settings - Fork 10
/
easy-conversions.rs
28 lines (23 loc) · 996 Bytes
/
easy-conversions.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
//! io-lifetimes provides safe, portable, and convenient conversions from types
//! implementing `IntoFilelike` and `FromSocketlike` to types implementing
//! `FromFilelike` and `IntoSocketlike`, respectively.
use io_lifetimes::FromFilelike;
use std::fs::File;
use std::io::{self, Read};
use std::process::{Command, Stdio};
fn main() -> io::Result<()> {
let mut child = Command::new("cargo")
.arg("--help")
.stdout(Stdio::piped())
.spawn()
.expect("failed to execute child");
// Convert from `ChildStderr` into `File` without any platform-specific
// code or `unsafe`!
let mut file = File::from_into_filelike(child.stdout.take().unwrap());
// Well, this example is not actually that cool, because `File` doesn't let
// you do anything that you couldn't already do with `ChildStderr` etc., but
// it's useful outside of standard library types.
let mut buffer = String::new();
file.read_to_string(&mut buffer)?;
Ok(())
}