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
type ArgsIter: Iterator<Item = String>
The iterator type returned by args()
.
type ArgsOsIter: Iterator<Item = OsString>
The iterator type returned by args_os()
.
type VarsIter: Iterator<Item = (String, String)>
The iterator type returned by vars()
.
type VarsOsIter: Iterator<Item = (OsString, OsString)>
The iterator type returned by vars_os()
.
Required Methods
fn args(&self) -> Self::ArgsIter
Returns the arguments which this program was started with (normally passed via the command line).
See std::env::args
for more information.
fn args_os(&self) -> Self::ArgsOsIter
Returns the arguments which this program was started with (normally passed via the command line).
See std::env::args_os
for more
information.
fn current_dir(&self) -> Result<PathBuf>
Returns the current working directory as a PathBuf
.
See std::env::current_dir
for
more information.
fn current_exe(&self) -> Result<PathBuf>
Returns the full filesystem path of the current running executable.
See std::env::current_exe
for
more information.
fn home_dir(&self) -> Option<PathBuf>
: 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.
fn remove_var<K: AsRef<OsStr>>(&mut self, k: K)
Removes an environment variable from the environment of the currently running process.
See std::env::remove_var
for more
information.
fn set_current_dir<P: AsRef<Path>>(&mut self, path: P) -> Result<()>
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.
fn set_var<K: AsRef<OsStr>, V: AsRef<OsStr>>(&mut self, k: K, v: V)
Sets the environment variable k
to the value v
for the currently running process.
See std::env::set_var
for more
information.
fn temp_dir(&self) -> PathBuf
Returns the path of a temporary directory.
See std::env::temp_dir
for more
information.
fn var<K: AsRef<OsStr>>(&self, key: K) -> Result<String, VarError>
Fetches the environment variable key
from the current process.
See std::env::var
for more information.
fn var_os<K: AsRef<OsStr>>(&self, key: K) -> Option<OsString>
Fetches the environment variable key
from the current process.
See std::env::var_os
for more information.
fn vars(&self) -> Self::VarsIter
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.
fn vars_os(&self) -> Self::VarsOsIter
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
impl Env for NativeEnv type ArgsIter = Args; type ArgsOsIter = ArgsOs; type VarsIter = Vars; type VarsOsIter = VarsOs;
impl Env for SimulatedEnv type ArgsIter = IntoIter<String>; type ArgsOsIter = IntoIter<OsString>; type VarsIter = IntoIter<(String, String)>; type VarsOsIter = IntoIter<(OsString, OsString)>;