Skip to content

Commit 01485c9

Browse files
committed
Unify owned Env types between platforms
Also, update the same pattern of reuse in `sys::args` to match.
1 parent 4695212 commit 01485c9

File tree

15 files changed

+98
-348
lines changed

15 files changed

+98
-348
lines changed

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

+10
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@
22
33
#![forbid(unsafe_op_in_unsafe_fn)]
44

5+
#[cfg(any(
6+
all(target_family = "unix", not(any(target_os = "espidf", target_os = "vita"))),
7+
target_family = "windows",
8+
target_os = "hermit",
9+
target_os = "uefi",
10+
target_os = "wasi",
11+
target_os = "xous",
12+
))]
13+
mod common;
14+
515
cfg_if::cfg_if! {
616
if #[cfg(any(
717
all(target_family = "unix", not(any(target_os = "espidf", target_os = "vita"))),

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

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

3+
pub use super::common::Args;
34
use crate::env::current_exe;
45
use crate::ffi::OsString;
56
use crate::iter::Iterator;
67
use crate::sys::pal::helpers;
78

8-
#[path = "common.rs"]
9-
mod common;
10-
pub use common::Args;
11-
129
pub fn args() -> Args {
1310
let lazy_current_exe = || Vec::from([current_exe().map(Into::into).unwrap_or_default()]);
1411

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

+1-4
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,13 @@
55
66
#![allow(dead_code)] // runtime init functions not used during testing
77

8+
pub use super::common::Args;
89
use crate::ffi::CStr;
910
#[cfg(target_os = "hermit")]
1011
use crate::os::hermit::ffi::OsStringExt;
1112
#[cfg(not(target_os = "hermit"))]
1213
use crate::os::unix::ffi::OsStringExt;
1314

14-
#[path = "common.rs"]
15-
mod common;
16-
pub use common::Args;
17-
1815
/// One-time global initialization.
1916
pub unsafe fn init(argc: isize, argv: *const *const u8) {
2017
unsafe { imp::init(argc, argv) }

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

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
#![forbid(unsafe_op_in_unsafe_fn)]
22

3+
pub use super::common::Args;
34
use crate::ffi::{CStr, OsStr, OsString};
45
use crate::os::wasi::ffi::OsStrExt;
56

6-
#[path = "common.rs"]
7-
mod common;
8-
pub use common::Args;
9-
107
/// Returns the command line arguments
118
pub fn args() -> Args {
129
Args::new(maybe_args().unwrap_or(Vec::new()))

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

+1-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#[cfg(test)]
77
mod tests;
88

9+
pub use super::common::Args;
910
use crate::ffi::{OsStr, OsString};
1011
use crate::num::NonZero;
1112
use crate::os::windows::prelude::*;
@@ -18,10 +19,6 @@ use crate::sys_common::AsInner;
1819
use crate::sys_common::wstr::WStrUnits;
1920
use crate::{io, iter, ptr};
2021

21-
#[path = "common.rs"]
22-
mod common;
23-
pub use common::Args;
24-
2522
pub fn args() -> Args {
2623
// SAFETY: `GetCommandLineW` returns a pointer to a null terminated UTF-16
2724
// string so it's safe for `WStrUnits` to use.

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

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1+
pub use super::common::Args;
12
use crate::sys::pal::os::get_application_parameters;
23
use crate::sys::pal::os::params::ArgumentList;
34

4-
#[path = "common.rs"]
5-
mod common;
6-
pub use common::Args;
7-
85
pub fn args() -> Args {
96
let Some(params) = get_application_parameters() else {
107
return Args::new(vec![]);

library/std/src/sys/env/common.rs

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
use crate::ffi::OsString;
2+
use crate::{fmt, vec};
3+
4+
pub struct Env {
5+
iter: vec::IntoIter<(OsString, OsString)>,
6+
}
7+
8+
// FIXME(https://github.com/rust-lang/rust/issues/114583): Remove this when <OsStr as Debug>::fmt matches <str as Debug>::fmt.
9+
pub struct EnvStrDebug<'a> {
10+
slice: &'a [(OsString, OsString)],
11+
}
12+
13+
impl fmt::Debug for EnvStrDebug<'_> {
14+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
15+
f.debug_list()
16+
.entries(self.slice.iter().map(|(a, b)| (a.to_str().unwrap(), b.to_str().unwrap())))
17+
.finish()
18+
}
19+
}
20+
21+
impl Env {
22+
pub(super) fn new(env: Vec<(OsString, OsString)>) -> Self {
23+
Env { iter: env.into_iter() }
24+
}
25+
26+
pub fn str_debug(&self) -> impl fmt::Debug + '_ {
27+
EnvStrDebug { slice: self.iter.as_slice() }
28+
}
29+
}
30+
31+
impl fmt::Debug for Env {
32+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
33+
f.debug_list().entries(self.iter.as_slice()).finish()
34+
}
35+
}
36+
37+
impl !Send for Env {}
38+
impl !Sync for Env {}
39+
40+
impl Iterator for Env {
41+
type Item = (OsString, OsString);
42+
fn next(&mut self) -> Option<(OsString, OsString)> {
43+
self.iter.next()
44+
}
45+
fn size_hint(&self) -> (usize, Option<usize>) {
46+
self.iter.size_hint()
47+
}
48+
}

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

+4-48
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
use core::slice::memchr;
22

3+
pub use super::common::Env;
34
use crate::collections::HashMap;
45
use crate::ffi::{CStr, OsStr, OsString, c_char};
6+
use crate::io;
57
use crate::os::hermit::ffi::OsStringExt;
68
use crate::sync::Mutex;
7-
use crate::{fmt, io, vec};
89

910
static ENV: Mutex<Option<HashMap<OsString, OsString>>> = Mutex::new(None);
1011

@@ -44,60 +45,15 @@ pub fn init(env: *const *const c_char) {
4445
}
4546
}
4647

47-
pub struct Env {
48-
iter: vec::IntoIter<(OsString, OsString)>,
49-
}
50-
51-
// FIXME(https://github.com/rust-lang/rust/issues/114583): Remove this when <OsStr as Debug>::fmt matches <str as Debug>::fmt.
52-
pub struct EnvStrDebug<'a> {
53-
slice: &'a [(OsString, OsString)],
54-
}
55-
56-
impl fmt::Debug for EnvStrDebug<'_> {
57-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
58-
let Self { slice } = self;
59-
f.debug_list()
60-
.entries(slice.iter().map(|(a, b)| (a.to_str().unwrap(), b.to_str().unwrap())))
61-
.finish()
62-
}
63-
}
64-
65-
impl Env {
66-
pub fn str_debug(&self) -> impl fmt::Debug + '_ {
67-
let Self { iter } = self;
68-
EnvStrDebug { slice: iter.as_slice() }
69-
}
70-
}
71-
72-
impl fmt::Debug for Env {
73-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
74-
let Self { iter } = self;
75-
f.debug_list().entries(iter.as_slice()).finish()
76-
}
77-
}
78-
79-
impl !Send for Env {}
80-
impl !Sync for Env {}
81-
82-
impl Iterator for Env {
83-
type Item = (OsString, OsString);
84-
fn next(&mut self) -> Option<(OsString, OsString)> {
85-
self.iter.next()
86-
}
87-
fn size_hint(&self) -> (usize, Option<usize>) {
88-
self.iter.size_hint()
89-
}
90-
}
91-
9248
/// Returns a vector of (variable, value) byte-vector pairs for all the
9349
/// environment variables of the current process.
9450
pub fn env() -> Env {
9551
let guard = ENV.lock().unwrap();
9652
let env = guard.as_ref().unwrap();
9753

98-
let result = env.iter().map(|(key, value)| (key.clone(), value.clone())).collect::<Vec<_>>();
54+
let result = env.iter().map(|(key, value)| (key.clone(), value.clone())).collect();
9955

100-
Env { iter: result.into_iter() }
56+
Env::new(result)
10157
}
10258

