Skip to content

Commit

Permalink
std: Directly call unsafe {un,}setenv in env
Browse files Browse the repository at this point in the history
  • Loading branch information
workingjubilee committed Jul 15, 2024
1 parent 8c75111 commit 91b7331
Showing 1 changed file with 4 additions and 11 deletions.
15 changes: 4 additions & 11 deletions std/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
//! and those without will return a [`String`].
#![stable(feature = "env", since = "1.0.0")]
#![allow(unsafe_op_in_unsafe_fn)]

#[cfg(test)]
mod tests;
Expand Down Expand Up @@ -367,11 +366,8 @@ impl Error for VarError {
#[rustc_deprecated_safe_2024]
#[stable(feature = "env", since = "1.0.0")]
pub unsafe fn set_var<K: AsRef<OsStr>, V: AsRef<OsStr>>(key: K, value: V) {
_set_var(key.as_ref(), value.as_ref())
}

unsafe fn _set_var(key: &OsStr, value: &OsStr) {
os_imp::setenv(key, value).unwrap_or_else(|e| {
let (key, value) = (key.as_ref(), value.as_ref());
unsafe { os_imp::setenv(key, value) }.unwrap_or_else(|e| {
panic!("failed to set environment variable `{key:?}` to `{value:?}`: {e}")
})
}
Expand Down Expand Up @@ -434,11 +430,8 @@ unsafe fn _set_var(key: &OsStr, value: &OsStr) {
#[rustc_deprecated_safe_2024]
#[stable(feature = "env", since = "1.0.0")]
pub unsafe fn remove_var<K: AsRef<OsStr>>(key: K) {
_remove_var(key.as_ref())
}

unsafe fn _remove_var(key: &OsStr) {
os_imp::unsetenv(key)
let key = key.as_ref();
unsafe { os_imp::unsetenv(key) }
.unwrap_or_else(|e| panic!("failed to remove environment variable `{key:?}`: {e}"))
}

Expand Down

0 comments on commit 91b7331

Please sign in to comment.