Skip to content

Commit bea2022

Browse files
committed
Unify owned Args types between platforms
1 parent e014fd6 commit bea2022

File tree

7 files changed

+77
-222
lines changed

7 files changed

+77
-222
lines changed

Diff for: library/std/src/sys/args/common.rs

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
use crate::ffi::OsString;
2+
use crate::{fmt, vec};
3+
4+
pub struct Args {
5+
iter: vec::IntoIter<OsString>,
6+
}
7+
8+
impl !Send for Args {}
9+
impl !Sync for Args {}
10+
11+
impl Args {
12+
pub(super) fn new(args: Vec<OsString>) -> Self {
13+
Args { iter: args.into_iter() }
14+
}
15+
}
16+
17+
impl fmt::Debug for Args {
18+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
19+
self.iter.as_slice().fmt(f)
20+
}
21+
}
22+
23+
impl Iterator for Args {
24+
type Item = OsString;
25+
fn next(&mut self) -> Option<OsString> {
26+
self.iter.next()
27+
}
28+
fn size_hint(&self) -> (usize, Option<usize>) {
29+
self.iter.size_hint()
30+
}
31+
}
32+
33+
impl ExactSizeIterator for Args {
34+
fn len(&self) -> usize {
35+
self.iter.len()
36+
}
37+
}
38+
39+
impl DoubleEndedIterator for Args {
40+
fn next_back(&mut self) -> Option<OsString> {
41+
self.iter.next_back()
42+
}
43+
}

Diff for: library/std/src/sys/args/hermit.rs

+6-37
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
use crate::ffi::{CStr, OsString, c_char};
22
use crate::os::hermit::ffi::OsStringExt;
3+
use crate::ptr;
34
use crate::sync::atomic::Ordering::{Acquire, Relaxed, Release};
45
use crate::sync::atomic::{AtomicIsize, AtomicPtr};
5-
use crate::{fmt, ptr, vec};
6+
7+
#[path = "common.rs"]
8+
mod common;
9+
pub use common::Args;
610

711
static ARGC: AtomicIsize = AtomicIsize::new(0);
812
static ARGV: AtomicPtr<*const u8> = AtomicPtr::new(ptr::null_mut());
@@ -27,40 +31,5 @@ pub fn args() -> Args {
2731
})
2832
.collect();
2933

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

Diff for: library/std/src/sys/args/uefi.rs

+6-41
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@ use crate::env::current_exe;
44
use crate::ffi::OsString;
55
use crate::iter::Iterator;
66
use crate::sys::pal::helpers;
7-
use crate::{fmt, vec};
87

9-
pub struct Args {
10-
parsed_args_list: vec::IntoIter<OsString>,
11-
}
8+
#[path = "common.rs"]
9+
mod common;
10+
pub use common::Args;
1211

1312
pub fn args() -> Args {
1413
let lazy_current_exe = || Vec::from([current_exe().map(Into::into).unwrap_or_default()]);
@@ -22,51 +21,17 @@ pub fn args() -> Args {
2221
let lp_size = unsafe { (*protocol.as_ptr()).load_options_size } as usize;
2322
// Break if we are sure that it cannot be UTF-16
2423
if lp_size < size_of::<u16>() || lp_size % size_of::<u16>() != 0 {
25-
return Args { parsed_args_list: lazy_current_exe().into_iter() };
24+
return Args::new(lazy_current_exe());
2625
}
2726
let lp_size = lp_size / size_of::<u16>();
2827

2928
let lp_cmd_line = unsafe { (*protocol.as_ptr()).load_options as *const u16 };
3029
if !lp_cmd_line.is_aligned() {
31-
return Args { parsed_args_list: lazy_current_exe().into_iter() };
30+
return Args::new(lazy_current_exe());
3231
}
3332
let lp_cmd_line = unsafe { crate::slice::from_raw_parts(lp_cmd_line, lp_size) };
3433

35-
Args {
36-
parsed_args_list: parse_lp_cmd_line(lp_cmd_line)
37-
.unwrap_or_else(lazy_current_exe)
38-
.into_iter(),
39-
}
40-
}
41-
42-
impl fmt::Debug for Args {
43-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
44-
self.parsed_args_list.as_slice().fmt(f)
45-
}
46-
}
47-
48-
impl Iterator for Args {
49-
type Item = OsString;
50-
51-
fn next(&mut self) -> Option<OsString> {
52-
self.parsed_args_list.next()
53-
}
54-
55-
fn size_hint(&self) -> (usize, Option<usize>) {
56-
self.parsed_args_list.size_hint()
57-
}
58-
}
59-
60-
impl ExactSizeIterator for Args {
61-
fn len(&self) -> usize {
62-
self.parsed_args_list.len()
63-
}
64-
}
65-
66-
impl DoubleEndedIterator for Args {
67-
fn next_back(&mut self) -> Option<OsString> {
68-
self.parsed_args_list.next_back()
69-
}
34+
Args::new(parse_lp_cmd_line(lp_cmd_line).unwrap_or_else(lazy_current_exe))
7035
}
7136

