Trait io_providers::env::Env[][src]

pub trait Env {
    type ArgsIter: Iterator<Item = String>;
    type ArgsOsIter: Iterator<Item = OsString>;
    type VarsIter: Iterator<Item = (String, String)>;
    type VarsOsIter: Iterator<Item = (OsString, OsString)>;
    fn args(&self) -> Self::ArgsIter;
fn args_os(&self) -> Self::ArgsOsIter;
fn current_dir(&self) -> Result<PathBuf>;
fn current_exe(&self) -> Result<PathBuf>;
fn home_dir(&self) -> Option<PathBuf>;
fn remove_var<K: AsRef<OsStr>>(&mut self, k: K);
fn set_current_dir<P: AsRef<Path>>(&mut self, path: P) -> Result<()>;
fn set_var<K: AsRef<OsStr>, V: AsRef<OsStr>>(&mut self, k: K, v: V);
fn temp_dir(&self) -> PathBuf;
fn var<K: AsRef<OsStr>>(&self, key: K) -> Result<String, VarError>;
fn var_os<K: AsRef<OsStr>>(&self, key: K) -> Option<OsString>;
fn vars(&self) -> Self::VarsIter;
fn vars_os(&self) -> Self::VarsOsIter; }

Provides inspection and manipulation of the process's environment.

This roughly corresponds to std::env.

Examples

extern crate io_providers;

use std::path::{Path, PathBuf};
use io_providers::{Env, NativeEnv, SimulatedEnv};

/// Uses `Env` to check if the currect working directory is "/foo/bar"
fn curdir_is_foobar<E: Env>(env: &mut E) -> bool {
    let cur_dir = env.current_dir().unwrap();
    cur_dir == PathBuf::from("/foo/bar")
}

fn main() {
    // By creating a fake `Env` and set its current working directory, we can use it to test
    // the behaviour of `curdir_is_foobar()`.
    let mut env = SimulatedEnv::new();
    env.set_current_dir(Path::new("/nope"));

    // Test that our function returns false with a current working directory of "/nope"
    assert!(!curdir_is_foobar(&mut env));

    // Now set the fake working directory to "/foo/bar" and confirm that our function returns
    // `true`
    env.set_current_dir(Path::new("/foo/bar"));
    assert!(curdir_is_foobar(&mut env));

    // To use the real system environment, we use a `NativeEnv` instead
    assert!(!curdir_is_foobar(&mut NativeEnv));
}

Associated Types

The iterator type returned by args().

The iterator type returned by args_os().

The iterator type returned by vars().

The iterator type returned by vars_os().

Required Methods

Returns the arguments which this program was started with (normally passed via the command line).

See std::env::args for more information.

Returns the arguments which this program was started with (normally passed via the command line).

See std::env::args_os for more information.

Returns the current working directory as a PathBuf.

See std::env::current_dir for more information.

Returns the full filesystem path of the current running executable.

See std::env::current_exe for more information.

Deprecated since 0.2.0

: This function's behavior is unexpected and probably not what you want. Consider using the home_dir function from crates.io/crates/dirs instead.

Returns the path of the current user's home directory if known.

See std::env::home_dir for more information.

Removes an environment variable from the environment of the currently running process.

See std::env::remove_var for more information.

Changes the current working directory to the specified path, returning whether the change was completed successfully or not.

See std::env::set_current_dir for more information.

Sets the environment variable k to the value v for the currently running process.

See std::env::set_var for more information.

Returns the path of a temporary directory.

See std::env::temp_dir for more information.

Fetches the environment variable key from the current process.

See std::env::var for more information.

Fetches the environment variable key from the current process.

See std::env::var_os for more information.

Returns an iterator of (variable, value) pairs of strings, for all the environment variables of the current process.

See std::env::vars for more information.

Returns an iterator of (variable, value) pairs of OS strings, for all the environment variables of the current process.

See std::env::vars_os for more information.

Implementors