Skip to content
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

Extract sys::args::Args implementation to sys_common #84503

Closed
wants to merge 2 commits into from
Closed
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
6 changes: 3 additions & 3 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
105 changes: 25 additions & 80 deletions library/std/src/sys/hermit/args.rs
Original file line number Diff line number Diff line change
@@ -1,94 +1,39 @@
use crate::ffi::OsString;
use crate::fmt;
use crate::vec;
use crate::ffi::{CStr, OsString};
use crate::os::unix::ffi::OsStringExt;
use crate::ptr;
use crate::sys_common::mutex::StaticMutex;
use crate::vec::IntoIter;

static mut ARGC: isize = 0;
static mut ARGV: *const *const u8 = ptr::null();
static LOCK: StaticMutex = StaticMutex::new();

/// One-time global initialization.
pub unsafe fn init(argc: isize, argv: *const *const u8) {
imp::init(argc, argv)
let _guard = LOCK.lock();
ARGC = argc;
ARGV = argv;
}

/// One-time global cleanup.
pub unsafe fn cleanup() {
imp::cleanup()
let _guard = LOCK.lock();
ARGC = 0;
ARGV = ptr::null();
}

/// Returns the command line arguments
pub fn args() -> Args {
imp::args()
}

pub struct Args {
iter: vec::IntoIter<OsString>,
}

impl fmt::Debug for Args {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.iter.as_slice().fmt(f)
}
}

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

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

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

impl DoubleEndedIterator for Args {
fn next_back(&mut self) -> Option<OsString> {
self.iter.next_back()
}
}

mod imp {
use super::Args;
use crate::ffi::{CStr, OsString};
use crate::os::unix::ffi::OsStringExt;
use crate::ptr;

use crate::sys_common::mutex::StaticMutex;

static mut ARGC: isize = 0;
static mut ARGV: *const *const u8 = ptr::null();
static LOCK: StaticMutex = StaticMutex::new();

pub unsafe fn init(argc: isize, argv: *const *const u8) {
unsafe {
let _guard = LOCK.lock();
ARGC = argc;
ARGV = argv;
}

pub unsafe fn cleanup() {
let _guard = LOCK.lock();
ARGC = 0;
ARGV = ptr::null();
}

pub fn args() -> Args {
Args { iter: clone().into_iter() }
}

fn clone() -> Vec<OsString> {
unsafe {
let _guard = LOCK.lock();
(0..ARGC)
.map(|i| {
let cstr = CStr::from_ptr(*ARGV.offset(i) as *const i8);
OsStringExt::from_vec(cstr.to_bytes().to_vec())
})
.collect()
}
(0..ARGC)
.map(|i| {
let cstr = CStr::from_ptr(*ARGV.offset(i) as *const i8);
OsStringExt::from_vec(cstr.to_bytes().to_vec())
})
.collect::<Vec<_>>()
.into_iter()
}
}

pub type Args = IntoIter<OsString>;
35 changes: 3 additions & 32 deletions library/std/src/sys/sgx/args.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use super::abi::usercalls::{alloc, raw::ByteBuffer};
use crate::ffi::OsString;
use crate::fmt;
use crate::slice;
use crate::sync::atomic::{AtomicUsize, Ordering};
use crate::sys::os_str::Buf;
use crate::sys_common::FromInner;
use crate::vec::IntoIter;

#[cfg_attr(test, linkage = "available_externally")]
#[export_name = "_ZN16__rust_internals3std3sys3sgx4args4ARGSE"]
Expand All @@ -25,35 +24,7 @@ pub unsafe fn init(argc: isize, argv: *const *const u8) {

pub fn args() -> Args {
let args = unsafe { (ARGS.load(Ordering::Relaxed) as *const ArgsStore).as_ref() };
if let Some(args) = args { Args(args.iter()) } else { Args([].iter()) }
if let Some(args) = args { args.clone().into_iter() } else { Vec::new().into_iter() }
}

pub struct Args(slice::Iter<'static, OsString>);

impl fmt::Debug for Args {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.as_slice().fmt(f)
}
}

impl Iterator for Args {
type Item = OsString;
fn next(&mut self) -> Option<OsString> {
self.0.next().cloned()
}
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().cloned()
}
}
pub type Args = IntoIter<OsString>;
44 changes: 5 additions & 39 deletions library/std/src/sys/unix/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
#![allow(dead_code)] // runtime init functions not used during testing

use crate::ffi::OsString;
use crate::fmt;
use crate::vec;
use crate::vec::IntoIter;

/// One-time global initialization.
pub unsafe fn init(argc: isize, argv: *const *const u8) {
Expand All @@ -24,40 +23,7 @@ pub fn args() -> Args {
imp::args()
}

pub struct Args {
iter: vec::IntoIter<OsString>,
}

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

impl fmt::Debug for Args {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.iter.as_slice().fmt(f)
}
}

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

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

impl DoubleEndedIterator for Args {
fn next_back(&mut self) -> Option<OsString> {
self.iter.next_back()
}
}
pub type Args = IntoIter<OsString>;

#[cfg(any(
target_os = "linux",
Expand Down Expand Up @@ -134,7 +100,7 @@ mod imp {
}

pub fn args() -> Args {
Args { iter: clone().into_iter() }
clone().into_iter()
}

fn clone() -> Vec<OsString> {
Expand Down Expand Up @@ -180,7 +146,7 @@ mod imp {
})
.collect::<Vec<_>>()
};
Args { iter: vec.into_iter() }
vec.into_iter()
}

// As _NSGetArgc and _NSGetArgv aren't mentioned in iOS docs
Expand Down Expand Up @@ -247,6 +213,6 @@ mod imp {
}
}

Args { iter: res.into_iter() }
res.into_iter()
}
}
34 changes: 3 additions & 31 deletions library/std/src/sys/unsupported/args.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,8 @@
use crate::ffi::OsString;
use crate::fmt;
use crate::vec::IntoIter;

pub struct Args {}
pub type Args = IntoIter<OsString>;

pub fn args() -> Args {
Args {}
}

impl fmt::Debug for Args {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_list().finish()
}
}

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

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

impl DoubleEndedIterator for Args {
fn next_back(&mut self) -> Option<OsString> {
None
}
Vec::new().into_iter()
}
40 changes: 3 additions & 37 deletions library/std/src/sys/wasi/args.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
#![deny(unsafe_op_in_unsafe_fn)]

use crate::ffi::{CStr, OsStr, OsString};
use crate::fmt;
use crate::os::wasi::ffi::OsStrExt;
use crate::vec;
use crate::vec::IntoIter;

pub struct Args {
iter: vec::IntoIter<OsString>,
}

impl !Send for Args {}
impl !Sync for Args {}
pub type Args = IntoIter<OsString>;

/// Returns the command line arguments
pub fn args() -> Args {
Args { iter: maybe_args().unwrap_or(Vec::new()).into_iter() }
maybe_args().unwrap_or(Vec::new()).into_iter()
}

fn maybe_args() -> Option<Vec<OsString>> {
Expand All @@ -32,31 +26,3 @@ fn maybe_args() -> Option<Vec<OsString>> {
Some(ret)
}
}

impl fmt::Debug for Args {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.iter.as_slice().fmt(f)
}
}

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

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

impl DoubleEndedIterator for Args {
fn next_back(&mut self) -> Option<OsString> {
self.iter.next_back()
}
}
Loading