Skip to content

Commit 275f9a0

Browse files
committed
Better Debug for Args and ArgsOs
Display actual args instead of two dots.
1 parent 29bce6e commit 275f9a0

File tree

4 files changed

+59
-2
lines changed

4 files changed

+59
-2
lines changed

Diff for: src/libstd/env.rs

+16-2
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,9 @@ impl DoubleEndedIterator for Args {
712712
#[stable(feature = "std_debug", since = "1.16.0")]
713713
impl fmt::Debug for Args {
714714
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
715-
f.pad("Args { .. }")
715+
f.debug_struct("Args")
716+
.field("inner", &self.inner.inner.inner_debug())
717+
.finish()
716718
}
717719
}
718720

@@ -737,7 +739,9 @@ impl DoubleEndedIterator for ArgsOs {
737739
#[stable(feature = "std_debug", since = "1.16.0")]
738740
impl fmt::Debug for ArgsOs {
739741
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
740-
f.pad("ArgsOs { .. }")
742+
f.debug_struct("ArgsOs")
743+
.field("inner", &self.inner.inner_debug())
744+
.finish()
741745
}
742746
}
743747

@@ -1085,4 +1089,14 @@ mod tests {
10851089
r#""c:\te;st";c:\"#));
10861090
assert!(join_paths([r#"c:\te"st"#].iter().cloned()).is_err());
10871091
}
1092+
1093+
#[test]
1094+
fn args_debug() {
1095+
assert_eq!(
1096+
format!("Args {{ inner: {:?} }}", args().collect::<Vec<_>>()),
1097+
format!("{:?}", args()));
1098+
assert_eq!(
1099+
format!("ArgsOs {{ inner: {:?} }}", args_os().collect::<Vec<_>>()),
1100+
format!("{:?}", args_os()));
10881101
}
1102+
}

Diff for: src/libstd/sys/redox/args.rs

+6
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ pub struct Args {
3535
_dont_send_or_sync_me: PhantomData<*mut ()>,
3636
}
3737

38+
impl Args {
39+
pub fn inner_debug(&self) -> &[OsString] {
40+
self.iter.as_slice()
41+
}
42+
}
43+
3844
impl Iterator for Args {
3945
type Item = OsString;
4046
fn next(&mut self) -> Option<OsString> { self.iter.next() }

Diff for: src/libstd/sys/unix/args.rs

+6
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ pub struct Args {
3535
_dont_send_or_sync_me: PhantomData<*mut ()>,
3636
}
3737

38+
impl Args {
39+
pub fn inner_debug(&self) -> &[OsString] {
40+
self.iter.as_slice()
41+
}
42+
}
43+
3844
impl Iterator for Args {
3945
type Item = OsString;
4046
fn next(&mut self) -> Option<OsString> { self.iter.next() }

Diff for: src/libstd/sys/windows/args.rs

+31
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use slice;
1616
use ops::Range;
1717
use ffi::OsString;
1818
use libc::{c_int, c_void};
19+
use fmt;
1920

2021
pub unsafe fn init(_argc: isize, _argv: *const *const u8) { }
2122

@@ -39,6 +40,36 @@ pub struct Args {
3940
cur: *mut *mut u16,
4041
}
4142

43+
pub struct ArgsInnerDebug<'a> {
44+
args: &'a Args,
45+
}
46+
47+
impl<'a> fmt::Debug for ArgsInnerDebug<'a> {
48+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
49+
f.write_str("[")?;
50+
let mut first = true;
51+
for i in self.args.range.clone() {
52+
if !first {
53+
f.write_str(", ")?;
54+
}
55+
first = false;
56+
57+
// Here we do allocation which could be avoided.
58+
fmt::Debug::fmt(&unsafe { os_string_from_ptr(*self.args.cur.offset(i)) }, f)?;
59+
}
60+
f.write_str("]")?;
61+
Ok(())
62+
}
63+
}
64+
65+
impl Args {
66+
pub fn inner_debug(&self) -> ArgsInnerDebug {
67+
ArgsInnerDebug {
68+
args: self
69+
}
70+
}
71+
}
72+
4273
unsafe fn os_string_from_ptr(ptr: *mut u16) -> OsString {
4374
let mut len = 0;
4475
while *ptr.offset(len) != 0 { len += 1; }

0 commit comments

Comments
 (0)