Skip to content
/ rust Public
forked from rust-lang/rust

Commit daed9e2

Browse files
authored
Rollup merge of rust-lang#139710 - thaliaarchi:move-args-pal, r=joboet
Move `args` into `std::sys` Move platform definitions of `args` into `std::sys`, as part of rust-lang#117276. cc ``@joboet``
2 parents 8a6d6f5 + bea2022 commit daed9e2

File tree

29 files changed

+171
-315
lines changed

29 files changed

+171
-315
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+
}
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/mod.rs

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//! Platform-dependent command line arguments abstraction.
2+
3+
#![forbid(unsafe_op_in_unsafe_fn)]
4+
5+
cfg_if::cfg_if! {
6+
if #[cfg(all(target_family = "unix", not(any(target_os = "espidf", target_os = "vita"))))] {
7+
mod unix;
8+
pub use unix::*;
9+
} else if #[cfg(target_family = "windows")] {
10+
mod windows;
11+
pub use windows::*;
12+
} else if #[cfg(target_os = "hermit")] {
13+
mod hermit;
14+
pub use hermit::*;
15+
} else if #[cfg(all(target_vendor = "fortanix", target_env = "sgx"))] {
16+
mod sgx;
17+
pub use sgx::*;
18+
} else if #[cfg(target_os = "uefi")] {
19+
mod uefi;
20+
pub use uefi::*;
21+
} else if #[cfg(target_os = "wasi")] {
22+
mod wasi;
23+
pub use wasi::*;
24+
} else if #[cfg(target_os = "xous")] {
25+
mod xous;
26+
pub use xous::*;
27+
} else if #[cfg(target_os = "zkvm")] {
28+
mod zkvm;
29+
pub use zkvm::*;
30+
} else {
31+
mod unsupported;
32+
pub use unsupported::*;
33+
}
34+
}

Diff for: library/std/src/sys/pal/sgx/args.rs renamed to library/std/src/sys/args/sgx.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
use super::abi::usercalls::alloc;
2-
use super::abi::usercalls::raw::ByteBuffer;
1+
#![allow(fuzzy_provenance_casts)] // FIXME: this module systematically confuses pointers and integers
2+
33
use crate::ffi::OsString;
44
use crate::sync::atomic::{AtomicUsize, Ordering};
55
use crate::sys::os_str::Buf;
6+
use crate::sys::pal::abi::usercalls::alloc;
7+
use crate::sys::pal::abi::usercalls::raw::ByteBuffer;
68
use crate::sys_common::FromInner;
79
use crate::{fmt, slice};
810

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

+7-42
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
use r_efi::protocols::loaded_image;
22

3-
use super::helpers;
43
use crate::env::current_exe;
54
use crate::ffi::OsString;
65
use crate::iter::Iterator;
7-
use crate::{fmt, vec};
6+
use crate::sys::pal::helpers;
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/pal/unix/args.rs renamed to library/std/src/sys/args/unix.rs

+9-56
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@
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) {
14-
imp::init(argc, argv)
17+
unsafe { imp::init(argc, argv) }
1518
}
1619

1720
/// Returns the command line arguments
@@ -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(
@@ -141,7 +109,7 @@ mod imp {
141109
pub unsafe fn init(argc: isize, argv: *const *const u8) {
142110
// on GNU/Linux if we are main then we will init argv and argc twice, it "duplicates work"
143111
// BUT edge-cases are real: only using .init_array can break most emulators, dlopen, etc.
144-
really_init(argc, argv);
112+
unsafe { really_init(argc, argv) };
145113
}
146114

147115
/// glibc passes argc, argv, and envp to functions in .init_array, as a non-standard extension.
@@ -159,9 +127,7 @@ mod imp {
159127
argv: *const *const u8,
160128
_envp: *const *const u8,
161129
) {
162-
unsafe {
163-
really_init(argc as isize, argv);
164-
}
130+
unsafe { really_init(argc as isize, argv) };
165131
}
166132
init_wrapper
167133
};
@@ -228,16 +194,3 @@ mod imp {
228194
(argc as isize, argv.cast())
229195
}
230196
}
231-
232-
#[cfg(any(target_os = "espidf", target_os = "vita"))]
233-
mod imp {
234-
use crate::ffi::c_char;
235-
use crate::ptr;
236-
237-
#[inline(always)]
238-
pub unsafe fn init(_argc: isize, _argv: *const *const u8) {}
239-
240-
pub fn argc_argv() -> (isize, *const *const c_char) {
241-
(0, ptr::null())
242-
}
243-
}

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

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#![forbid(unsafe_op_in_unsafe_fn)]
2+
3+
use crate::ffi::{CStr, OsStr, OsString};
4+
use crate::os::wasi::ffi::OsStrExt;
5+
6+
#[path = "common.rs"]
7+
mod common;
8+
pub use common::Args;
9+
10+
/// Returns the command line arguments
11+
pub fn args() -> Args {
12+
Args::new(maybe_args().unwrap_or(Vec::new()))
13+
}
14+
15+
fn maybe_args() -> Option<Vec<OsString>> {
16+
unsafe {
17+
let (argc, buf_size) = wasi::args_sizes_get().ok()?;
18+
let mut argv = Vec::with_capacity(argc);
19+
let mut buf = Vec::with_capacity(buf_size);
20+
wasi::args_get(argv.as_mut_ptr(), buf.as_mut_ptr()).ok()?;
21+
argv.set_len(argc);
22+
let mut ret = Vec::with_capacity(argc);
23+
for ptr in argv {
24+
let s = CStr::from_ptr(ptr.cast());
25+
ret.push(OsStr::from_bytes(s.to_bytes()).to_owned());
26+
}
27+
Some(ret)
28+
}
29+
}

0 commit comments

Comments
 (0)