std::env::set_var safely
| -rw-r--r-- | Cargo.lock | 2 | ||||
| -rw-r--r-- | Cargo.toml | 4 | ||||
| -rw-r--r-- | README.md | 2 | ||||
| -rw-r--r-- | src/lib.rs | 1 |
4 files changed, 5 insertions, 4 deletions
@@ -4,7 +4,7 @@ version = 3 [[package]] name = "env" -version = "1.0.0" +version = "1.0.1" dependencies = [ "num_threads", ] @@ -1,10 +1,10 @@ cargo-features = ["edition2024"] [package] name = "env" -version = "1.0.0" +version = "1.0.1" edition = "2024" license = "MIT" -description = "std::env::{`set_var`, `remove_var`} safely" +description = "std::env::{set_var, remove_var} safely" keywords = ["environment", "safe"] categories = ["config", "os"] readme = "README.md" @@ -8,6 +8,6 @@ Provides a safe interface for <code>[std::env](http://doc.rust-lang.org/std/env) Since [#124636](https://github.com/rust-lang/rust/pull/124636), `std::env::set_var` and `std::env::remove_var}` have become unsafe, due to their being unsafe when in a multi-threaded unix context[^1]. -This crate wraps these functions, checking if these conditions are met at runtime, such that these functions are safe to call. +This crate wraps these functions, adding runtime checks to ensure thread safety on unix systems, making them safe again. [^1]: https://github.com/rust-lang/rust/issues/27970 @@ -43,6 +43,7 @@ const SAFE: bool = matches!( /// ``` #[must_use = "this function may not always run"] pub fn set_var<K: AsRef<OsStr>, V: AsRef<OsStr>>(key: K, value: V) -> Option<()> { + // SAFETY: this function is safe to call in single threaded environments or on good OS's. (SAFE || num_threads::is_single_threaded() == Some(true)) .then(|| unsafe { std::env::set_var(key, value) }) } |