10359
pub fn getenv(k: &OsStr) -> Option<OsString> {

library/std/src/sys/env/mod.rs

+11
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@
22
33
#![forbid(unsafe_op_in_unsafe_fn)]
44

5+
#[cfg(any(
6+
target_family = "unix",
7+
target_os = "hermit",
8+
all(target_vendor = "fortanix", target_env = "sgx"),
9+
target_os = "solid_asp3",
10+
target_os = "uefi",
11+
target_os = "wasi",
12+
target_os = "xous",
13+
))]
14+
mod common;
15+
516
cfg_if::cfg_if! {
617
if #[cfg(target_family = "unix")] {
718
mod unix;

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

+4-51
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
#![allow(fuzzy_provenance_casts)] // FIXME: this module systematically confuses pointers and integers
22

3+
pub use super::common::Env;
34
use crate::collections::HashMap;
45
use crate::ffi::{OsStr, OsString};
6+
use crate::io;
57
use crate::sync::atomic::{AtomicUsize, Ordering};
68
use crate::sync::{Mutex, Once};
7-
use crate::{fmt, io, vec};
89

910
// Specifying linkage/symbol name is solely to ensure a single instance between this crate and its unit tests
1011
#[cfg_attr(test, linkage = "available_externally")]
@@ -27,61 +28,13 @@ fn create_env_store() -> &'static EnvStore {
2728
unsafe { &*(ENV.load(Ordering::Relaxed) as *const EnvStore) }
2829
}
2930

