Skip to content

Commit 2628780

Browse files
authored
Rollup merge of rust-lang#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 ec0fb72 + 216edcb commit 2628780

File tree

8 files changed

+91
-10
lines changed

8 files changed

+91
-10
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).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

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

115+
impl fmt::Debug for Env {
116+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
117+
let Self { iter } = self;
118+
f.debug_list().entries(iter.as_slice()).finish()
119+
}
120+
}
121+
115122
impl !Send for Env {}
116123
impl !Sync for Env {}
117124

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

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

88+
impl fmt::Debug for Env {
89+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
90+
let Self { iter } = self;
91+
f.debug_list().entries(iter.as_slice()).finish()
92+
}
93+
}
94+
8895
impl !Send for Env {}
8996
impl !Sync for Env {}
9097

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

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

498+
impl fmt::Debug for Env {
499+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
500+
let Self { iter } = self;
501+
f.debug_list().entries(iter.as_slice()).finish()
502+
}
503+
}
504+
498505
impl !Send for Env {}
499506
impl !Sync for Env {}
500507

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

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

6666
pub struct Env(!);
6767

68+
impl fmt::Debug for Env {
69+
fn fmt(&self, _: &mut fmt::Formatter<'_>) -> fmt::Result {
70+
let Self(inner) = self;
71+
match *inner {}
72+
}
73+
}
74+
6875
impl Iterator for Env {
6976
type Item = (OsString, OsString);
7077
fn next(&mut self) -> Option<(OsString, OsString)> {
71-
self.0
78+
let Self(inner) = self;
79+
match *inner {}
7280
}
7381
}
7482

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

+8
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,18 @@ 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+
impl fmt::Debug for Env {
151+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
152+
let Self { iter } = self;
153+
f.debug_list().entries(iter.as_slice()).finish()
154+
}
155+
}
156+
149157
impl !Send for Env {}
150158
impl !Sync for Env {}
151159

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

+25-5
Original file line numberDiff line numberDiff line change
@@ -85,25 +85,45 @@ 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+
impl fmt::Debug for Env {
92+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
93+
let Self { base: _, iter } = self;
94+
f.debug_list().entries(iter.clone()).finish()
95+
}
8996
}
9097

9198
impl Iterator for Env {
9299
type Item = (OsString, OsString);
93100

94101
fn next(&mut self) -> Option<(OsString, OsString)> {
102+
let Self { base: _, iter } = self;
103+
iter.next()
104+
}
105+
}
106+
107+
#[derive(Clone)]
108+
struct EnvIterator(c::LPWCH);
109+
110+
impl Iterator for EnvIterator {
111+
type Item = (OsString, OsString);
112+
113+
fn next(&mut self) -> Option<(OsString, OsString)> {
114+
let Self(cur) = self;
95115
loop {
96116
unsafe {
97-
if *self.cur == 0 {
117+
if **cur == 0 {
98118
return None;
99119
}
100-
let p = self.cur as *const u16;
120+
let p = *cur as *const u16;
101121
let mut len = 0;
102122
while *p.add(len) != 0 {
103123
len += 1;
104124
}
105125
let s = slice::from_raw_parts(p, len);
106-
self.cur = self.cur.add(len + 1);
126+
*cur = cur.add(len + 1);
107127

108128
// Windows allows environment variables to start with an equals
109129
// symbol (in any other position, this is the separator between
@@ -137,7 +157,7 @@ pub fn env() -> Env {
137157
if ch.is_null() {
138158
panic!("failure getting env string from OS: {}", io::Error::last_os_error());
139159
}
140-
Env { base: ch, cur: ch }
160+
Env { base: ch, iter: EnvIterator(ch) }
141161
}
142162
}
143163

0 commit comments

Comments
 (0)