Skip to content

Commit 8a997b1

Browse files
authored
Rollup merge of #114132 - tamird:better-env-debug-impls, r=Amanieu
Better Debug for Vars and VarsOs Display actual vars instead of two dots. The same was done for Args and ArgsOs in 275f9a0.
2 parents 9aea966 + 35c0c03 commit 8a997b1

File tree

9 files changed

+256
-12
lines changed

9 files changed

+256
-12
lines changed

library/std/src/env.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,8 @@ impl Iterator for Vars {
178178
#[stable(feature = "std_debug", since = "1.16.0")]
179179
impl fmt::Debug for Vars {
180180
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
181-
f.debug_struct("Vars").finish_non_exhaustive()
181+
let Self { inner: VarsOs { inner } } = self;
182+
f.debug_struct("Vars").field("inner", &inner.str_debug()).finish()
182183
}
183184
}
184185

@@ -196,7 +197,8 @@ impl Iterator for VarsOs {
196197
#[stable(feature = "std_debug", since = "1.16.0")]
197198
impl fmt::Debug for VarsOs {
198199
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
199-
f.debug_struct("VarOs").finish_non_exhaustive()
200+
let Self { inner } = self;
201+
f.debug_struct("VarsOs").field("inner", inner).finish()
200202
}
201203
}
202204

@@ -829,7 +831,8 @@ impl DoubleEndedIterator for Args {
829831
#[stable(feature = "std_debug", since = "1.16.0")]
830832
impl fmt::Debug for Args {
831833
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
832-
f.debug_struct("Args").field("inner", &self.inner.inner).finish()
834+
let Self { inner: ArgsOs { inner } } = self;
835+
f.debug_struct("Args").field("inner", inner).finish()
833836
}
834837
}
835838

@@ -870,7 +873,8 @@ impl DoubleEndedIterator for ArgsOs {
870873
#[stable(feature = "std_debug", since = "1.16.0")]
871874
impl fmt::Debug for ArgsOs {
872875
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
873-
f.debug_struct("ArgsOs").field("inner", &self.inner).finish()
876+
let Self { inner } = self;
877+
f.debug_struct("ArgsOs").field("inner", inner).finish()
874878
}
875879
}
876880

library/std/src/env/tests.rs

+20
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,28 @@ fn args_debug() {
9595
format!("Args {{ inner: {:?} }}", args().collect::<Vec<_>>()),
9696
format!("{:?}", args())
9797
);
98+
}
99+
100+
#[test]
101+
fn args_os_debug() {
98102
assert_eq!(
99103
format!("ArgsOs {{ inner: {:?} }}", args_os().collect::<Vec<_>>()),
100104
format!("{:?}", args_os())
101105
);
102106
}
107+
108+
#[test]
109+
fn vars_debug() {
110+
assert_eq!(
111+
format!("Vars {{ inner: {:?} }}", vars().collect::<Vec<_>>()),
112+
format!("{:?}", vars())
113+
);
114+
}
115+
116+
#[test]
117+
fn vars_os_debug() {
118+
assert_eq!(
119+
format!("VarsOs {{ inner: {:?} }}", vars_os().collect::<Vec<_>>()),
120+
format!("{:?}", vars_os())
121+
);
122+
}

library/std/src/sys/hermit/os.rs

+28
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,34 @@ pub struct Env {
112112
iter: vec::IntoIter<(OsString, OsString)>,
113113
}
114114

115+
// FIXME(https://github.com/rust-lang/rust/issues/114583): Remove this when <OsStr as Debug>::fmt matches <str as Debug>::fmt.
116+
pub struct EnvStrDebug<'a> {
117+
slice: &'a [(OsString, OsString)],
118+
}
119+
120+
impl fmt::Debug for EnvStrDebug<'_> {
121+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
122+
let Self { slice } = self;
123+
f.debug_list()
124+
.entries(slice.iter().map(|(a, b)| (a.to_str().unwrap(), b.to_str().unwrap())))
125+
.finish()
126+
}
127+
}
128+
129+
impl Env {
130+
pub fn str_debug(&self) -> impl fmt::Debug + '_ {
131+
let Self { iter } = self;
132+
EnvStrDebug { slice: iter.as_slice() }
133+
}
134+
}
135+
136+
impl fmt::Debug for Env {
137+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
138+
let Self { iter } = self;
139+
f.debug_list().entries(iter.as_slice()).finish()
140+
}
141+
}
142+
115143
impl !Send for Env {}
116144
impl !Sync for Env {}
117145

library/std/src/sys/sgx/os.rs

+49-2
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,61 @@ fn create_env_store() -> &'static EnvStore {
9696
unsafe { &*(ENV.load(Ordering::Relaxed) as *const EnvStore) }
9797
}
9898