30-
pub struct Env {
31-
iter: vec::IntoIter<(OsString, OsString)>,
32-
}
33-
34-
// FIXME(https://github.com/rust-lang/rust/issues/114583): Remove this when <OsStr as Debug>::fmt matches <str as Debug>::fmt.
35-
pub struct EnvStrDebug<'a> {
36-
slice: &'a [(OsString, OsString)],
37-
}
38-
39-
impl fmt::Debug for EnvStrDebug<'_> {
40-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
41-
let Self { slice } = self;
42-
f.debug_list()
43-
.entries(slice.iter().map(|(a, b)| (a.to_str().unwrap(), b.to_str().unwrap())))
44-
.finish()
45-
}
46-
}
47-
48-
impl Env {
49-
pub fn str_debug(&self) -> impl fmt::Debug + '_ {
50-
let Self { iter } = self;
51-
EnvStrDebug { slice: iter.as_slice() }
52-
}
53-
}
54-
55-
impl fmt::Debug for Env {
56-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
57-
let Self { iter } = self;
58-
f.debug_list().entries(iter.as_slice()).finish()
59-
}
60-
}
61-
62-
impl !Send for Env {}
63-
impl !Sync for Env {}
64-
65-
impl Iterator for Env {
66-
type Item = (OsString, OsString);
67-
fn next(&mut self) -> Option<(OsString, OsString)> {
68-
self.iter.next()
69-
}
70-
fn size_hint(&self) -> (usize, Option<usize>) {
71-
self.iter.size_hint()
72-
}
73-
}
74-
7531
pub fn env() -> Env {
7632
let clone_to_vec = |map: &HashMap<OsString, OsString>| -> Vec<_> {
7733
map.iter().map(|(k, v)| (k.clone(), v.clone())).collect()
7834
};
7935

80-
let iter = get_env_store()
81-
.map(|env| clone_to_vec(&env.lock().unwrap()))
82-
.unwrap_or_default()
83-
.into_iter();
84-
Env { iter }
36+
let env = get_env_store().map(|env| clone_to_vec(&env.lock().unwrap())).unwrap_or_default();
37+
Env::new(env)
8538
}
8639

8740
pub fn getenv(k: &OsStr) -> Option<OsString> {

library/std/src/sys/env/solid.rs

+3-47
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,19 @@
11
use core::slice::memchr;
22

3+
pub use super::common::Env;
34
use crate::ffi::{CStr, OsStr, OsString};
5+
use crate::io;
46
use crate::os::raw::{c_char, c_int};
57
use crate::os::solid::ffi::{OsStrExt, OsStringExt};
68
use crate::sync::{PoisonError, RwLock};
79
use crate::sys::common::small_c_string::run_with_cstr;
8-
use crate::{fmt, io, vec};
910

1011
static ENV_LOCK: RwLock<()> = RwLock::new(());
1112

1213
pub fn env_read_lock() -> impl Drop {
1314
ENV_LOCK.read().unwrap_or_else(PoisonError::into_inner)
1415
}
1516

16-
pub struct Env {
17-
iter: vec::IntoIter<(OsString, OsString)>,
18-
}
19-
20-
// FIXME(https://github.com/rust-lang/rust/issues/114583): Remove this when <OsStr as Debug>::fmt matches <str as Debug>::fmt.
21-
pub struct EnvStrDebug<'a> {
22-
slice: &'a [(OsString, OsString)],
23-
}
24-
25-
impl fmt::Debug for EnvStrDebug<'_> {
26-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
27-
let Self { slice } = self;
28-
f.debug_list()
29-
.entries(slice.iter().map(|(a, b)| (a.to_str().unwrap(), b.to_str().unwrap())))
30-
.finish()
31-
}
32-
}
33-
34-
impl Env {
35-
pub fn str_debug(&self) -> impl fmt::Debug + '_ {
36-
let Self { iter } = self;
37-
EnvStrDebug { slice: iter.as_slice() }
38-
}
39-
}
40-
41-
impl fmt::Debug for Env {
42-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
43-
let Self { iter } = self;
44-
f.debug_list().entries(iter.as_slice()).finish()
45-
}
46-
}
47-
48-
impl !Send for Env {}
49-
impl !Sync for Env {}
50-
51-
impl Iterator for Env {
52-
type Item = (OsString, OsString);
53-
fn next(&mut self) -> Option<(OsString, OsString)> {
54-
self.iter.next()
55-
}
56-
fn size_hint(&self) -> (usize, Option<usize>) {
57-
self.iter.size_hint()
58-
}
59-
}
60-
6117
/// Returns a vector of (variable, value) byte-vector pairs for all the
6218
/// environment variables of the current process.
6319
pub fn env() -> Env {
@@ -76,7 +32,7 @@ pub fn env() -> Env {
7632
environ = environ.add(1);
7733
}
7834
}
79-
return Env { iter: result.into_iter() };
35+
return Env::new(result);
8036
}
8137

8238
fn parse(input: &[u8]) -> Option<(OsString, OsString)> {

0 commit comments

Comments
 (0)