Crate io_providers[−][src]
Defines "provider" traits and implementations for different types of I/O operations, enabling dependency injection that's very helpful for testing.
A number of different I/O types are supported:
- Process environment (variables, working directy etc), via
Env
- Standard streams (stdin, stdout and stderr), via
StdStreams
- Filesystem access, via
Fs
In addition to "native" implementations for each trait, "simulated" implementations are also built-in:
SimulatedEnv
for faking process environment stateSimulatedStdStreams
for faking standard stream input and inspecting outputTempFs
for performing filesystem access in achroot
-like sandbox isolated from the rest of the filesystem
Each provider trait can be used independently, however there is also the all-encompassing
Io
which provides access to all of them. If you have a variety of I/O
dependencies, it might be easiest to create and pass around a single &mut Io
.
Examples
extern crate io_providers; use std::io::Write; use std::path::Path; use io_providers::{Env, Io, NativeIo, SimulatedIo, StdStreams}; /// Gets the current working directory and prints it to stdout. fn do_work<I: Io>(io: &mut I) { let cur_dir = io.env().current_dir().unwrap(); let stdout = io.std_streams().output(); writeln!(stdout, "The current directory is: {}", cur_dir.to_str().unwrap()).unwrap(); } fn main() { // Test `do_work()` using a simulated I/O environment let mut simulated_io = SimulatedIo::new().unwrap(); simulated_io.env_mut().set_current_dir(Path::new("/foo/bar")).unwrap(); do_work(&mut simulated_io); assert_eq!( "The current directory is: /foo/bar\n", ::std::str::from_utf8(simulated_io.std_streams().read_output()).unwrap()); // Now use a native I/O provided to access the real system let mut real_io = NativeIo::new(); do_work(&mut real_io); }
Re-exports
pub use env::Env; |
pub use fs::Fs; |
pub use fs::OpenOptions; |
pub use std_streams::StdStreams; |
Modules
env |
Defines traits and implementations for the inspection and manipulation of the process's environment. |
fs |
Defines traits and implementations for filesystem manipulation operations. |
std_streams |
Providers of input/output/error streams (i.e. stdin, stdout and stderr). |
Structs
NativeEnv |
Provides inspection and manipulation of the process's environment, using
|
NativeFs |
Provides access to native file I/O. |
NativeIo |
|
NativeStdStreams |
Handles for the standard input streams of a process, using
|
SimulatedEnv |
Provides inspection and manipulation of a simulated process environment. |
SimulatedIo |
|
SimulatedStdStreams |
Simulated handles for the standard input streams of a process. |
TempFs |
Provides access to file I/O in a chroot-like temporary filesystem, located in the system's default temp directory. This temporary directory acts like the root of the filesystem: all absolute paths are relative to it, and any path which would traverse out of it is considered invalid. |
Traits
Io |
Provides access to the process environment, filesystem, and standard streams. |