Skip to content

Add missing links and examples for FileExt #45631

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

Merged
merged 1 commit into from
Nov 11, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 40 additions & 2 deletions src/libstd/sys/unix/ext/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,28 @@ pub trait FileExt {
///
/// 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
/// Note that similar to [`File::read`], it is not an error to return with a
/// short read.
///
/// [`File::read`]: ../../../../std/fs/struct.File.html#method.read
///
/// # Examples
///
/// ```
/// use std::os::unix::prelude::FileExt;
/// use std::fs::File;
///
/// # use std::io;
/// # fn f() -> io::Result<()> {
/// let mut buf = [0u8; 8];
/// let file = File::open("foo.txt")?;
///
/// // We now read 8 bytes from the offset 10.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not guaranteed to read 8 bytes, so phrasing it as 'up to 8 bytes' is a bit more accurate

/// let num_bytes_read = file.read_at(&mut buf, 10)?;
/// println!("read {} bytes: {:?}", num_bytes_read, buf);
/// # Ok(())
/// # }
/// ```
#[stable(feature = "file_offset", since = "1.15.0")]
fn read_at(&self, buf: &mut [u8], offset: u64) -> io::Result<usize>;

Expand All @@ -51,8 +71,26 @@ pub trait FileExt {
/// When writing beyond the end of the file, the file is appropriately
/// 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
/// Note that similar to [`File::write`], it is not an error to return a
/// short write.
///
/// [`File::write`]: ../../../../std/fs/struct.File.html#write.v
///
/// # Examples
///
/// ```
/// use std::os::unix::prelude::FileExt;
/// use std::fs::File;
///
/// # use std::io;
/// # fn f() -> io::Result<()> {
/// let file = File::open("foo.txt")?;
///
/// // We now write at the offset 10.
/// file.write_at(b"sushi", 10)?;
/// # Ok(())
/// # }
/// ```
#[stable(feature = "file_offset", since = "1.15.0")]
fn write_at(&self, buf: &[u8], offset: u64) -> io::Result<usize>;
}
Expand Down