]> git.proxmox.com Git - rustc.git/blob - library/std/src/sys/hermit/args.rs
New upstream version 1.48.0~beta.8+dfsg1
[rustc.git] / library / std / src / sys / hermit / args.rs
1 use crate::ffi::OsString;
2 use crate::marker::PhantomData;
3 use crate::vec;
4
5 /// One-time global initialization.
6 pub unsafe fn init(argc: isize, argv: *const *const u8) {
7 imp::init(argc, argv)
8 }
9
10 /// One-time global cleanup.
11 pub unsafe fn cleanup() {
12 imp::cleanup()
13 }
14
15 /// Returns the command line arguments
16 pub fn args() -> Args {
17 imp::args()
18 }
19
20 pub struct Args {
21 iter: vec::IntoIter<OsString>,
22 _dont_send_or_sync_me: PhantomData<*mut ()>,
23 }
24
25 impl Args {
26 pub fn inner_debug(&self) -> &[OsString] {
27 self.iter.as_slice()
28 }
29 }
30
31 impl Iterator for Args {
32 type Item = OsString;
33 fn next(&mut self) -> Option<OsString> {
34 self.iter.next()
35 }
36 fn size_hint(&self) -> (usize, Option<usize>) {
37 self.iter.size_hint()
38 }
39 }
40
41 impl ExactSizeIterator for Args {
42 fn len(&self) -> usize {
43 self.iter.len()
44 }
45 }
46
47 impl DoubleEndedIterator for Args {
48 fn next_back(&mut self) -> Option<OsString> {
49 self.iter.next_back()
50 }
51 }
52
53 mod imp {
54 use super::Args;
55 use crate::ffi::{CStr, OsString};
56 use crate::marker::PhantomData;
57 use crate::ptr;
58 use crate::sys_common::os_str_bytes::*;
59
60 use crate::sys_common::mutex::StaticMutex;
61
62 static mut ARGC: isize = 0;
63 static mut ARGV: *const *const u8 = ptr::null();
64 static LOCK: StaticMutex = StaticMutex::new();
65
66 pub unsafe fn init(argc: isize, argv: *const *const u8) {
67 let _guard = LOCK.lock();
68 ARGC = argc;
69 ARGV = argv;
70 }
71
72 pub unsafe fn cleanup() {
73 let _guard = LOCK.lock();
74 ARGC = 0;
75 ARGV = ptr::null();
76 }
77
78 pub fn args() -> Args {
79 Args { iter: clone().into_iter(), _dont_send_or_sync_me: PhantomData }
80 }
81
82 fn clone() -> Vec<OsString> {
83 unsafe {
84 let _guard = LOCK.lock();
85 (0..ARGC)
86 .map(|i| {
87 let cstr = CStr::from_ptr(*ARGV.offset(i) as *const i8);
88 OsStringExt::from_vec(cstr.to_bytes().to_vec())
89 })
90 .collect()
91 }
92 }
93 }