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