-
Notifications
You must be signed in to change notification settings - Fork 9
Description
Hi! 👋 I’ve been using temp_env and ran into a limitation with its current closure-based API design.
Right now, temp_env requires the temporary environment setup and teardown to happen inside a closure. While this works fine in most sync contexts, it becomes restrictive when writing async code, benchmarks, or long-running test setups.
For example, in a benchmark, the closure forces you to nest logic in ways that add overhead or don’t fit naturally with async code. The async feature partially helps, but still imposes execution control and can’t cover some use cases.
Proposed Improvement
Add an explicit RAII-style API that returns a temporary environment "lock" object whose Drop impl cleans up automatically.
This way, users can manage the scope themselves instead of being forced into a closure.
Example:
let _env = temp_env::set_var("KEY", "VALUE");
// run code here — async or sync
// environment automatically restored when `_env` goes out of scopeThe existing closure API can remain for convenience and backward compatibility:
temp_env::with_var("KEY", "VALUE", || {
// still works
});Benefits
- Works seamlessly in sync, async, and benchmarking contexts
- Removes need for
asyncfeature gating - Avoids closure overhead and nesting
- Keeps cleanup deterministic through RAII semantics
If this sounds reasonable, I’d be happy to open a PR adding the new API and keeping the current one intact.
Thanks for maintaining this crate — it’s been really helpful! 🙏