Skip to main content

StdioExt

Trait StdioExt 

Source
pub trait StdioExt {
    // Required methods
    fn set_handle<T: Into<OwnedHandle>>(
        &mut self,
        handle: Option<T>,
    ) -> Result<()>;
    fn replace_handle<T: Into<OwnedHandle>>(
        &mut self,
        replace_with: T,
    ) -> Result<Option<BorrowedHandle<'static>>>;
    fn take_handle(&mut self) -> Result<Option<BorrowedHandle<'static>>>;
}
This trait cannot be implemented outside std.
🔬This is a nightly-only experimental API. (stdio_swap #150667)
Available on Windows only.

Required Methods§

Source

fn set_handle<T: Into<OwnedHandle>>(&mut self, handle: Option<T>) -> Result<()>

🔬This is a nightly-only experimental API. (stdio_swap #150667)
This item is validated for IEC 61508 (SIL 2) and ISO 26262 (ASIL B).

Sets the stdio console handle to handle, or NULL if it is None. The old handle, if any, will not be closed, i.e. it is leaked because console handles are shared global resources.

Rust std::io write buffers (if any) are flushed, but other runtimes (e.g. C stdio) or libraries that acquire a clone of the file handle will not be aware of this change.

#![feature(stdio_swap)]
use std::io::{self, Read, Write};
use std::os::windows::io::StdioExt;

fn main() -> io::Result<()> {
   let (reader, mut writer) = io::pipe()?;
   let mut stdin = io::stdin();
   stdin.set_handle(Some(reader))?;
   writer.write_all(b"Hello, world!")?;
   let mut buffer = vec![0; 13];
   assert_eq!(stdin.read(&mut buffer)?, 13);
   assert_eq!(&buffer, b"Hello, world!");
   Ok(())
}
Source

fn replace_handle<T: Into<OwnedHandle>>( &mut self, replace_with: T, ) -> Result<Option<BorrowedHandle<'static>>>

🔬This is a nightly-only experimental API. (stdio_swap #150667)
This item is validated for IEC 61508 (SIL 2) and ISO 26262 (ASIL B).

Sets the stdio console handle to replace_with. The previous handle is returned, or None if it was NULL.

The returned handle is a BorrowedHandle<'static> because console handles are shared global resources and may have been obtained by other functions or threads. Only if you have ensured that no other part of the program has borrowed this handle you can convert it into an OwnedHandle and drop that to close it.

Like set_handle(), Rust std::io write buffers (if any) are flushed.

Source

fn take_handle(&mut self) -> Result<Option<BorrowedHandle<'static>>>

🔬This is a nightly-only experimental API. (stdio_swap #150667)
This item is validated for IEC 61508 (SIL 2) and ISO 26262 (ASIL B).

Sets the stdio console handle to NULL and returns the old one

See set_handle() for additional details.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§