diff options
Diffstat (limited to 'vendor/io-lifetimes/examples/portable-views.rs')
-rw-r--r-- | vendor/io-lifetimes/examples/portable-views.rs | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/vendor/io-lifetimes/examples/portable-views.rs b/vendor/io-lifetimes/examples/portable-views.rs new file mode 100644 index 000000000..410b4c2f0 --- /dev/null +++ b/vendor/io-lifetimes/examples/portable-views.rs @@ -0,0 +1,28 @@ +//! io-lifetimes provides safe, convenient, and portable ways to temporarily +//! view an I/O resource as a `File`, `Socket`, or other types. + +#![cfg_attr(io_lifetimes_use_std, feature(io_safety))] + +use io_lifetimes::AsFilelike; +use std::fs::File; +use std::io::{self, stdout}; + +fn main() -> io::Result<()> { + let stdout = stdout(); + + // With `AsFilelike`, any type implementing `AsFd`/`AsHandle` can be viewed + // as any type supporting `FromFilelike`, so you can call `File` methods on + // `Stdout` or other things. + // + // Whether or not you can actually do this is up to the OS, of course. In + // this case, Unix can do this, but it appears Windows can't. + let metadata = stdout.as_filelike_view::<File>().metadata()?; + + if metadata.is_file() { + println!("stdout is a file!"); + } else { + println!("stdout is not a file!"); + } + + Ok(()) +} |