@@ -11,6 +11,7 @@ use super::platform::fs::MetadataExt as _;
11
11
// Used for `File::read` on intra-doc links
12
12
use crate :: ffi:: OsStr ;
13
13
use crate :: fs:: { self , OpenOptions , Permissions } ;
14
+ use crate :: io:: BorrowedCursor ;
14
15
use crate :: os:: unix:: io:: { AsFd , AsRawFd } ;
15
16
use crate :: path:: Path ;
16
17
use crate :: sealed:: Sealed ;
@@ -130,6 +131,42 @@ pub trait FileExt {
130
131
if !buf. is_empty ( ) { Err ( io:: Error :: READ_EXACT_EOF ) } else { Ok ( ( ) ) }
131
132
}
132
133
134
+ /// Reads some bytes starting from a given offset into the buffer.
135
+ ///
136
+ /// This equivalent to the [`read_at`](FileExt::read_at) method,
137
+ /// except that it is passed a [`BorrowedCursor`] rather than `&mut [u8]` to allow
138
+ /// use with uninitialized buffers. The new data will be appended to any
139
+ /// existing contents of `buf`.
140
+ #[ unstable( feature = "read_buf" , issue = "78485" ) ]
141
+ fn read_buf_at ( & self , buf : BorrowedCursor < ' _ > , offset : u64 ) -> io:: Result < ( ) > {
142
+ io:: default_read_buf ( |b| self . read_at ( b, offset) , buf)
143
+ }
144
+
145
+ /// Reads the exact number of bytes required to fill the buffer from a given
146
+ /// offset.
147
+ ///
148
+ /// This is equivalent to the [`read_exact_at`](FileExt::read_exact_at) method,
149
+ /// except that it is passed a [`BorrowedCursor`] rather than `&mut [u8]` to allow
150
+ /// use with uninitialized buffers. The new data will be appended to any
151
+ /// existing contents of `buf`.
152
+ #[ unstable( feature = "read_buf" , issue = "78485" ) ]
153
+ fn read_buf_exact_at ( & self , mut buf : BorrowedCursor < ' _ > , mut offset : u64 ) -> io:: Result < ( ) > {
154
+ while buf. capacity ( ) > 0 {
155
+ let prev_written = buf. written ( ) ;
156
+ match self . read_buf_at ( buf. reborrow ( ) , offset) {
157
+ Ok ( ( ) ) => { }
158
+ Err ( e) if e. is_interrupted ( ) => { }
159
+ Err ( e) => return Err ( e) ,
160
+ }
161
+ let n = buf. written ( ) - prev_written;
162
+ offset += n as u64 ;
163
+ if n == 0 {
164
+ return Err ( io:: Error :: READ_EXACT_EOF ) ;
165
+ }
166
+ }
167
+ Ok ( ( ) )
168
+ }
169
+
133
170
/// Writes a number of bytes starting from a given offset.
134
171
///
135
172
/// Returns the number of bytes written.
@@ -264,6 +301,9 @@ impl FileExt for fs::File {
264
301
fn read_at ( & self , buf : & mut [ u8 ] , offset : u64 ) -> io:: Result < usize > {
265
302
self . as_inner ( ) . read_at ( buf, offset)
266
303
}
304
+ fn read_buf_at ( & self , buf : BorrowedCursor < ' _ > , offset : u64 ) -> io:: Result < ( ) > {
305
+ self . as_inner ( ) . read_buf_at ( buf, offset)
306
+ }
267
307
fn read_vectored_at ( & self , bufs : & mut [ io:: IoSliceMut < ' _ > ] , offset : u64 ) -> io:: Result < usize > {
268
308
self . as_inner ( ) . read_vectored_at ( bufs, offset)
269
309
}
0 commit comments