#io #file-descriptor #file-io #unix #unix-socket #file #stdio

stdio-override

Rust library for overriding Stdin/Stdout/Stderr with a different File Descriptor

4 releases

0.1.3 May 2, 2019
0.1.2 May 2, 2019
0.1.1 May 2, 2019
0.1.0 May 1, 2019

#782 in Unix APIs

Download history 486/week @ 2023-12-10 357/week @ 2023-12-17 261/week @ 2023-12-24 474/week @ 2023-12-31 509/week @ 2024-01-07 383/week @ 2024-01-14 386/week @ 2024-01-21 326/week @ 2024-01-28 579/week @ 2024-02-04 552/week @ 2024-02-11 478/week @ 2024-02-18 468/week @ 2024-02-25 820/week @ 2024-03-03 777/week @ 2024-03-10 673/week @ 2024-03-17 758/week @ 2024-03-24

3,070 downloads per month
Used in 3 crates

MIT/Apache

15KB
168 lines

stdio-override

Build Status Latest version Documentation License

A Rust library to easily override Stdio file descriptors in Rust

Usage

Add this to your Cargo.toml:

[dependencies]
stdio-override = "0.1"

and for Rust Edition 2015 add this to your crate root:

extern crate stdio_override;

In Rust Edition 2018 you can simply do:

use stdio_override::*;

Here's an example on how to write stdout into a file:

use std::{fs::read_to_string, io};
use stdio_override::StdoutOverride;

fn main() -> io::Result<()> {
    let file_name = "./readme_test.txt";

    let guard = StdoutOverride::override_file(file_name)?;
    println!("12345");
    drop(guard);

    let contents = read_to_string(file_name)?;
    assert_eq!("12345\n", contents);
    println!("Outside!");
    Ok(())
}

You can do the same with sockets:

use std::{
    io::Read,
    net::{TcpListener, TcpStream},
};
use stdio_override::StdoutOverride;

fn main() {
    let address = ("127.0.0.1", 5543);

    let listener = TcpListener::bind(address).unwrap();
    let socket = TcpStream::connect(address).unwrap();

    let guard = StdoutOverride::override_raw(socket).unwrap();
    println!("12345");
    drop(guard);

    let mut contents = String::new();
    let (mut stream, _) = listener.accept().unwrap();
    stream.read_to_string(&mut contents).unwrap();

    assert_eq!("12345\n", contents);

    println!("Outside!");
}

Both will work the same for Stderr and if you want to input Stdin from a file/socket you can do the following:

use std::{fs::{File, read_to_string}, io::{self, Write}};
use stdio_override::StdinOverride;

fn main() -> io::Result<()> {
    let file_name = "./test_inputs.txt";
    
    {
        let mut file = File::create(&file_name)?;
        file.write_all(b"Data")?;
    }

    let guard = StdinOverride::override_file(file_name)?;
    
    let mut inputs = String::new();
    io::stdin().read_line(&mut inputs)?;
    
    drop(guard);

    assert_eq!("Data", inputs);
    // Stdin is working as usual again, because the guard is dropped.
    Ok(())
}

Dependencies

~45KB