Skip to content

Commit

Permalink
Create sys::args::Args wrapper in sys_common
Browse files Browse the repository at this point in the history
  • Loading branch information
CDirkx committed Jun 21, 2021
1 parent fffe272 commit f831945
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 7 deletions.
12 changes: 5 additions & 7 deletions library/std/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ use crate::ffi::{OsStr, OsString};
use crate::fmt;
use crate::io;
use crate::path::{Path, PathBuf};
use crate::sys;
use crate::sys::os as os_imp;
use crate::sys_common;

/// Returns the current working directory as a [`PathBuf`].
///
Expand Down Expand Up @@ -705,7 +705,7 @@ pub struct Args {
/// [`env::args_os()`]: args_os
#[stable(feature = "env", since = "1.0.0")]
pub struct ArgsOs {
inner: sys::args::Args,
inner: sys_common::args::Args,
}

/// Returns the arguments that this program was started with (normally passed
Expand Down Expand Up @@ -777,7 +777,7 @@ pub fn args() -> Args {
/// ```
#[stable(feature = "env", since = "1.0.0")]
pub fn args_os() -> ArgsOs {
ArgsOs { inner: sys::args::args() }
ArgsOs { inner: sys_common::args::Args::get() }
}

#[stable(feature = "env_unimpl_send_sync", since = "1.26.0")]
Expand Down Expand Up @@ -817,8 +817,7 @@ 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 {
let args = &AsRef::<[OsString]>::as_ref(&self.inner.inner);
f.debug_struct("Args").field("inner", args).finish()
f.debug_struct("Args").field("inner", &self.inner.inner).finish()
}
}

Expand Down Expand Up @@ -859,8 +858,7 @@ 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 {
let args = &AsRef::<[OsString]>::as_ref(&self.inner);
f.debug_struct("ArgsOs").field("inner", args).finish()
f.debug_struct("ArgsOs").field("inner", &self.inner).finish()
}
}

Expand Down
50 changes: 50 additions & 0 deletions library/std/src/sys_common/args.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use crate::convert::AsRef;
use crate::ffi::OsString;
use crate::fmt;
use crate::sys::args as sys;

pub struct Args(sys::Args);

impl !Send for Args {}
impl !Sync for Args {}

impl Args {
pub fn get() -> Args {
Args(sys::args())
}
}

impl fmt::Debug for Args {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let args = &AsRef::<[OsString]>::as_ref(self);
args.fmt(f)
}
}

impl AsRef<[OsString]> for Args {
fn as_ref(&self) -> &[OsString] {
self.0.as_ref()
}
}

impl Iterator for Args {
type Item = OsString;
fn next(&mut self) -> Option<OsString> {
self.0.next()
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.0.size_hint()
}
}

impl ExactSizeIterator for Args {
fn len(&self) -> usize {
self.0.len()
}
}

impl DoubleEndedIterator for Args {
fn next_back(&mut self) -> Option<OsString> {
self.0.next_back()
}
}
1 change: 1 addition & 0 deletions library/std/src/sys_common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#[cfg(test)]
mod tests;

pub mod args;
pub mod backtrace;
pub mod bytestring;
pub mod condvar;
Expand Down

0 comments on commit f831945

Please sign in to comment.