std::env::set_var safely
bendn 2025-01-13
parent 0963314 · commit c79c0bd
-rw-r--r--Cargo.lock2
-rw-r--r--Cargo.toml4
-rw-r--r--README.md2
-rw-r--r--src/lib.rs1
4 files changed, 5 insertions, 4 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 4bb66ed..1105131 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -4,7 +4,7 @@ version = 3
[[package]]
name = "env"
-version = "1.0.0"
+version = "1.0.1"
dependencies = [
"num_threads",
]
diff --git a/Cargo.toml b/Cargo.toml
index 708da8f..27d01b4 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -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"
diff --git a/README.md b/README.md
index 964d1a3..d42b89b 100644
--- a/README.md
+++ b/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
diff --git a/src/lib.rs b/src/lib.rs
index ce09c3a..5eb2715 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -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) })
}