Skip to content

Commit fffe272

Browse files
committed
Make sys::args::Args a type alias instead of wrapper
1 parent 3824017 commit fffe272

File tree

7 files changed

+46
-255
lines changed

7 files changed

+46
-255
lines changed

library/std/src/env.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -817,7 +817,8 @@ impl DoubleEndedIterator for Args {
817817
#[stable(feature = "std_debug", since = "1.16.0")]
818818
impl fmt::Debug for Args {
819819
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
820-
f.debug_struct("Args").field("inner", &self.inner.inner).finish()
820+
let args = &AsRef::<[OsString]>::as_ref(&self.inner.inner);
821+
f.debug_struct("Args").field("inner", args).finish()
821822
}
822823
}
823824

@@ -858,7 +859,8 @@ impl DoubleEndedIterator for ArgsOs {
858859
#[stable(feature = "std_debug", since = "1.16.0")]
859860
impl fmt::Debug for ArgsOs {
860861
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
861-
f.debug_struct("ArgsOs").field("inner", &self.inner).finish()
862+
let args = &AsRef::<[OsString]>::as_ref(&self.inner);
863+
f.debug_struct("ArgsOs").field("inner", args).finish()
862864
}
863865
}
864866

library/std/src/sys/hermit/args.rs

+25-80
Original file line numberDiff line numberDiff line change
@@ -1,94 +1,39 @@
1-
use crate::ffi::OsString;
2-
use crate::fmt;
3-
use crate::vec;
1+
use crate::ffi::{CStr, OsString};
2+
use crate::os::unix::ffi::OsStringExt;
3+
use crate::ptr;
4+
use crate::sys_common::mutex::StaticMutex;
5+
use crate::vec::IntoIter;
6+
7+
static mut ARGC: isize = 0;
8+
static mut ARGV: *const *const u8 = ptr::null();
9+
static LOCK: StaticMutex = StaticMutex::new();
410

511
/// One-time global initialization.
612
pub unsafe fn init(argc: isize, argv: *const *const u8) {
7-
imp::init(argc, argv)
13+
let _guard = LOCK.lock();
14+
ARGC = argc;
15+
ARGV = argv;
816
}
917

1018
/// One-time global cleanup.
1119
pub unsafe fn cleanup() {
12-
imp::cleanup()
20+
let _guard = LOCK.lock();
21+
ARGC = 0;
22+
ARGV = ptr::null();
1323
}
1424

1525
/// Returns the command line arguments
1626
pub fn args() -> Args {
17-
imp::args()
18-
}
19-
20-
pub struct Args {
21-
iter: vec::IntoIter<OsString>,
22-
}
23-
24-
impl fmt::Debug for Args {
25-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
26-
self.iter.as_slice().fmt(f)
27-
}
28-
}
29-
30-
impl !Send for Args {}
31-
impl !Sync for Args {}
32-
33-
impl Iterator for Args {
34-
type Item = OsString;
35-
fn next(&mut self) -> Option<OsString> {
36-
self.iter.next()
37-
}
38-
fn size_hint(&self) -> (usize, Option<usize>) {
39-
self.iter.size_hint()
40-
}
41-
}
42-
43-
impl ExactSizeIterator for Args {
44-
fn len(&self) -> usize {
45-
self.iter.len()
46-
}
47-
}
48-
49-
impl DoubleEndedIterator for Args {
50-
fn next_back(&mut self) -> Option<OsString> {
51-
self.iter.next_back()
52-
}
53-
}
54-
55-
mod imp {
56-
use super::Args;
57-
use crate::ffi::{CStr, OsString};
58-
use crate::os::unix::ffi::OsStringExt;
59-
use crate::ptr;
60-
61-
use crate::sys_common::mutex::StaticMutex;
62-
63-
static mut ARGC: isize = 0;
64-
static mut ARGV: *const *const u8 = ptr::null();
65-
static LOCK: StaticMutex = StaticMutex::new();
66-
67-
pub unsafe fn init(argc: isize, argv: *const *const u8) {
27+
unsafe {
6828
let _guard = LOCK.lock();
69-
ARGC = argc;
70-
ARGV = argv;
71-
}
72-
73-
pub unsafe fn cleanup() {
74-
let _guard = LOCK.lock();
75-
ARGC = 0;
76-
ARGV = ptr::null();
77-
}
78-
79-
pub fn args() -> Args {
80-
Args { iter: clone().into_iter() }
81-
}
82-
83-
fn clone() -> Vec<OsString> {
84-
unsafe {
85-
let _guard = LOCK.lock();
86-
(0..ARGC)
87-
.map(|i| {
88-
let cstr = CStr::from_ptr(*ARGV.offset(i) as *const i8);
89-
OsStringExt::from_vec(cstr.to_bytes().to_vec())
90-
})
91-
.collect()
92-
}
29+
(0..ARGC)
30+
.map(|i| {
31+
let cstr = CStr::from_ptr(*ARGV.offset(i) as *const i8);
32+
OsStringExt::from_vec(cstr.to_bytes().to_vec())
33+
})
34+
.collect::<Vec<_>>()
35+
.into_iter()
9336
}
9437
}
38+
39+
pub type Args = IntoIter<OsString>;

library/std/src/sys/sgx/args.rs

+3-32
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
use super::abi::usercalls::{alloc, raw::ByteBuffer};
22
use crate::ffi::OsString;
3-
use crate::fmt;
4-
use crate::slice;
53
use crate::sync::atomic::{AtomicUsize, Ordering};
64
use crate::sys::os_str::Buf;
75
use crate::sys_common::FromInner;
6+
use crate::vec::IntoIter;
87

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

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

31-
pub struct Args(slice::Iter<'static, OsString>);
32-
33-
impl fmt::Debug for Args {
34-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
35-
self.0.as_slice().fmt(f)
36-
}
37-
}
38-
39-
impl Iterator for Args {
40-
type Item = OsString;
41-
fn next(&mut self) -> Option<OsString> {
42-
self.0.next().cloned()
43-
}
44-
fn size_hint(&self) -> (usize, Option<usize>) {
45-
self.0.size_hint()
46-
}
47-
}
48-
49-
impl ExactSizeIterator for Args {
50-
fn len(&self) -> usize {
51-
self.0.len()
52-
}
53-
}
54-
55-
impl DoubleEndedIterator for Args {
56-
fn next_back(&mut self) -> Option<OsString> {
57-
self.0.next_back().cloned()
58-
}
59-
}
30+
pub type Args = IntoIter<OsString>;

library/std/src/sys/unix/args.rs

+5-39
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
#![allow(dead_code)] // runtime init functions not used during testing
77

88
use crate::ffi::OsString;
9-
use crate::fmt;
10-
use crate::vec;
9+
use crate::vec::IntoIter;
1110

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

27-
pub struct Args {
28-
iter: vec::IntoIter<OsString>,
29-
}
30-
31-
impl !Send for Args {}
32-
impl !Sync for Args {}
33-
34-
impl fmt::Debug for Args {
35-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
36-
self.iter.as_slice().fmt(f)
37-
}
38-
}
39-
40-
impl Iterator for Args {
41-
type Item = OsString;
42-
fn next(&mut self) -> Option<OsString> {
43-
self.iter.next()
44-
}
45-
fn size_hint(&self) -> (usize, Option<usize>) {
46-
self.iter.size_hint()
47-
}
48-
}
49-
50-
impl ExactSizeIterator for Args {
51-
fn len(&self) -> usize {
52-
self.iter.len()
53-
}
54-
}
55-
56-
impl DoubleEndedIterator for Args {
57-
fn next_back(&mut self) -> Option<OsString> {
58-
self.iter.next_back()
59-
}
60-
}
26+
pub type Args = IntoIter<OsString>;
6127

