diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:02:58 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:02:58 +0000 |
commit | 698f8c2f01ea549d77d7dc3338a12e04c11057b9 (patch) | |
tree | 173a775858bd501c378080a10dca74132f05bc50 /vendor/io-lifetimes/examples/easy-conversions.rs | |
parent | Initial commit. (diff) | |
download | rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.tar.xz rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.zip |
Adding upstream version 1.64.0+dfsg1.upstream/1.64.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/io-lifetimes/examples/easy-conversions.rs')
-rw-r--r-- | vendor/io-lifetimes/examples/easy-conversions.rs | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/vendor/io-lifetimes/examples/easy-conversions.rs b/vendor/io-lifetimes/examples/easy-conversions.rs new file mode 100644 index 000000000..87481c12f --- /dev/null +++ b/vendor/io-lifetimes/examples/easy-conversions.rs @@ -0,0 +1,30 @@ +//! io-lifetimes provides safe, portable, and convenient conversions from types +//! implementing `IntoFilelike` and `FromSocketlike` to types implementing +//! `FromFilelike` and `IntoSocketlike`, respectively. + +#![cfg_attr(io_lifetimes_use_std, feature(io_safety))] + +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(()) +} |