From 275f9a04af6191e3aee3852a5a1713130f635164 Mon Sep 17 00:00:00 2001 From: Stepan Koltsov Date: Wed, 21 Jun 2017 15:40:45 +0300 Subject: Better Debug for Args and ArgsOs Display actual args instead of two dots. --- src/libstd/sys/redox/args.rs | 6 ++++++ src/libstd/sys/unix/args.rs | 6 ++++++ src/libstd/sys/windows/args.rs | 31 +++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+) (limited to 'src/libstd/sys') diff --git a/src/libstd/sys/redox/args.rs b/src/libstd/sys/redox/args.rs index 212895d7b76..6e44ad705fe 100644 --- a/src/libstd/sys/redox/args.rs +++ b/src/libstd/sys/redox/args.rs @@ -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 { self.iter.next() } diff --git a/src/libstd/sys/unix/args.rs b/src/libstd/sys/unix/args.rs index 6e35a472792..bbdcb5d3616 100644 --- a/src/libstd/sys/unix/args.rs +++ b/src/libstd/sys/unix/args.rs @@ -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 { self.iter.next() } diff --git a/src/libstd/sys/windows/args.rs b/src/libstd/sys/windows/args.rs index aa61f9adb82..4784633edc1 100644 --- a/src/libstd/sys/windows/args.rs +++ b/src/libstd/sys/windows/args.rs @@ -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) { } @@ -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; } -- cgit 1.4.1-3-g733a5