@@ -11,6 +11,7 @@ use super::platform::fs::MetadataExt as _;
1111// Used for `File::read` on intra-doc links
1212use crate :: ffi:: OsStr ;
1313use crate :: fs:: { self , OpenOptions , Permissions } ;
14+ use crate :: io:: BorrowedCursor ;
1415use crate :: os:: unix:: io:: { AsFd , AsRawFd } ;
1516use crate :: path:: Path ;
1617use crate :: sealed:: Sealed ;
@@ -130,6 +131,42 @@ pub trait FileExt {
130131 if !buf. is_empty ( ) { Err ( io:: Error :: READ_EXACT_EOF ) } else { Ok ( ( ) ) }
131132 }
132133
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+
133170 /// Writes a number of bytes starting from a given offset.
134171 ///
135172 /// Returns the number of bytes written.
@@ -264,6 +301,9 @@ impl FileExt for fs::File {
264301 fn read_at ( & self , buf : & mut [ u8 ] , offset : u64 ) -> io:: Result < usize > {
265302 self . as_inner ( ) . read_at ( buf, offset)
266303 }
304+ fn read_buf_at ( & self , buf : BorrowedCursor < ' _ > , offset : u64 ) -> io:: Result < ( ) > {
305+ self . as_inner ( ) . read_buf_at ( buf, offset)
306+ }
267307 fn read_vectored_at ( & self , bufs : & mut [ io:: IoSliceMut < ' _ > ] , offset : u64 ) -> io:: Result < usize > {
268308 self . as_inner ( ) . read_vectored_at ( bufs, offset)
269309 }
0 commit comments