@@ -318,11 +318,6 @@ impl Error for VarError {
318
318
///
319
319
/// # Safety
320
320
///
321
- /// Even though this function is currently not marked as `unsafe`, it needs to
322
- /// be because invoking it can cause undefined behaviour. The function will be
323
- /// marked `unsafe` in a future version of Rust. This is tracked in
324
- /// [rust#27970](https://github.com/rust-lang/rust/issues/27970).
325
- ///
326
321
/// This function is safe to call in a single-threaded program.
327
322
///
328
323
/// In multi-threaded programs, you must ensure that are no other threads
@@ -331,7 +326,7 @@ impl Error for VarError {
331
326
/// how to achieve this, but we strongly suggest not using `set_var` or
332
327
/// `remove_var` in multi-threaded programs at all.
333
328
///
334
- /// Most C libraries, including libc itself do not advertise which functions
329
+ /// Most C libraries, including libc itself, do not advertise which functions
335
330
/// read from the environment. Even functions from the Rust standard library do
336
331
/// that, e.g. for DNS lookups from [`std::net::ToSocketAddrs`].
337
332
///
@@ -353,15 +348,26 @@ impl Error for VarError {
353
348
/// use std::env;
354
349
///
355
350
/// let key = "KEY";
356
- /// env::set_var(key, "VALUE");
351
+ /// unsafe {
352
+ /// env::set_var(key, "VALUE");
353
+ /// }
357
354
/// assert_eq!(env::var(key), Ok("VALUE".to_string()));
358
355
/// ```
356
+ #[ cfg( not( bootstrap) ) ]
357
+ #[ rustc_deprecated_safe_2024]
359
358
#[ stable( feature = "env" , since = "1.0.0" ) ]
360
- pub fn set_var < K : AsRef < OsStr > , V : AsRef < OsStr > > ( key : K , value : V ) {
359
+ pub unsafe fn set_var < K : AsRef < OsStr > , V : AsRef < OsStr > > ( key : K , value : V ) {
361
360
_set_var ( key. as_ref ( ) , value. as_ref ( ) )
362
361
}
363
362
364
- fn _set_var ( key : & OsStr , value : & OsStr ) {
363
+ #[ cfg( bootstrap) ]
364
+ #[ allow( missing_docs) ]
365
+ #[ stable( feature = "env" , since = "1.0.0" ) ]
366
+ pub fn set_var < K : AsRef < OsStr > , V : AsRef < OsStr > > ( key : K , value : V ) {
367
+ unsafe { _set_var ( key. as_ref ( ) , value. as_ref ( ) ) }
368
+ }
369
+
370
+ unsafe fn _set_var ( key : & OsStr , value : & OsStr ) {
365
371
os_imp:: setenv ( key, value) . unwrap_or_else ( |e| {
366
372
panic ! ( "failed to set environment variable `{key:?}` to `{value:?}`: {e}" )
367
373
} )
@@ -371,11 +377,6 @@ fn _set_var(key: &OsStr, value: &OsStr) {
371
377
///
372
378
/// # Safety
373
379
///
374
- /// Even though this function is currently not marked as `unsafe`, it needs to
375
- /// be because invoking it can cause undefined behaviour. The function will be
376
- /// marked `unsafe` in a future version of Rust. This is tracked in
377
- /// [rust#27970](https://github.com/rust-lang/rust/issues/27970).
378
- ///
379
380
/// This function is safe to call in a single-threaded program.
380
381
///
381
382
/// In multi-threaded programs, you must ensure that are no other threads
@@ -384,7 +385,7 @@ fn _set_var(key: &OsStr, value: &OsStr) {
384
385
/// how to achieve this, but we strongly suggest not using `set_var` or
385
386
/// `remove_var` in multi-threaded programs at all.
386
387
///
387
- /// Most C libraries, including libc itself do not advertise which functions
388
+ /// Most C libraries, including libc itself, do not advertise which functions
388
389
/// read from the environment. Even functions from the Rust standard library do
389
390
/// that, e.g. for DNS lookups from [`std::net::ToSocketAddrs`].
390
391
///
@@ -403,22 +404,35 @@ fn _set_var(key: &OsStr, value: &OsStr) {
403
404
///
404
405
/// # Examples
405
406
///
406
- /// ```
407
+ /// ```no_run
407
408
/// use std::env;
408
409
///
409
410
/// let key = "KEY";
410
- /// env::set_var(key, "VALUE");
411
+ /// unsafe {
412
+ /// env::set_var(key, "VALUE");
413
+ /// }
411
414
/// assert_eq!(env::var(key), Ok("VALUE".to_string()));
412
415
///
413
- /// env::remove_var(key);
416
+ /// unsafe {
417
+ /// env::remove_var(key);
418
+ /// }
414
419
/// assert!(env::var(key).is_err());
415
420
/// ```
421
+ #[ cfg( not( bootstrap) ) ]
422
+ #[ rustc_deprecated_safe_2024]
416
423
#[ stable( feature = "env" , since = "1.0.0" ) ]
417
- pub fn remove_var < K : AsRef < OsStr > > ( key : K ) {
424
+ pub unsafe fn remove_var < K : AsRef < OsStr > > ( key : K ) {
418
425
_remove_var ( key. as_ref ( ) )
419
426
}
420
427
421
- fn _remove_var ( key : & OsStr ) {
428
+ #[ cfg( bootstrap) ]
429
+ #[ allow( missing_docs) ]
430
+ #[ stable( feature = "env" , since = "1.0.0" ) ]
431
+ pub fn remove_var < K : AsRef < OsStr > > ( key : K ) {
432
+ unsafe { _remove_var ( key. as_ref ( ) ) }
433
+ }
434
+
435
+ unsafe fn _remove_var ( key : & OsStr ) {
422
436
os_imp:: unsetenv ( key)
423
437
. unwrap_or_else ( |e| panic ! ( "failed to remove environment variable `{key:?}`: {e}" ) )
424
438
}
0 commit comments