-
Notifications
You must be signed in to change notification settings - Fork 13k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement read_offset
and write_offset
#35704
Changes from all commits
b3f2644
f352f0e
70dcfd6
2eda01e
744aecf
94aa08b
1554993
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,51 @@ use sys; | |
use sys_common::{FromInner, AsInner, AsInnerMut}; | ||
use sys::platform::fs::MetadataExt as UnixMetadataExt; | ||
|
||
/// Unix-specific extensions to `File` | ||
#[unstable(feature = "file_offset", issue = "35918")] | ||
pub trait FileExt { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you add this to the prelude as well? (as well as the windows prelude) |
||
/// Reads a number of bytes starting from a given offset. | ||
/// | ||
/// Returns the number of bytes read. | ||
/// | ||
/// The offset is relative to the start of the file and thus independent | ||
/// from the current cursor. | ||
/// | ||
/// The current file cursor is not affected by this function. | ||
/// | ||
/// Note that similar to `File::read`, it is not an error to return with a | ||
/// short read. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could these docs also clarify what happens if you read beyond the end of a file? E.g. if a file is 10 bytes long what do these return:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (ping on this) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not really sure what I can write there. It'll return with an empty buffer. |
||
#[unstable(feature = "file_offset", issue = "35918")] | ||
fn read_at(&self, buf: &mut [u8], offset: u64) -> io::Result<usize>; | ||
|
||
/// Writes a number of bytes starting from a given offset. | ||
/// | ||
/// Returns the number of bytes written. | ||
/// | ||
/// The offset is relative to the start of the file and thus independent | ||
/// from the current cursor. | ||
/// | ||
/// The current file cursor is not affected by this function. | ||
/// | ||
/// When writing beyond the end of the file, the file is appropiately | ||
/// extended and the intermediate bytes are initialized with the value 0. | ||
/// | ||
/// Note that similar to `File::write`, it is not an error to return a | ||
/// short write. | ||
#[unstable(feature = "file_offset", issue = "35918")] | ||
fn write_at(&self, buf: &[u8], offset: u64) -> io::Result<usize>; | ||
} | ||
|
||
#[unstable(feature = "file_offset", issue = "35918")] | ||
impl FileExt for fs::File { | ||
fn read_at(&self, buf: &mut [u8], offset: u64) -> io::Result<usize> { | ||
self.as_inner().read_at(buf, offset) | ||
} | ||
fn write_at(&self, buf: &[u8], offset: u64) -> io::Result<usize> { | ||
self.as_inner().write_at(buf, offset) | ||
} | ||
} | ||
|
||
/// Unix-specific extensions to `Permissions` | ||
#[stable(feature = "fs_ext", since = "1.1.0")] | ||
pub trait PermissionsExt { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could this also assert that the first 5 bytes were filled in with 0s?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(same below for windows)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done for Unix. On Windows, these bytes are left in an unspecified state.