1 #![deny(unsafe_op_in_unsafe_fn)]
3 use crate::ffi::{CStr, OsStr, OsString};
5 use crate::os::wasi::ffi::OsStrExt;
9 iter: vec::IntoIter<OsString>,
12 impl !Send for Args {}
13 impl !Sync for Args {}
15 /// Returns the command line arguments
16 pub fn args() -> Args {
17 Args { iter: maybe_args().unwrap_or(Vec::new()).into_iter() }
20 fn maybe_args() -> Option<Vec<OsString>> {
22 let (argc, buf_size) = wasi::args_sizes_get().ok()?;
23 let mut argv = Vec::with_capacity(argc);
24 let mut buf = Vec::with_capacity(buf_size);
25 wasi::args_get(argv.as_mut_ptr(), buf.as_mut_ptr()).ok()?;
27 let mut ret = Vec::with_capacity(argc);
29 let s = CStr::from_ptr(ptr.cast());
30 ret.push(OsStr::from_bytes(s.to_bytes()).to_owned());
36 impl fmt::Debug for Args {
37 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
38 self.iter.as_slice().fmt(f)
42 impl Iterator for Args {
44 fn next(&mut self) -> Option<OsString> {
47 fn size_hint(&self) -> (usize, Option<usize>) {
52 impl ExactSizeIterator for Args {
53 fn len(&self) -> usize {
58 impl DoubleEndedIterator for Args {
59 fn next_back(&mut self) -> Option<OsString> {