summaryrefslogtreecommitdiffstats
path: root/tokio/src/fs/file.rs
diff options
context:
space:
mode:
authorAlice Ryhl <alice@ryhl.io>2020-04-24 00:11:49 +0200
committerGitHub <noreply@github.com>2020-04-23 15:11:49 -0700
commit9bcb50660e2d9de26a4373aaf4ab8f324c8ebbe8 (patch)
tree744803b55ae681aeae2f6ba02c3f939744bdce34 /tokio/src/fs/file.rs
parenta3aab864d776692bc53357b115c8b06d789c630b (diff)
docs: make it easier to discover extension traits (#2434)
Refs: #2307
Diffstat (limited to 'tokio/src/fs/file.rs')
-rw-r--r--tokio/src/fs/file.rs58
1 files changed, 52 insertions, 6 deletions
diff --git a/tokio/src/fs/file.rs b/tokio/src/fs/file.rs
index a1f22fc9..cc4a187d 100644
--- a/tokio/src/fs/file.rs
+++ b/tokio/src/fs/file.rs
@@ -24,12 +24,28 @@ use std::task::Poll::*;
/// Tokio runtime.
///
/// An instance of a `File` can be read and/or written depending on what options
-/// it was opened with. Files also implement Seek to alter the logical cursor
-/// that the file contains internally.
+/// it was opened with. Files also implement [`AsyncSeek`] to alter the logical
+/// cursor that the file contains internally.
///
-/// Files are automatically closed when they go out of scope.
+/// A file will not be closed immediately when it goes out of scope if there
+/// are any IO operations that have not yet completed. To ensure that a file is
+/// closed immediately when it is dropped, you should call [`flush`] before
+/// dropping it. Note that this does not ensure that the file has been fully
+/// written to disk; the operating system might keep the changes around in an
+/// in-memory buffer. See the [`sync_all`] method for telling the OS to write
+/// the data to disk.
///
-/// [std]: std::fs::File
+/// Reading and writing to a `File` is usually done using the convenience
+/// methods found on the [`AsyncReadExt`] and [`AsyncWriteExt`] traits. Examples
+/// import these traits through [the prelude].
+///
+/// [std]: struct@std::fs::File
+/// [`AsyncSeek`]: trait@crate::io::AsyncSeek
+/// [`flush`]: fn@crate::io::AsyncWriteExt::flush
+/// [`sync_all`]: fn@crate::fs::File::sync_all
+/// [`AsyncReadExt`]: trait@crate::io::AsyncReadExt
+/// [`AsyncWriteExt`]: trait@crate::io::AsyncWriteExt
+/// [the prelude]: crate::prelude
///
/// # Examples
///
@@ -37,7 +53,7 @@ use std::task::Poll::*;
///
/// ```no_run
/// use tokio::fs::File;
-/// use tokio::prelude::*;
+/// use tokio::prelude::*; // for write_all()
///
/// # async fn dox() -> std::io::Result<()> {
/// let mut file = File::create("foo.txt").await?;
@@ -50,7 +66,7 @@ use std::task::Poll::*;
///
/// ```no_run
/// use tokio::fs::File;
-/// use tokio::prelude::*;
+/// use tokio::prelude::*; // for read_to_end()
///
/// # async fn dox() -> std::io::Result<()> {
/// let mut file = File::open("foo.txt").await?;
@@ -114,6 +130,11 @@ impl File {
/// # Ok(())
/// # }
/// ```
+ ///
+ /// The [`read_to_end`] method is defined on the [`AsyncReadExt`] trait.
+ ///
+ /// [`read_to_end`]: fn@crate::io::AsyncReadExt::read_to_end
+ /// [`AsyncReadExt`]: trait@crate::io::AsyncReadExt
pub async fn open(path: impl AsRef<Path>) -> io::Result<File> {
let path = path.as_ref().to_owned();
let std = asyncify(|| sys::File::open(path)).await?;
@@ -149,6 +170,11 @@ impl File {
/// # Ok(())
/// # }
/// ```
+ ///
+ /// The [`write_all`] method is defined on the [`AsyncWriteExt`] trait.
+ ///
+ /// [`write_all`]: fn@crate::io::AsyncWriteExt::write_all
+ /// [`AsyncWriteExt`]: trait@crate::io::AsyncWriteExt
pub async fn create(path: impl AsRef<Path>) -> io::Result<File> {
let path = path.as_ref().to_owned();
let std_file = asyncify(move || sys::File::create(path)).await?;
@@ -195,6 +221,11 @@ impl File {
/// # Ok(())
/// # }
/// ```
+ ///
+ /// The [`read_exact`] method is defined on the [`AsyncReadExt`] trait.
+ ///
+ /// [`read_exact`]: fn@crate::io::AsyncReadExt::read_exact
+ /// [`AsyncReadExt`]: trait@crate::io::AsyncReadExt
pub async fn seek(&mut self, mut pos: SeekFrom) -> io::Result<u64> {
self.complete_inflight().await;
@@ -251,6 +282,11 @@ impl File {
/// # Ok(())
/// # }
/// ```
+ ///
+ /// The [`write_all`] method is defined on the [`AsyncWriteExt`] trait.
+ ///
+ /// [`write_all`]: fn@crate::io::AsyncWriteExt::write_all
+ /// [`AsyncWriteExt`]: trait@crate::io::AsyncWriteExt
pub async fn sync_all(&mut self) -> io::Result<()> {
self.complete_inflight().await;
@@ -280,6 +316,11 @@ impl File {
/// # Ok(())
/// # }
/// ```
+ ///
+ /// The [`write_all`] method is defined on the [`AsyncWriteExt`] trait.
+ ///
+ /// [`write_all`]: fn@crate::io::AsyncWriteExt::write_all
+ /// [`AsyncWriteExt`]: trait@crate::io::AsyncWriteExt
pub async fn sync_data(&mut self) -> io::Result<()> {
self.complete_inflight().await;
@@ -312,6 +353,11 @@ impl File {
/// # Ok(())
/// # }
/// ```
+ ///
+ /// The [`write_all`] method is defined on the [`AsyncWriteExt`] trait.
+ ///
+ /// [`write_all`]: fn@crate::io::AsyncWriteExt::write_all
+ /// [`AsyncWriteExt`]: trait@crate::io::AsyncWriteExt
pub async fn set_len(&mut self, size: u64) -> io::Result<()> {
self.complete_inflight().await;