scoped_env

Struct ScopedEnv

Source
pub struct ScopedEnv<T>
where T: AsRef<OsStr>,
{ /* private fields */ }
Expand description

A rust lifetime scope for a set environment variable. When an instance goes out of scope it will automatically cleanup the environment

Implementations§

Source§

impl<T> ScopedEnv<T>
where T: AsRef<OsStr>,

Source

pub fn set(name: T, value: T) -> Self

Sets the environment variable {name} to {value}. The returned instance should be assigned to a _name binding so that it lasts as long as the current block.

use scoped_env::ScopedEnv;
let c = ScopedEnv::set("HELLO", "WORLD");
assert_eq!(std::env::var(c).unwrap().as_str(), "WORLD");
Source

pub fn remove(name: T) -> Self

Removes the environment variable {name} from the environment of the currently running process. The returned instance should be assigned to a _name binding. The variable value will be restored when the handle goes out of scope.

use scoped_env::ScopedEnv;
std::env::set_var("HELLO", "WORLD");
{
    let c = ScopedEnv::remove("HELLO");
    assert!(std::env::var(c).is_err());
}
assert_eq!(std::env::var("HELLO").unwrap().as_str(), "WORLD");

Trait Implementations§

Source§

impl<T> AsRef<OsStr> for ScopedEnv<T>
where T: AsRef<OsStr>,

Source§

fn as_ref(&self) -> &OsStr

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<T> Drop for ScopedEnv<T>
where T: AsRef<OsStr>,

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<T> Freeze for ScopedEnv<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for ScopedEnv<T>
where T: RefUnwindSafe,

§

impl<T> Send for ScopedEnv<T>
where T: Send,

§

impl<T> Sync for ScopedEnv<T>
where T: Sync,

§

impl<T> Unpin for ScopedEnv<T>
where T: Unpin,

§

impl<T> UnwindSafe for ScopedEnv<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.