Skip to content

Commit 6deb42e

Browse files
committed
doc: make it easier to pick the correct tempfile constructor/type
1 parent e284782 commit 6deb42e

File tree

4 files changed

+22
-12
lines changed

4 files changed

+22
-12
lines changed

src/dir/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use crate::Builder;
2020
#[cfg(doc)]
2121
use crate::env;
2222

23-
/// Create a new temporary directory.
23+
/// Create a new temporary directory. Also see [`tempdir_in`].
2424
///
2525
/// The `tempdir` function creates a directory in the file system and returns a
2626
/// [`TempDir`]. The directory will be automatically deleted when the `TempDir`'s
@@ -67,7 +67,7 @@ pub fn tempdir() -> io::Result<TempDir> {
6767
TempDir::new()
6868
}
6969

70-
/// Create a new temporary directory in a specific directory.
70+
/// Create a new temporary directory in a specific directory. Also see [`tempdir`].
7171
///
7272
/// The `tempdir_in` function creates a directory in the specified directory
7373
/// and returns a [`TempDir`].

src/file/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use crate::Builder;
1919

2020
mod imp;
2121

22-
/// Create a new temporary file.
22+
/// Create a new temporary file. Also see [`tempfile_in`].
2323
///
2424
/// The file will be created in the location returned by [`env::temp_dir()`].
2525
///
@@ -52,7 +52,7 @@ pub fn tempfile() -> io::Result<File> {
5252
tempfile_in(env::temp_dir())
5353
}
5454

55-
/// Create a new temporary file in the specified directory.
55+
/// Create a new temporary file in the specified directory. Also see [`tempfile`].
5656
///
5757
/// # Security
5858
///

src/lib.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
1-
//! Temporary files and directories.
1+
//! This is a library for creating temporary files and directories that are automatically deleted
2+
//! when no longer referenced (i.e., on drop).
23
//!
3-
//! - Use the [`tempfile()`] function for temporary files
4-
//! - Use the [`tempdir()`] function for temporary directories.
4+
//! - Use [`tempfile()`] when you need a real [`std::fs::File`] but don't need to refer to it
5+
//! by-path.
6+
//! - Use [`NamedTempFile::new()`] when you need a _named_ temporary file that can be refered to its
7+
//! path.
8+
//! - Use [`tempdir()`] when you need a temporary directory that will be recursively deleted on drop.
9+
//! - Use [`spooled_tempfile()`] when you need an in-memory buffer that will ultimately be backed by
10+
//! a temporary file if it gets too large.
511
//!
612
//! # Design
713
//!
814
//! This crate provides several approaches to creating temporary files and directories.
915
//! [`tempfile()`] relies on the OS to remove the temporary file once the last handle is closed.
1016
//! [`TempDir`] and [`NamedTempFile`] both rely on Rust destructors for cleanup.
1117
//!
12-
//! When choosing between the temporary file variants, prefer `tempfile`
13-
//! unless you either need to know the file's path or to be able to persist it.
14-
//!
1518
//! ## Resource Leaking
1619
//!
1720
//! `tempfile` will (almost) never fail to cleanup temporary resources. However `TempDir` and

src/spooled.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,20 @@ pub struct SpooledTempFile {
2222
inner: SpooledData,
2323
}
2424

25-
/// Create a new spooled temporary file.
25+
/// Create a new [`SpooledTempFile`]. Also see [`spooled_tempfile_in`].
2626
///
2727
/// # Security
2828
///
2929
/// This variant is secure/reliable in the presence of a pathological temporary
3030
/// file cleaner.
3131
///
32+
/// # Backing Storage
33+
///
34+
/// By default, the underlying temporary file will be created in your operating system's temporary
35+
/// file directory which is _often_ an in-memory filesystem. You may want to consider using
36+
/// [`spooled_tempfile_in`] instead, passing a storage-backed filesystem (e.g., `/var/tmp` on
37+
/// Linux).
38+
///
3239
/// # Resource Leaking
3340
///
3441
/// The temporary file will be automatically removed by the OS when the last
@@ -60,7 +67,7 @@ pub fn spooled_tempfile(max_size: usize) -> SpooledTempFile {
6067

6168
/// Construct a new [`SpooledTempFile`], backed by a file in the specified directory. Use this when,
6269
/// e.g., you need the temporary file to be backed by a specific filesystem (e.g., when your default
63-
/// temporary directory is in-memory).
70+
/// temporary directory is in-memory). Also see [`spooled_tempfile`].
6471
///
6572
/// **NOTE:** The specified path isn't checked until the temporary file is "rolled over" into a real
6673
/// temporary file. If the specified directory isn't writable, writes to the temporary file will

0 commit comments

Comments
 (0)