Skip to content

Commit 58fc61b

Browse files
committed
Make WASI's FileExt's read_at/write_at consistent with other targets.
Rename the existing read_at/write_at to read_vectored_at/write_vectored_at, for consistency with libstd's read_vectored/write_vectored. And, introduce new read_at/write_at functions which take a single buffer, similar to all other targets which provide these functions, so this will make it easier for applications to share code between WASI and other targets. Note that WASI's FileExt is currently unstable.
1 parent 653c091 commit 58fc61b

File tree

1 file changed

+43
-4
lines changed
  • src/libstd/sys/wasi/ext

1 file changed

+43
-4
lines changed

Diff for: src/libstd/sys/wasi/ext/fs.rs

+43-4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,24 @@ use crate::sys_common::{AsInner, AsInnerMut, FromInner};
1212
///
1313
/// [`File`]: ../../../../std/fs/struct.File.html
1414
pub trait FileExt {
15+
/// Reads a number of bytes starting from a given offset.
16+
///
17+
/// Returns the number of bytes read.
18+
///
19+
/// The offset is relative to the start of the file and thus independent
20+
/// from the current cursor.
21+
///
22+
/// The current file cursor is not affected by this function.
23+
///
24+
/// Note that similar to [`File::read`], it is not an error to return with a
25+
/// short read.
26+
///
27+
/// [`File::read`]: ../../../../std/fs/struct.File.html#method.read
28+
fn read_at(&self, buf: &mut [u8], offset: u64) -> io::Result<usize> {
29+
let bufs = &mut [IoSliceMut::new(buf)];
30+
self.read_vectored_at(bufs, offset)
31+
}
32+
1533
/// Reads a number of bytes starting from a given offset.
1634
///
1735
/// Returns the number of bytes read.
@@ -25,7 +43,7 @@ pub trait FileExt {
2543
/// return with a short read.
2644
///
2745
/// [`File::read`]: ../../../../std/fs/struct.File.html#method.read_vectored
28-
fn read_at(&self, bufs: &mut [IoSliceMut<'_>], offset: u64) -> io::Result<usize>;
46+
fn read_vectored_at(&self, bufs: &mut [IoSliceMut<'_>], offset: u64) -> io::Result<usize>;
2947

3048
/// Reads the exact number of byte required to fill `buf` from the given offset.
3149
///
@@ -79,6 +97,27 @@ pub trait FileExt {
7997
}
8098
}
8199

100+
/// Writes a number of bytes starting from a given offset.
101+
///
102+
/// Returns the number of bytes written.
103+
///
104+
/// The offset is relative to the start of the file and thus independent
105+
/// from the current cursor.
106+
///
107+
/// The current file cursor is not affected by this function.
108+
///
109+
/// When writing beyond the end of the file, the file is appropriately
110+
/// extended and the intermediate bytes are initialized with the value 0.
111+
///
112+
/// Note that similar to [`File::write`], it is not an error to return a
113+
/// short write.
114+
///
115+
/// [`File::write`]: ../../../../std/fs/struct.File.html#write.v
116+
fn write_at(&self, buf: &[u8], offset: u64) -> io::Result<usize> {
117+
let bufs = &[IoSlice::new(buf)];
118+
self.write_vectored_at(bufs, offset)
119+
}
120+
82121
/// Writes a number of bytes starting from a given offset.
83122
///
84123
/// Returns the number of bytes written.
@@ -95,7 +134,7 @@ pub trait FileExt {
95134
/// short write.
96135
///
97136
/// [`File::write`]: ../../../../std/fs/struct.File.html#method.write_vectored
98-
fn write_at(&self, bufs: &[IoSlice<'_>], offset: u64) -> io::Result<usize>;
137+
fn write_vectored_at(&self, bufs: &[IoSlice<'_>], offset: u64) -> io::Result<usize>;
99138

100139
/// Attempts to write an entire buffer starting from a given offset.
101140
///
@@ -199,11 +238,11 @@ pub trait FileExt {
199238
// FIXME: bind random_get maybe? - on crates.io for unix
200239

201240
impl FileExt for fs::File {
202-
fn read_at(&self, bufs: &mut [IoSliceMut<'_>], offset: u64) -> io::Result<usize> {
241+
fn read_vectored_at(&self, bufs: &mut [IoSliceMut<'_>], offset: u64) -> io::Result<usize> {
203242
self.as_inner().fd().pread(bufs, offset)
204243
}
205244

206-
fn write_at(&self, bufs: &[IoSlice<'_>], offset: u64) -> io::Result<usize> {
245+
fn write_vectored_at(&self, bufs: &[IoSlice<'_>], offset: u64) -> io::Result<usize> {
207246
self.as_inner().fd().pwrite(bufs, offset)
208247
}
209248

0 commit comments

Comments
 (0)