99-
pub type Env = vec::IntoIter<(OsString, OsString)>;
99+
pub struct Env {
100+
iter: vec::IntoIter<(OsString, OsString)>,
101+
}
102+
103+
// FIXME(https://github.com/rust-lang/rust/issues/114583): Remove this when <OsStr as Debug>::fmt matches <str as Debug>::fmt.
104+
pub struct EnvStrDebug<'a> {
105+
slice: &'a [(OsString, OsString)],
106+
}
107+
108+
impl fmt::Debug for EnvStrDebug<'_> {
109+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
110+
let Self { slice } = self;
111+
f.debug_list()
112+
.entries(slice.iter().map(|(a, b)| (a.to_str().unwrap(), b.to_str().unwrap())))
113+
.finish()
114+
}
115+
}
116+
117+
impl Env {
118+
pub fn str_debug(&self) -> impl fmt::Debug + '_ {
119+
let Self { iter } = self;
120+
EnvStrDebug { slice: iter.as_slice() }
121+
}
122+
}
123+
124+
impl fmt::Debug for Env {
125+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
126+
let Self { iter } = self;
127+
f.debug_list().entries(iter.as_slice()).finish()
128+
}
129+
}
130+
131+
impl !Send for Env {}
132+
impl !Sync for Env {}
133+
134+
impl Iterator for Env {
135+
type Item = (OsString, OsString);
136+
fn next(&mut self) -> Option<(OsString, OsString)> {
137+
self.iter.next()
138+
}
139+
fn size_hint(&self) -> (usize, Option<usize>) {
140+
self.iter.size_hint()
141+
}
142+
}
100143

101144
pub fn env() -> Env {
102145
let clone_to_vec = |map: &HashMap<OsString, OsString>| -> Vec<_> {
103146
map.iter().map(|(k, v)| (k.clone(), v.clone())).collect()
104147
};
105148

106-
get_env_store().map(|env| clone_to_vec(&env.lock().unwrap())).unwrap_or_default().into_iter()
149+
let iter = get_env_store()
150+
.map(|env| clone_to_vec(&env.lock().unwrap()))
151+
.unwrap_or_default()
152+
.into_iter();
153+
Env { iter }
107154
}
108155

109156
pub fn getenv(k: &OsStr) -> Option<OsString> {

library/std/src/sys/solid/os.rs

+28
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,34 @@ pub struct Env {
8585
iter: vec::IntoIter<(OsString, OsString)>,
8686
}
8787

88+
// FIXME(https://github.com/rust-lang/rust/issues/114583): Remove this when <OsStr as Debug>::fmt matches <str as Debug>::fmt.
89+
pub struct EnvStrDebug<'a> {
90+
slice: &'a [(OsString, OsString)],
91+
}
92+
93+
impl fmt::Debug for EnvStrDebug<'_> {
94+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
95+
let Self { slice } = self;
96+
f.debug_list()
97+
.entries(slice.iter().map(|(a, b)| (a.to_str().unwrap(), b.to_str().unwrap())))
98+
.finish()
99+
}
100+
}
101+
102+
impl Env {
103+
pub fn str_debug(&self) -> impl fmt::Debug + '_ {
104+
let Self { iter } = self;
105+
EnvStrDebug { slice: iter.as_slice() }
106+
}
107+
}
108+
109+
impl fmt::Debug for Env {
110+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
111+
let Self { iter } = self;
112+
f.debug_list().entries(iter.as_slice()).finish()
113+
}
114+
}
115+
88116
impl !Send for Env {}
89117
impl !Sync for Env {}
90118

library/std/src/sys/unix/os.rs

+28
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,34 @@ pub struct Env {
495495
iter: vec::IntoIter<(OsString, OsString)>,
496496
}
497497

498+
// FIXME(https://github.com/rust-lang/rust/issues/114583): Remove this when <OsStr as Debug>::fmt matches <str as Debug>::fmt.
499+
pub struct EnvStrDebug<'a> {
500+
slice: &'a [(OsString, OsString)],
501+
}
502+
503+
impl fmt::Debug for EnvStrDebug<'_> {
504+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
505+
let Self { slice } = self;
506+
f.debug_list()
507+
.entries(slice.iter().map(|(a, b)| (a.to_str().unwrap(), b.to_str().unwrap())))
508+
.finish()
509+
}
510+
}
511+
512+
impl Env {
513+
pub fn str_debug(&self) -> impl fmt::Debug + '_ {
514+
let Self { iter } = self;
515+
EnvStrDebug { slice: iter.as_slice() }
516+
}
517+
}
518+
519+
impl fmt::Debug for Env {
520+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
521+
let Self { iter } = self;
522+
f.debug_list().entries(iter.as_slice()).finish()
523+
}
524+
}
525+
498526
impl !Send for Env {}
499527
impl !Sync for Env {}
500528

library/std/src/sys/unsupported/os.rs