6228
#[cfg(any(
6329
target_os = "linux",
@@ -134,7 +100,7 @@ mod imp {
134100
}
135101

136102
pub fn args() -> Args {
137-
Args { iter: clone().into_iter() }
103+
clone().into_iter()
138104
}
139105

140106
fn clone() -> Vec<OsString> {
@@ -180,7 +146,7 @@ mod imp {
180146
})
181147
.collect::<Vec<_>>()
182148
};
183-
Args { iter: vec.into_iter() }
149+
vec.into_iter()
184150
}
185151

186152
// As _NSGetArgc and _NSGetArgv aren't mentioned in iOS docs
@@ -247,6 +213,6 @@ mod imp {
247213
}
248214
}
249215

250-
Args { iter: res.into_iter() }
216+
res.into_iter()
251217
}
252218
}
+3-31
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,8 @@
11
use crate::ffi::OsString;
2-
use crate::fmt;
2+
use crate::vec::IntoIter;
33

4-
pub struct Args {}
4+
pub type Args = IntoIter<OsString>;
55

66
pub fn args() -> Args {
7-
Args {}
8-
}
9-
10-
impl fmt::Debug for Args {
11-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
12-
f.debug_list().finish()
13-
}
14-
}
15-
16-
impl Iterator for Args {
17-
type Item = OsString;
18-
fn next(&mut self) -> Option<OsString> {
19-
None
20-
}
21-
fn size_hint(&self) -> (usize, Option<usize>) {
22-
(0, Some(0))
23-
}
24-
}
25-
26-
impl ExactSizeIterator for Args {
27-
fn len(&self) -> usize {
28-
0
29-
}
30-
}
31-
32-
impl DoubleEndedIterator for Args {
33-
fn next_back(&mut self) -> Option<OsString> {
34-
None
35-
}
7+
Vec::new().into_iter()
368
}

library/std/src/sys/wasi/args.rs

+3-37
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
11
#![deny(unsafe_op_in_unsafe_fn)]
22

33
use crate::ffi::{CStr, OsStr, OsString};
4-
use crate::fmt;
54
use crate::os::wasi::ffi::OsStrExt;
6-
use crate::vec;
5+
use crate::vec::IntoIter;
76

8-
pub struct Args {
9-
iter: vec::IntoIter<OsString>,
10-
}
11-
12-
impl !Send for Args {}
13-
impl !Sync for Args {}
7+
pub type Args = IntoIter<OsString>;
148

159
/// Returns the command line arguments
1610
pub fn args() -> Args {
17-
Args { iter: maybe_args().unwrap_or(Vec::new()).into_iter() }
11+
maybe_args().unwrap_or(Vec::new()).into_iter()
1812
}
1913

2014
fn maybe_args() -> Option<Vec<OsString>> {
@@ -32,31 +26,3 @@ fn maybe_args() -> Option<Vec<OsString>> {
3226
Some(ret)
3327
}
3428
}
35-
36-
impl fmt::Debug for Args {
37-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
38-
self.iter.as_slice().fmt(f)
39-
}
40-
}
41-
42-
impl Iterator for Args {
43-
type Item = OsString;
44-
fn next(&mut self) -> Option<OsString> {
45-
self.iter.next()
46-
}
47-
fn size_hint(&self) -> (usize, Option<usize>) {
48-
self.iter.size_hint()
49-
}
50-
}
51-
52-
impl ExactSizeIterator for Args {
53-
fn len(&self) -> usize {
54-
self.iter.len()
55-
}
56-
}
57-
58-
impl DoubleEndedIterator for Args {
59-
fn next_back(&mut self) -> Option<OsString> {
60-
self.iter.next_back()
61-
}
62-
}

0 commit comments

Comments
 (0)