7237
/// Implements the UEFI command-line argument parsing algorithm.

Diff for: library/std/src/sys/args/unix.rs

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

8-
use crate::ffi::{CStr, OsString};
8+
use crate::ffi::CStr;
99
use crate::os::unix::ffi::OsStringExt;
10-
use crate::{fmt, vec};
10+
11+
#[path = "common.rs"]
12+
mod common;
13+
pub use common::Args;
1114

1215
/// One-time global initialization.
1316
pub unsafe fn init(argc: isize, argv: *const *const u8) {
@@ -55,42 +58,7 @@ pub fn args() -> Args {
5558
vec.push(OsStringExt::from_vec(cstr.to_bytes().to_vec()));
5659
}
5760

58-
Args { iter: vec.into_iter() }
59-
}
60-
61-
pub struct Args {
62-
iter: vec::IntoIter<OsString>,
63-
}
64-
65-
impl !Send for Args {}
66-
impl !Sync for Args {}
67-
68-
impl fmt::Debug for Args {
69-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
70-
self.iter.as_slice().fmt(f)
71-
}
72-
}
73-
74-
impl Iterator for Args {
75-
type Item = OsString;
76-
fn next(&mut self) -> Option<OsString> {
77-
self.iter.next()
78-
}
79-
fn size_hint(&self) -> (usize, Option<usize>) {
80-
self.iter.size_hint()
81-
}
82-
}
83-
84-
impl ExactSizeIterator for Args {
85-
fn len(&self) -> usize {
86-
self.iter.len()
87-
}
88-
}
89-
90-
impl DoubleEndedIterator for Args {
91-
fn next_back(&mut self) -> Option<OsString> {
92-
self.iter.next_back()
93-
}
61+
Args::new(vec)
9462
}
9563

9664
#[cfg(any(

Diff for: library/std/src/sys/args/wasi.rs

+4-36
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,14 @@
22

33
use crate::ffi::{CStr, OsStr, OsString};
44
use crate::os::wasi::ffi::OsStrExt;
5-
use crate::{fmt, vec};
65

7-
pub struct Args {
8-
iter: vec::IntoIter<OsString>,
9-
}
10-
11-
impl !Send for Args {}
12-
impl !Sync for Args {}
6+
#[path = "common.rs"]
7+
mod common;
8+
pub use common::Args;
139

1410
/// Returns the command line arguments
1511
pub fn args() -> Args {
16-
Args { iter: maybe_args().unwrap_or(Vec::new()).into_iter() }
12+
Args::new(maybe_args().unwrap_or(Vec::new()))
1713
}
1814

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

Diff for: library/std/src/sys/args/windows.rs

+6-34
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ use crate::sys::path::get_long_path;
1616
use crate::sys::{c, to_u16s};
1717
use crate::sys_common::AsInner;
1818
use crate::sys_common::wstr::WStrUnits;
19-
use crate::{fmt, io, iter, ptr, vec};
19+
use crate::{io, iter, ptr};
20+
21+
#[path = "common.rs"]
22+
mod common;
23+
pub use common::Args;
2024

2125
pub fn args() -> Args {
2226
// SAFETY: `GetCommandLineW` returns a pointer to a null terminated UTF-16
@@ -27,7 +31,7 @@ pub fn args() -> Args {
2731
current_exe().map(PathBuf::into_os_string).unwrap_or_else(|_| OsString::new())
2832
});
2933

30-
Args { parsed_args_list: parsed_args_list.into_iter() }
34+
Args::new(parsed_args_list)
3135
}
3236
}
3337

@@ -153,38 +157,6 @@ fn parse_lp_cmd_line<'a, F: Fn() -> OsString>(
153157
ret_val
154158
}
155159

156-
pub struct Args {
157-
parsed_args_list: vec::IntoIter<OsString>,
158-
}
159-
160-
impl fmt::Debug for Args {
161-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
162-
self.parsed_args_list.as_slice().fmt(f)
163-
}
164-
}
165-
166-
impl Iterator for Args {
167-
type Item = OsString;
168-
fn next(&mut self) -> Option<OsString> {
169-
self.parsed_args_list.next()
170-
}
171-
fn size_hint(&self) -> (usize, Option<usize>) {
172-
self.parsed_args_list.size_hint()
173-
}
174-
}
175-
176-
impl DoubleEndedIterator for Args {
177-
fn next_back(&mut self) -> Option<OsString> {
178-
self.parsed_args_list.next_back()
179-
}
180-
}
181-
182-
impl ExactSizeIterator for Args {
183-
fn len(&self) -> usize {
184-
self.parsed_args_list.len()
185-
}
186-
}
187-
188160
#[derive(Debug)]
189161
pub(crate) enum Arg {
190162
/// Add quotes (if needed)

0 commit comments

Comments
 (0)