-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
97 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,48 @@ | ||
# stdio-override | ||
Overriding Stdio file descriptors in Rust | ||
[![Build Status](https://travis-ci.com/elichai/stdio-override.svg?branch=master)](https://travis-ci.com/elichai/stdio-override) | ||
[![Latest version](https://img.shields.io/crates/v/stdio-override.svg)](https://crates.io/crates/stdio-override) | ||
[![Documentation](https://docs.rs/stdio-override/badge.svg)](https://docs.rs/stdio-override) | ||
![License](https://img.shields.io/crates/l/stdio-override.svg) | ||
|
||
A Rust library to easily override Stdio file descriptors in Rust | ||
|
||
* [Documentation](https://docs.rs/stdio-override) | ||
|
||
## Usage | ||
|
||
Add this to your `Cargo.toml`: | ||
|
||
```toml | ||
[dependencies] | ||
stdio-override = "0.1" | ||
|
||
``` | ||
|
||
and for Rust Edition 2015 add this to your crate root: | ||
|
||
```rust | ||
extern crate stdio_override; | ||
``` | ||
In Rust Edition 2018 you can simply do: | ||
```rust | ||
use stdio_override::*; | ||
``` | ||
|
||
Here's an example on how to write stdout into a file: | ||
|
||
```rust | ||
use stdio_override::StdoutOverride; | ||
use std::{fs::read_to_string, mem}; | ||
|
||
fn main() { | ||
let file_name = "./test.txt"; | ||
|
||
let guard = StdoutOverride::override_file(file_name).unwrap(); | ||
println!("12345"); | ||
mem::drop(guard); | ||
|
||
let contents = read_to_string(file_name).unwrap(); | ||
assert_eq!("12345\n", contents); | ||
println!("Outside!"); | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,37 @@ | ||
#![deny(missing_docs)] | ||
#![cfg_attr(test, deny(warnings))] | ||
|
||
//! # Stdio-Override | ||
//! | ||
//! This crate provides a library for overriding Stdio file descriptors. <br> | ||
//! It provides a Guard for the replacement so that when the guard is dropped the file descriptors are switched back | ||
//! and the replacement File Descriptor will be closed. | ||
//! | ||
//! ** Trying to replace an std File Descriptor twice will result in a panic ** <br> | ||
//! | ||
//! *Notice:* When trying to use this in tests you **must** run with `cargo test -- --nocapture` otherwise it will redirect stdout/stderr again. | ||
//! | ||
//! This library is made to be intuitive and easy to use. | ||
//! | ||
//! ## Examples | ||
//! ```rust | ||
//! use stdio_override::StdoutOverride; | ||
//! use std::{fs, mem}; | ||
//! let file_name = "./test.txt"; | ||
//! let guard = StdoutOverride::override_file(file_name).unwrap(); | ||
//! println!("Isan to Stdout!"); | ||
//! | ||
//! let contents = fs::read_to_string(file_name).unwrap(); | ||
//! assert_eq!("Isan to Stdout!\n", contents); | ||
//! mem::drop(guard); | ||
//! println!("Outside!"); | ||
//! ``` | ||
//! | ||
mod ffi; | ||
mod stdout; | ||
|
||
|
||
pub use stdout::{StdoutOverride, StdoutOverrideGuard}; | ||
|
||
#[cfg(feature = "test-readme")] | ||
doc_comment::doctest!("../README.md"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters