Unify owned Env
types between platforms · rust-lang/rust@01485c9 (original) (raw)
1
1
`#![allow(fuzzy_provenance_casts)] // FIXME: this module systematically confuses pointers and integers
`
2
2
``
``
3
`+
pub use super::common::Env;
`
3
4
`use crate::collections::HashMap;
`
4
5
`use crate::ffi::{OsStr, OsString};
`
``
6
`+
use crate::io;
`
5
7
`use crate::sync::atomic::{AtomicUsize, Ordering};
`
6
8
`use crate::sync::{Mutex, Once};
`
7
``
`-
use crate::{fmt, io, vec};
`
8
9
``
9
10
`// Specifying linkage/symbol name is solely to ensure a single instance between this crate and its unit tests
`
10
11
`#[cfg_attr(test, linkage = "available_externally")]
`
`@@ -27,61 +28,13 @@ fn create_env_store() -> &'static EnvStore {
`
27
28
`unsafe { &*(ENV.load(Ordering::Relaxed) as *const EnvStore) }
`
28
29
`}
`
29
30
``
30
``
`-
pub struct Env {
`
31
``
`-
iter: vec::IntoIter<(OsString, OsString)>,
`
32
``
`-
}
`
33
``
-
34
``
`-
// FIXME(https://github.com/rust-lang/rust/issues/114583): Remove this when ::fmt matches ::fmt.
`
35
``
`-
pub struct EnvStrDebug<'a> {
`
36
``
`-
slice: &'a [(OsString, OsString)],
`
37
``
`-
}
`
38
``
-
39
``
`-
impl fmt::Debug for EnvStrDebug<'_> {
`
40
``
`-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
`
41
``
`-
let Self { slice } = self;
`
42
``
`-
f.debug_list()
`
43
``
`-
.entries(slice.iter().map(|(a, b)| (a.to_str().unwrap(), b.to_str().unwrap())))
`
44
``
`-
.finish()
`
45
``
`-
}
`
46
``
`-
}
`
47
``
-
48
``
`-
impl Env {
`
49
``
`-
pub fn str_debug(&self) -> impl fmt::Debug + '_ {
`
50
``
`-
let Self { iter } = self;
`
51
``
`-
EnvStrDebug { slice: iter.as_slice() }
`
52
``
`-
}
`
53
``
`-
}
`
54
``
-
55
``
`-
impl fmt::Debug for Env {
`
56
``
`-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
`
57
``
`-
let Self { iter } = self;
`
58
``
`-
f.debug_list().entries(iter.as_slice()).finish()
`
59
``
`-
}
`
60
``
`-
}
`
61
``
-
62
``
`-
impl !Send for Env {}
`
63
``
`-
impl !Sync for Env {}
`
64
``
-
65
``
`-
impl Iterator for Env {
`
66
``
`-
type Item = (OsString, OsString);
`
67
``
`-
fn next(&mut self) -> Option<(OsString, OsString)> {
`
68
``
`-
self.iter.next()
`
69
``
`-
}
`
70
``
`-
fn size_hint(&self) -> (usize, Option) {
`
71
``
`-
self.iter.size_hint()
`
72
``
`-
}
`
73
``
`-
}
`
74
``
-
75
31
`pub fn env() -> Env {
`
76
32
`let clone_to_vec = |map: &HashMap<OsString, OsString>| -> Vec<_> {
`
77
33
` map.iter().map(|(k, v)| (k.clone(), v.clone())).collect()
`
78
34
`};
`
79
35
``
80
``
`-
let iter = get_env_store()
`
81
``
`-
.map(|env| clone_to_vec(&env.lock().unwrap()))
`
82
``
`-
.unwrap_or_default()
`
83
``
`-
.into_iter();
`
84
``
`-
Env { iter }
`
``
36
`+
let env = get_env_store().map(|env| clone_to_vec(&env.lock().unwrap())).unwrap_or_default();
`
``
37
`+
Env::new(env)
`
85
38
`}
`
86
39
``
87
40
`pub fn getenv(k: &OsStr) -> Option {
`