pub struct EnvVarGuard { /* private fields */ }Expand description
Guard that temporarily sets an environment variable and restores the previous value on drop.
This guard is useful for testing or temporarily modifying environment variables in a controlled manner. When the guard is dropped, it automatically restores the environment variable to its previous state (or removes it if it didn’t exist before).
§Safety
This struct uses unsafe blocks to modify environment variables, which is necessary
because Rust’s standard library marks env::set_var and env::remove_var as unsafe
due to their potential for data races in multi-threaded contexts. The guard ensures
that modifications are properly cleaned up.
§Examples
use xet_runtime::utils::EnvVarGuard;
// Temporarily set an environment variable
let _guard = EnvVarGuard::set("MY_VAR", "test_value");
// Environment variable is set here
// When _guard is dropped, the previous value (or absence) is restoredImplementations§
Trait Implementations§
Auto Trait Implementations§
impl Freeze for EnvVarGuard
impl RefUnwindSafe for EnvVarGuard
impl Send for EnvVarGuard
impl Sync for EnvVarGuard
impl Unpin for EnvVarGuard
impl UnsafeUnpin for EnvVarGuard
impl UnwindSafe for EnvVarGuard
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more