Unify owned Args types between platforms
This commit is contained in:
43
library/std/src/sys/args/common.rs
Normal file
43
library/std/src/sys/args/common.rs
Normal file
@@ -0,0 +1,43 @@
|
||||
use crate::ffi::OsString;
|
||||
use crate::{fmt, vec};
|
||||
|
||||
pub struct Args {
|
||||
iter: vec::IntoIter<OsString>,
|
||||
}
|
||||
|
||||
impl !Send for Args {}
|
||||
impl !Sync for Args {}
|
||||
|
||||
impl Args {
|
||||
pub(super) fn new(args: Vec<OsString>) -> Self {
|
||||
Args { iter: args.into_iter() }
|
||||
}
|
||||
}
|
||||
|
||||
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()
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,12 @@
|
||||
use crate::ffi::{CStr, OsString, c_char};
|
||||
use crate::os::hermit::ffi::OsStringExt;
|
||||
use crate::ptr;
|
||||
use crate::sync::atomic::Ordering::{Acquire, Relaxed, Release};
|
||||
use crate::sync::atomic::{AtomicIsize, AtomicPtr};
|
||||
use crate::{fmt, ptr, vec};
|
||||
|
||||
#[path = "common.rs"]
|
||||
mod common;
|
||||
pub use common::Args;
|
||||
|
||||
static ARGC: AtomicIsize = AtomicIsize::new(0);
|
||||
static ARGV: AtomicPtr<*const u8> = AtomicPtr::new(ptr::null_mut());
|
||||
@@ -27,40 +31,5 @@ pub fn args() -> Args {
|
||||
})
|
||||
.collect();
|
||||
|
||||
Args { iter: args.into_iter() }
|
||||
}
|
||||
|
||||
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()
|
||||
}
|
||||
Args::new(args)
|
||||
}
|
||||
|
||||
@@ -4,11 +4,10 @@ use crate::env::current_exe;
|
||||
use crate::ffi::OsString;
|
||||
use crate::iter::Iterator;
|
||||
use crate::sys::pal::helpers;
|
||||
use crate::{fmt, vec};
|
||||
|
||||
pub struct Args {
|
||||
parsed_args_list: vec::IntoIter<OsString>,
|
||||
}
|
||||
#[path = "common.rs"]
|
||||
mod common;
|
||||
pub use common::Args;
|
||||
|
||||
pub fn args() -> Args {
|
||||
let lazy_current_exe = || Vec::from([current_exe().map(Into::into).unwrap_or_default()]);
|
||||
@@ -22,51 +21,17 @@ pub fn args() -> Args {
|
||||
let lp_size = unsafe { (*protocol.as_ptr()).load_options_size } as usize;
|
||||
// Break if we are sure that it cannot be UTF-16
|
||||
if lp_size < size_of::<u16>() || lp_size % size_of::<u16>() != 0 {
|
||||
return Args { parsed_args_list: lazy_current_exe().into_iter() };
|
||||
return Args::new(lazy_current_exe());
|
||||
}
|
||||
let lp_size = lp_size / size_of::<u16>();
|
||||
|
||||
let lp_cmd_line = unsafe { (*protocol.as_ptr()).load_options as *const u16 };
|
||||
if !lp_cmd_line.is_aligned() {
|
||||
return Args { parsed_args_list: lazy_current_exe().into_iter() };
|
||||
return Args::new(lazy_current_exe());
|
||||
}
|
||||
let lp_cmd_line = unsafe { crate::slice::from_raw_parts(lp_cmd_line, lp_size) };
|
||||
|
||||
Args {
|
||||
parsed_args_list: parse_lp_cmd_line(lp_cmd_line)
|
||||
.unwrap_or_else(lazy_current_exe)
|
||||
.into_iter(),
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Debug for Args {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
self.parsed_args_list.as_slice().fmt(f)
|
||||
}
|
||||
}
|
||||
|
||||
impl Iterator for Args {
|
||||
type Item = OsString;
|
||||
|
||||
fn next(&mut self) -> Option<OsString> {
|
||||
self.parsed_args_list.next()
|
||||
}
|
||||
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
self.parsed_args_list.size_hint()
|
||||
}
|
||||
}
|
||||
|
||||
impl ExactSizeIterator for Args {
|
||||
fn len(&self) -> usize {
|
||||
self.parsed_args_list.len()
|
||||
}
|
||||
}
|
||||
|
||||
impl DoubleEndedIterator for Args {
|
||||
fn next_back(&mut self) -> Option<OsString> {
|
||||
self.parsed_args_list.next_back()
|
||||
}
|
||||
Args::new(parse_lp_cmd_line(lp_cmd_line).unwrap_or_else(lazy_current_exe))
|
||||
}
|
||||
|
||||
/// Implements the UEFI command-line argument parsing algorithm.
|
||||
|
||||
@@ -5,9 +5,12 @@
|
||||
|
||||
#![allow(dead_code)] // runtime init functions not used during testing
|
||||
|
||||
use crate::ffi::{CStr, OsString};
|
||||
use crate::ffi::CStr;
|
||||
use crate::os::unix::ffi::OsStringExt;
|
||||
use crate::{fmt, vec};
|
||||
|
||||
#[path = "common.rs"]
|
||||
mod common;
|
||||
pub use common::Args;
|
||||
|
||||
/// One-time global initialization.
|
||||
pub unsafe fn init(argc: isize, argv: *const *const u8) {
|
||||
@@ -55,42 +58,7 @@ pub fn args() -> Args {
|
||||
vec.push(OsStringExt::from_vec(cstr.to_bytes().to_vec()));
|
||||
}
|
||||
|
||||
Args { iter: vec.into_iter() }
|
||||
}
|
||||
|
||||
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()
|
||||
}
|
||||
Args::new(vec)
|
||||
}
|
||||
|
||||
#[cfg(any(
|
||||
|
||||
@@ -2,18 +2,14 @@
|
||||
|
||||
use crate::ffi::{CStr, OsStr, OsString};
|
||||
use crate::os::wasi::ffi::OsStrExt;
|
||||
use crate::{fmt, vec};
|
||||
|
||||
pub struct Args {
|
||||
iter: vec::IntoIter<OsString>,
|
||||
}
|
||||
|
||||
impl !Send for Args {}
|
||||
impl !Sync for Args {}
|
||||
#[path = "common.rs"]
|
||||
mod common;
|
||||
pub use common::Args;
|
||||
|
||||
/// Returns the command line arguments
|
||||
pub fn args() -> Args {
|
||||
Args { iter: maybe_args().unwrap_or(Vec::new()).into_iter() }
|
||||
Args::new(maybe_args().unwrap_or(Vec::new()))
|
||||
}
|
||||
|
||||
fn maybe_args() -> Option<Vec<OsString>> {
|
||||
@@ -31,31 +27,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()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,7 +16,11 @@ use crate::sys::path::get_long_path;
|
||||
use crate::sys::{c, to_u16s};
|
||||
use crate::sys_common::AsInner;
|
||||
use crate::sys_common::wstr::WStrUnits;
|
||||
use crate::{fmt, io, iter, ptr, vec};
|
||||
use crate::{io, iter, ptr};
|
||||
|
||||
#[path = "common.rs"]
|
||||
mod common;
|
||||
pub use common::Args;
|
||||
|
||||
pub fn args() -> Args {
|
||||
// SAFETY: `GetCommandLineW` returns a pointer to a null terminated UTF-16
|
||||
@@ -27,7 +31,7 @@ pub fn args() -> Args {
|
||||
current_exe().map(PathBuf::into_os_string).unwrap_or_else(|_| OsString::new())
|
||||
});
|
||||
|
||||
Args { parsed_args_list: parsed_args_list.into_iter() }
|
||||
Args::new(parsed_args_list)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -153,38 +157,6 @@ fn parse_lp_cmd_line<'a, F: Fn() -> OsString>(
|
||||
ret_val
|
||||
}
|
||||
|
||||
pub struct Args {
|
||||
parsed_args_list: vec::IntoIter<OsString>,
|
||||
}
|
||||
|
||||
impl fmt::Debug for Args {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
self.parsed_args_list.as_slice().fmt(f)
|
||||
}
|
||||
}
|
||||
|
||||
impl Iterator for Args {
|
||||
type Item = OsString;
|
||||
fn next(&mut self) -> Option<OsString> {
|
||||
self.parsed_args_list.next()
|
||||
}
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
self.parsed_args_list.size_hint()
|
||||
}
|
||||
}
|
||||
|
||||
impl DoubleEndedIterator for Args {
|
||||
fn next_back(&mut self) -> Option<OsString> {
|
||||
self.parsed_args_list.next_back()
|
||||
}
|
||||
}
|
||||
|
||||
impl ExactSizeIterator for Args {
|
||||
fn len(&self) -> usize {
|
||||
self.parsed_args_list.len()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) enum Arg {
|
||||
/// Add quotes (if needed)
|
||||
|
||||
@@ -1,15 +1,13 @@
|
||||
use crate::ffi::OsString;
|
||||
use crate::sys::pal::os::get_application_parameters;
|
||||
use crate::sys::pal::os::params::ArgumentList;
|
||||
use crate::{fmt, vec};
|
||||
|
||||
pub struct Args {
|
||||
parsed_args_list: vec::IntoIter<OsString>,
|
||||
}
|
||||
#[path = "common.rs"]
|
||||
mod common;
|
||||
pub use common::Args;
|
||||
|
||||
pub fn args() -> Args {
|
||||
let Some(params) = get_application_parameters() else {
|
||||
return Args { parsed_args_list: vec![].into_iter() };
|
||||
return Args::new(vec![]);
|
||||
};
|
||||
|
||||
for param in params {
|
||||
@@ -18,36 +16,8 @@ pub fn args() -> Args {
|
||||
for arg in args {
|
||||
parsed_args.push(arg.into());
|
||||
}
|
||||
return Args { parsed_args_list: parsed_args.into_iter() };
|
||||
return Args::new(parsed_args);
|
||||
}
|
||||
}
|
||||
Args { parsed_args_list: vec![].into_iter() }
|
||||
}
|
||||
|
||||
impl fmt::Debug for Args {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
self.parsed_args_list.as_slice().fmt(f)
|
||||
}
|
||||
}
|
||||
|
||||
impl Iterator for Args {
|
||||
type Item = OsString;
|
||||
fn next(&mut self) -> Option<OsString> {
|
||||
self.parsed_args_list.next()
|
||||
}
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
self.parsed_args_list.size_hint()
|
||||
}
|
||||
}
|
||||
|
||||
impl DoubleEndedIterator for Args {
|
||||
fn next_back(&mut self) -> Option<OsString> {
|
||||
self.parsed_args_list.next_back()
|
||||
}
|
||||
}
|
||||
|
||||
impl ExactSizeIterator for Args {
|
||||
fn len(&self) -> usize {
|
||||
self.parsed_args_list.len()
|
||||
}
|
||||
Args::new(vec![])
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user