Skip to content

Better Debug for Args and ArgsOs #42798

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions src/libstd/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,9 @@ impl DoubleEndedIterator for Args {
#[stable(feature = "std_debug", since = "1.16.0")]
impl fmt::Debug for Args {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.pad("Args { .. }")
f.debug_struct("Args")
.field("inner", &self.inner.inner.inner_debug())
.finish()
}
}

Expand All @@ -737,7 +739,9 @@ impl DoubleEndedIterator for ArgsOs {
#[stable(feature = "std_debug", since = "1.16.0")]
impl fmt::Debug for ArgsOs {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.pad("ArgsOs { .. }")
f.debug_struct("ArgsOs")
.field("inner", &self.inner.inner_debug())
.finish()
}
}

Expand Down Expand Up @@ -1085,4 +1089,14 @@ mod tests {
r#""c:\te;st";c:\"#));
assert!(join_paths([r#"c:\te"st"#].iter().cloned()).is_err());
}

#[test]
fn args_debug() {
assert_eq!(
format!("Args {{ inner: {:?} }}", args().collect::<Vec<_>>()),
format!("{:?}", args()));
assert_eq!(
format!("ArgsOs {{ inner: {:?} }}", args_os().collect::<Vec<_>>()),
format!("{:?}", args_os()));
}
}
6 changes: 6 additions & 0 deletions src/libstd/sys/redox/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ pub struct Args {
_dont_send_or_sync_me: PhantomData<*mut ()>,
}

impl Args {
pub fn inner_debug(&self) -> &[OsString] {
self.iter.as_slice()
}
}

impl Iterator for Args {
type Item = OsString;
fn next(&mut self) -> Option<OsString> { self.iter.next() }
Expand Down
6 changes: 6 additions & 0 deletions src/libstd/sys/unix/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ pub struct Args {
_dont_send_or_sync_me: PhantomData<*mut ()>,
}

impl Args {
pub fn inner_debug(&self) -> &[OsString] {
self.iter.as_slice()
}
}

impl Iterator for Args {
type Item = OsString;
fn next(&mut self) -> Option<OsString> { self.iter.next() }
Expand Down
31 changes: 31 additions & 0 deletions src/libstd/sys/windows/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use slice;
use ops::Range;
use ffi::OsString;
use libc::{c_int, c_void};
use fmt;

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

Expand All @@ -39,6 +40,36 @@ pub struct Args {
cur: *mut *mut u16,
}

pub struct ArgsInnerDebug<'a> {
args: &'a Args,
}

impl<'a> fmt::Debug for ArgsInnerDebug<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str("[")?;
let mut first = true;
for i in self.args.range.clone() {
if !first {
f.write_str(", ")?;
}
first = false;

// Here we do allocation which could be avoided.
fmt::Debug::fmt(&unsafe { os_string_from_ptr(*self.args.cur.offset(i)) }, f)?;
}
f.write_str("]")?;
Ok(())
}
}

impl Args {
pub fn inner_debug(&self) -> ArgsInnerDebug {
ArgsInnerDebug {
args: self
}
}
}

unsafe fn os_string_from_ptr(ptr: *mut u16) -> OsString {
let mut len = 0;
while *ptr.offset(len) != 0 { len += 1; }
Expand Down