+17-1
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,26 @@ pub fn current_exe() -> io::Result<PathBuf> {
6565

6666
pub struct Env(!);
6767

68+
impl Env {
69+
// FIXME(https://github.com/rust-lang/rust/issues/114583): Remove this when <OsStr as Debug>::fmt matches <str as Debug>::fmt.
70+
pub fn str_debug(&self) -> impl fmt::Debug + '_ {
71+
let Self(inner) = self;
72+
match *inner {}
73+
}
74+
}
75+
76+
impl fmt::Debug for Env {
77+
fn fmt(&self, _: &mut fmt::Formatter<'_>) -> fmt::Result {
78+
let Self(inner) = self;
79+
match *inner {}
80+
}
81+
}
82+
6883
impl Iterator for Env {
6984
type Item = (OsString, OsString);
7085
fn next(&mut self) -> Option<(OsString, OsString)> {
71-
self.0
86+
let Self(inner) = self;
87+
match *inner {}
7288
}
7389
}
7490

library/std/src/sys/wasi/os.rs

+29
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,39 @@ impl StdError for JoinPathsError {
142142
pub fn current_exe() -> io::Result<PathBuf> {
143143
unsupported()
144144
}
145+
145146
pub struct Env {
146147
iter: vec::IntoIter<(OsString, OsString)>,
147148
}
148149

150+
// FIXME(https://github.com/rust-lang/rust/issues/114583): Remove this when <OsStr as Debug>::fmt matches <str as Debug>::fmt.
151+
pub struct EnvStrDebug<'a> {
152+
slice: &'a [(OsString, OsString)],
153+
}
154+
155+
impl fmt::Debug for EnvStrDebug<'_> {
156+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
157+
let Self { slice } = self;
158+
f.debug_list()
159+
.entries(slice.iter().map(|(a, b)| (a.to_str().unwrap(), b.to_str().unwrap())))
160+
.finish()
161+
}
162+
}
163+
164+
impl Env {
165+
pub fn str_debug(&self) -> impl fmt::Debug + '_ {
166+
let Self { iter } = self;
167+
EnvStrDebug { slice: iter.as_slice() }
168+
}
169+
}
170+
171+
impl fmt::Debug for Env {
172+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
173+
let Self { iter } = self;
174+
f.debug_list().entries(iter.as_slice()).finish()
175+
}
176+
}
177+
149178
impl !Send for Env {}
150179
impl !Sync for Env {}
151180

library/std/src/sys/windows/os.rs

+49-5
Original file line numberDiff line numberDiff line change
@@ -85,25 +85,69 @@ pub fn error_string(mut errnum: i32) -> String {
8585

8686
pub struct Env {
8787
base: c::LPWCH,
88-
cur: c::LPWCH,
88+
iter: EnvIterator,
89+
}
90+
91+
// FIXME(https://github.com/rust-lang/rust/issues/114583): Remove this when <OsStr as Debug>::fmt matches <str as Debug>::fmt.
92+
pub struct EnvStrDebug<'a> {
93+
iter: &'a EnvIterator,
94+
}
95+
96+
impl fmt::Debug for EnvStrDebug<'_> {
97+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
98+
let Self { iter } = self;
99+
let iter: EnvIterator = (*iter).clone();
100+
let mut list = f.debug_list();
101+
for (a, b) in iter {
102+
list.entry(&(a.to_str().unwrap(), b.to_str().unwrap()));
103+
}
104+
list.finish()
105+
}
106+
}
107+
108+
impl Env {
109+
pub fn str_debug(&self) -> impl fmt::Debug + '_ {
110+
let Self { base: _, iter } = self;
111+
EnvStrDebug { iter }
112+
}
113+
}
114+
115+
impl fmt::Debug for Env {
116+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
117+
let Self { base: _, iter } = self;
118+
f.debug_list().entries(iter.clone()).finish()
119+
}
89120
}
90121

91122
impl Iterator for Env {
92123
type Item = (OsString, OsString);
93124

94125
fn next(&mut self) -> Option<(OsString, OsString)> {
126+
let Self { base: _, iter } = self;
127+
iter.next()
128+
}
129+
}
130+
131+
#[derive(Clone)]
132+
struct EnvIterator(c::LPWCH);
133+
134+
impl Iterator for EnvIterator {
135+
type Item = (OsString, OsString);
136+
137+
fn next(&mut self) -> Option<(OsString, OsString)> {
138+
let Self(cur) = self;
95139
loop {
96140
unsafe {
97-
if *self.cur == 0 {
141+
if **cur == 0 {
98142
return None;
99143
}
100-
let p = self.cur as *const u16;
144+
let p = *cur as *const u16;
101145
let mut len = 0;
102146
while *p.add(len) != 0 {
103147
len += 1;
104148
}
105149
let s = slice::from_raw_parts(p, len);
106-
self.cur = self.cur.add(len + 1);
150+
*cur = cur.add(len + 1);
107151

108152
// Windows allows environment variables to start with an equals
109153
// symbol (in any other position, this is the separator between
@@ -137,7 +181,7 @@ pub fn env() -> Env {
137181
if ch.is_null() {
138182
panic!("failure getting env string from OS: {}", io::Error::last_os_error());
139183
}
140-
Env { base: ch, cur: ch }
184+
Env { base: ch, iter: EnvIterator(ch) }
141185
}
142186
}
143187

0 commit comments

Comments
 (0)