Skip to content

Commit a47e3c0

Browse files
committed
fixes
1 parent 88fd945 commit a47e3c0

File tree

11 files changed

+69
-74
lines changed

11 files changed

+69
-74
lines changed

src/libstd/sys/wasi/args.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
use crate::ffi::CStr;
2-
use crate::io;
31
use crate::ffi::OsString;
42
use crate::marker::PhantomData;
53
use crate::os::wasi::ffi::OsStringExt;
64
use crate::vec;
75

6+
use ::wasi::wasi_unstable as wasi;
7+
88
pub unsafe fn init(_argc: isize, _argv: *const *const u8) {
99
}
1010

@@ -20,7 +20,7 @@ pub struct Args {
2020
pub fn args() -> Args {
2121
let buf = wasi::args_sizes_get().and_then(|args_sizes| {
2222
let mut buf = Vec::with_capacity(args_sizes.get_count());
23-
wasi::get_args(args_sizes, |arg| {
23+
wasi::args_get(args_sizes, |arg| {
2424
let arg = OsString::from_vec(arg.to_vec());
2525
buf.push(arg);
2626
})?;

src/libstd/sys/wasi/ext/fs.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ use crate::os::wasi::ffi::OsStrExt;
88
use crate::path::{Path, PathBuf};
99
use crate::sys_common::{AsInner, AsInnerMut, FromInner};
1010

11+
use ::wasi::wasi_unstable as wasi;
12+
1113
/// WASI-specific extensions to [`File`].
1214
///
1315
/// [`File`]: ../../../../std/fs/struct.File.html

src/libstd/sys/wasi/ext/io.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::sys;
88
use crate::net;
99
use crate::sys_common::{AsInner, FromInner, IntoInner};
1010

11-
use wasi::wasi_unstable as wasi;
11+
use ::wasi::wasi_unstable as wasi;
1212

1313
/// Raw file descriptors.
1414
pub type RawFd = u32;

src/libstd/sys/wasi/fd.rs

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
use crate::io::{self, IoSlice, IoSliceMut, SeekFrom};
44
use crate::mem;
55
use crate::net::Shutdown;
6-
use wasi::wasi_unstable as wasi;
6+
use super::err2io;
7+
use ::wasi::wasi_unstable as wasi;
78

89
#[derive(Debug)]
910
pub struct WasiFd {
1011
fd: wasi::Fd,
1112
}
1213

13-
fn iovec(a: &mut [IoSliceMut<'_>]) -> &[wasi::IoVec] {
14+
fn iovec<'a>(a: &'a mut [IoSliceMut<'_>]) -> &'a [wasi::IoVec] {
1415
assert_eq!(
1516
mem::size_of::<IoSliceMut<'_>>(),
1617
mem::size_of::<wasi::IoVec>()
@@ -23,7 +24,7 @@ fn iovec(a: &mut [IoSliceMut<'_>]) -> &[wasi::IoVec] {
2324
unsafe { mem::transmute(a) }
2425
}
2526

26-
fn ciovec(a: &[IoSlice<'_>]) -> &[wasi::CIoVec] {
27+
fn ciovec<'a>(a: &'a [IoSlice<'_>]) -> &'a [wasi::CIoVec] {
2728
assert_eq!(
2829
mem::size_of::<IoSlice<'_>>(),
2930
mem::size_of::<wasi::CIoVec>()
@@ -52,23 +53,23 @@ impl WasiFd {
5253
}
5354

5455
pub fn datasync(&self) -> io::Result<()> {
55-
wasi::fd_datasync(self.fd).map_err(From::from)
56+
wasi::fd_datasync(self.fd).map_err(err2io)
5657
}
5758

5859
pub fn pread(&self, bufs: &mut [IoSliceMut<'_>], offset: u64) -> io::Result<usize> {
59-
wasi::fd_pread(self.fd, iovec(bufs), offset).map_err(From::from)
60+
wasi::fd_pread(self.fd, iovec(bufs), offset).map_err(err2io)
6061
}
6162

6263
pub fn pwrite(&self, bufs: &[IoSlice<'_>], offset: u64) -> io::Result<usize> {
63-
wasi::fd_pwrite(self.fd, ciovec(bufs), offset).map_err(From::from)
64+
wasi::fd_pwrite(self.fd, ciovec(bufs), offset).map_err(err2io)
6465
}
6566

6667
pub fn read(&self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
67-
wasi::fd_read(self.fd, iovec(bufs)).map_err(From::from)
68+
wasi::fd_read(self.fd, iovec(bufs)).map_err(err2io)
6869
}
6970

7071
pub fn write(&self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
71-
wasi::fd_write(self.fd, ciovec(bufs)).map_err(From::from)
72+
wasi::fd_write(self.fd, ciovec(bufs)).map_err(err2io)
7273
}
7374

7475
pub fn seek(&self, pos: SeekFrom) -> io::Result<u64> {
@@ -77,37 +78,37 @@ impl WasiFd {
7778
SeekFrom::End(pos) => (wasi::WHENCE_END, pos),
7879
SeekFrom::Current(pos) => (wasi::WHENCE_CUR, pos),
7980
};
80-
wasi::fd_seek(self.fd, offset, whence).map_err(From::from)
81+
wasi::fd_seek(self.fd, offset, whence).map_err(err2io)
8182
}
8283

8384
pub fn tell(&self) -> io::Result<u64> {
84-
wasi::fd_tell(self.fd).map_err(From::from)
85+
wasi::fd_tell(self.fd).map_err(err2io)
8586
}
8687

8788
// FIXME: __wasi_fd_fdstat_get
8889

8990
pub fn set_flags(&self, flags: wasi::FdFlags) -> io::Result<()> {
90-
wasi::fd_fdstat_set_flags(self.fd, flags).map_err(From::from)
91+
wasi::fd_fdstat_set_flags(self.fd, flags).map_err(err2io)
9192
}
9293

9394
pub fn set_rights(&self, base: wasi::Rights, inheriting: wasi::Rights) -> io::Result<()> {
94-
wasi::fd_fdstat_set_rights(self.fd, base, inheriting).map_err(From::from)
95+
wasi::fd_fdstat_set_rights(self.fd, base, inheriting).map_err(err2io)
9596
}
9697

9798
pub fn sync(&self) -> io::Result<()> {
98-
wasi::fd_sync(self.fd).map_err(From::from)
99+
wasi::fd_sync(self.fd).map_err(err2io)
99100
}
100101

101102
pub fn advise(&self, offset: u64, len: u64, advice: wasi::Advice) -> io::Result<()> {
102-
wasi::fd_advise(self.fd, offset, len, advice).map_err(From::from)
103+
wasi::fd_advise(self.fd, offset, len, advice).map_err(err2io)
103104
}
104105

105106
pub fn allocate(&self, offset: u64, len: u64) -> io::Result<()> {
106-
wasi::fd_allocate(self.fd, offset, len).map_err(From::from)
107+
wasi::fd_allocate(self.fd, offset, len).map_err(err2io)
107108
}
108109

109110
pub fn create_directory(&self, path: &[u8]) -> io::Result<()> {
110-
wasi::path_create_directory(self.fd, path).map_err(From::from)
111+
wasi::path_create_directory(self.fd, path).map_err(err2io)
111112
}
112113

113114
pub fn link(
@@ -118,7 +119,7 @@ impl WasiFd {
118119
new_path: &[u8],
119120
) -> io::Result<()> {
120121
wasi::path_link(self.fd, old_flags, old_path, new_fd.fd, new_path)
121-
.map_err(From::from)
122+
.map_err(err2io)
122123
}
123124

124125
pub fn open(
@@ -130,33 +131,32 @@ impl WasiFd {
130131
fs_rights_inheriting: wasi::Rights,
131132
fs_flags: wasi::FdFlags,
132133
) -> io::Result<WasiFd> {
133-
let fd = wasi_path_open(
134+
wasi::path_open(
134135
self.fd,
135136
dirflags,
136137
path,
137138
oflags,
138139
fs_rights_base,
139140
fs_rights_inheriting,
140141
fs_flags,
141-
)?;
142-
Ok(WasiFd::from_raw(fd))
142+
).map(|fd| unsafe { WasiFd::from_raw(fd) }).map_err(err2io)
143143
}
144144

145145
pub fn readdir(&self, buf: &mut [u8], cookie: wasi::DirCookie) -> io::Result<usize> {
146-
wasi::fd_readdir(self.fd, buf, cookie).map_err(From::from)
146+
wasi::fd_readdir(self.fd, buf, cookie).map_err(err2io)
147147
}
148148

149149
pub fn readlink(&self, path: &[u8], buf: &mut [u8]) -> io::Result<usize> {
150-
wasi::path_readlink(self.fd, path, buf).map_err(From::from)
150+
wasi::path_readlink(self.fd, path, buf).map_err(err2io)
151151
}
152152

153153
pub fn rename(&self, old_path: &[u8], new_fd: &WasiFd, new_path: &[u8]) -> io::Result<()> {
154154
wasi::path_rename(self.fd, old_path, new_fd.fd, new_path)
155-
.map_err(From::from)
155+
.map_err(err2io)
156156
}
157157

158-
pub fn filestat_get(&self) -> io::Result<wasi::Filestat> {
159-
wasi::fd_filestat_get(self.fd, buf).map_err(From::from)
158+
pub fn filestat_get(&self) -> io::Result<wasi::FileStat> {
159+
wasi::fd_filestat_get(self.fd).map_err(err2io)
160160
}
161161

162162
pub fn filestat_set_times(
@@ -166,19 +166,19 @@ impl WasiFd {
166166
fstflags: wasi::FstFlags,
167167
) -> io::Result<()> {
168168
wasi::fd_filestat_set_times(self.fd, atim, mtim, fstflags)
169-
.map_err(From::from)
169+
.map_err(err2io)
170170
}
171171

172172
pub fn filestat_set_size(&self, size: u64) -> io::Result<()> {
173-
wasi::fd_filestat_set_size(self.fd, size).map_err(From::from)
173+
wasi::fd_filestat_set_size(self.fd, size).map_err(err2io)
174174
}
175175

176176
pub fn path_filestat_get(
177177
&self,
178178
flags: wasi::LookupFlags,
179179
path: &[u8],
180180
) -> io::Result<wasi::FileStat> {
181-
wasi::path_filestat_get(self.fd, flags, path).map_err(From::from)
181+
wasi::path_filestat_get(self.fd, flags, path).map_err(err2io)
182182
}
183183

184184
pub fn path_filestat_set_times(
@@ -196,40 +196,40 @@ impl WasiFd {
196196
atim,
197197
mtim,
198198
fstflags,
199-
).map_err(From::from)
199+
).map_err(err2io)
200200
}
201201

202202
pub fn symlink(&self, old_path: &[u8], new_path: &[u8]) -> io::Result<()> {
203-
wasi::path_symlink(old_path, self.fd, new_path).map_err(From::from)
203+
wasi::path_symlink(old_path, self.fd, new_path).map_err(err2io)
204204
}
205205

206206
pub fn unlink_file(&self, path: &[u8]) -> io::Result<()> {
207-
wasi::path_unlink_file(self.fd, path).map_err(From::from)
207+
wasi::path_unlink_file(self.fd, path).map_err(err2io)
208208
}
209209

210210
pub fn remove_directory(&self, path: &[u8]) -> io::Result<()> {
211-
wasi::path_remove_directory(self.fd, path).map_err(From::from)
211+
wasi::path_remove_directory(self.fd, path).map_err(err2io)
212212
}
213213

214214
pub fn sock_recv(
215215
&self,
216216
ri_data: &mut [IoSliceMut<'_>],
217217
ri_flags: wasi::RiFlags,
218218
) -> io::Result<(usize, wasi::RoFlags)> {
219-
wasi::sock_recv(self.fd, iovec(ri_data), ri_flags).map_err(From::from)
219+
wasi::sock_recv(self.fd, iovec(ri_data), ri_flags).map_err(err2io)
220220
}
221221

222222
pub fn sock_send(&self, si_data: &[IoSlice<'_>], si_flags: wasi::SiFlags) -> io::Result<usize> {
223-
wasi::sock_send(self.fd, ciovec(si_data), si_flags).map_err(From::from)
223+
wasi::sock_send(self.fd, ciovec(si_data), si_flags).map_err(err2io)
224224
}
225225

226226
pub fn sock_shutdown(&self, how: Shutdown) -> io::Result<()> {
227227
let how = match how {
228-
Shutdown::Read => WASI::SHUT_RD,
229-
Shutdown::Write => WASI::SHUT_WR,
230-
Shutdown::Both => WASI::SHUT_WR | WASI::SHUT_RD,
228+
Shutdown::Read => wasi::SHUT_RD,
229+
Shutdown::Write => wasi::SHUT_WR,
230+
Shutdown::Both => wasi::SHUT_WR | wasi::SHUT_RD,
231231
};
232-
wasi::sock_shutdown(self.fd, how).map_err(From::from)
232+
wasi::sock_shutdown(self.fd, how).map_err(err2io)
233233
}
234234
}
235235

src/libstd/sys/wasi/fs.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ use crate::os::wasi::ffi::{OsStrExt, OsStringExt};
77
use crate::path::{Path, PathBuf};
88
use crate::ptr;
99
use crate::sync::Arc;
10-
use crate::sys::fd::{DirCookie, WasiFd};
10+
use crate::sys::fd::WasiFd;
1111
use crate::sys::time::SystemTime;
1212
use crate::sys::unsupported;
1313
use crate::sys_common::FromInner;
1414

1515
pub use crate::sys_common::fs::copy;
1616
pub use crate::sys_common::fs::remove_dir_all;
1717

18-
use wasi::wasi_unstable as wasi;
18+
use ::wasi::wasi_unstable as wasi;
1919

2020
pub struct File {
2121
fd: WasiFd,
@@ -28,7 +28,7 @@ pub struct FileAttr {
2828

2929
pub struct ReadDir {
3030
inner: Arc<ReadDirInner>,
31-
cookie: Option<DirCookie>,
31+
cookie: Option<wasi::DirCookie>,
3232
buf: Vec<u8>,
3333
offset: usize,
3434
cap: usize,
@@ -70,12 +70,6 @@ pub struct FileType {
7070
pub struct DirBuilder {}
7171

7272
impl FileAttr {
73-
fn zero() -> FileAttr {
74-
FileAttr {
75-
meta: unsafe { mem::zeroed() },
76-
}
77-
}
78-
7973
pub fn size(&self) -> u64 {
8074
self.meta.st_size
8175
}
@@ -390,7 +384,7 @@ impl File {
390384
}
391385

392386
pub fn file_attr(&self) -> io::Result<FileAttr> {
393-
self.fd.filestat_get().map_ok(|meta| FileAttr { meta })
387+
self.fd.filestat_get().map(|meta| FileAttr { meta })
394388
}
395389

396390
pub fn metadata_at(
@@ -601,7 +595,7 @@ fn metadata_at(
601595
path: &Path,
602596
) -> io::Result<FileAttr> {
603597
fd.path_filestat_get(flags, path.as_os_str().as_bytes())
604-
.map_ok(|meta| FileAttr { meta })
598+
.map(|meta| FileAttr { meta })
605599
}
606600

607601
pub fn canonicalize(_p: &Path) -> io::Result<PathBuf> {

src/libstd/sys/wasi/io.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::marker::PhantomData;
22
use crate::slice;
33

4-
use wasi::wasi_unstable as wasi;
4+
use ::wasi::wasi_unstable as wasi;
55
use core::ffi::c_void;
66

77
#[repr(transparent)]

src/libstd/sys/wasi/mod.rs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414
//! compiling for wasm. That way it's a compile time error for something that's
1515
//! guaranteed to be a runtime error!
1616
17-
use crate::io;
17+
use crate::io as std_io;
1818
use crate::mem;
1919
use crate::os::raw::c_char;
20-
use wasi::wasi_unstable as wasi;
20+
use ::wasi::wasi_unstable as wasi;
2121

2222
pub mod alloc;
2323
pub mod args;
@@ -56,16 +56,19 @@ pub mod ext;
5656
pub fn init() {
5757
}
5858

59-
pub fn unsupported<T>() -> crate::io::Result<T> {
59+
pub fn unsupported<T>() -> std_io::Result<T> {
6060
Err(unsupported_err())
6161
}
6262

63-
pub fn unsupported_err() -> io::Error {
64-
io::Error::new(io::ErrorKind::Other, "operation not supported on wasm yet")
63+
pub fn unsupported_err() -> std_io::Error {
64+
std_io::Error::new(
65+
std_io::ErrorKind::Other,
66+
"operation not supported on wasm yet",
67+
)
6568
}
6669

67-
pub fn decode_error_kind(_code: i32) -> io::ErrorKind {
68-
io::ErrorKind::Other
70+
pub fn decode_error_kind(_code: i32) -> std_io::ErrorKind {
71+
std_io::ErrorKind::Other
6972
}
7073

7174
// This enum is used as the storage for a bunch of types which can't actually
@@ -114,16 +117,14 @@ macro_rules! impl_is_minus_one {
114117

115118
impl_is_minus_one! { i8 i16 i32 i64 isize }
116119

117-
pub fn cvt<T: IsMinusOne>(t: T) -> crate::io::Result<T> {
120+
pub fn cvt<T: IsMinusOne>(t: T) -> std_io::Result<T> {
118121
if t.is_minus_one() {
119-
Err(io::Error::last_os_error())
122+
Err(std_io::Error::last_os_error())
120123
} else {
121124
Ok(t)
122125
}
123126
}
124127

125-
impl From<wasi::Error> for io::Error {
126-
fn from(err: wasi::Error) -> Self {
127-
Self::from_raw_os_error(err as i32)
128-
}
128+
fn err2io(err: wasi::Error) -> std_io::Error {
129+
std_io::Error::from_raw_os_error(err.get() as i32)
129130
}

0 commit comments

Comments
 (0)