@@ -12,6 +12,24 @@ use crate::sys_common::{AsInner, AsInnerMut, FromInner};
12
12
///
13
13
/// [`File`]: ../../../../std/fs/struct.File.html
14
14
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
+
15
33
/// Reads a number of bytes starting from a given offset.
16
34
///
17
35
/// Returns the number of bytes read.
@@ -25,7 +43,7 @@ pub trait FileExt {
25
43
/// return with a short read.
26
44
///
27
45
/// [`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 > ;
29
47
30
48
/// Reads the exact number of byte required to fill `buf` from the given offset.
31
49
///
@@ -79,6 +97,27 @@ pub trait FileExt {
79
97
}
80
98
}
81
99
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
+
82
121
/// Writes a number of bytes starting from a given offset.
83
122
///
84
123
/// Returns the number of bytes written.
@@ -95,7 +134,7 @@ pub trait FileExt {
95
134
/// short write.
96
135
///
97
136
/// [`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 > ;
99
138
100
139
/// Attempts to write an entire buffer starting from a given offset.
101
140
///
@@ -199,11 +238,11 @@ pub trait FileExt {
199
238
// FIXME: bind random_get maybe? - on crates.io for unix
200
239
201
240
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 > {
203
242
self . as_inner ( ) . fd ( ) . pread ( bufs, offset)
204
243
}
205
244
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 > {
207
246
self . as_inner ( ) . fd ( ) . pwrite ( bufs, offset)
208
247
}
209
248
0 commit comments