mirror of
https://github.com/quickwit-oss/tantivy.git
synced 2026-05-21 10:40:41 +00:00
71 lines
12 KiB
HTML
71 lines
12 KiB
HTML
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="API documentation for the Rust `tempfile` crate."><meta name="keywords" content="rust, rustlang, rust-lang, tempfile"><title>tempfile - Rust</title><link rel="stylesheet" type="text/css" href="../normalize.css"><link rel="stylesheet" type="text/css" href="../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" type="text/css" href="../dark.css"><link rel="stylesheet" type="text/css" href="../light.css" id="themeStyle"><script src="../storage.js"></script><noscript><link rel="stylesheet" href="../noscript.css"></noscript><link rel="shortcut icon" href="https://www.rust-lang.org/favicon.ico"><style type="text/css">#crate-search{background-image:url("../down-arrow.svg");}</style></head><body class="rustdoc mod"><!--[if lte IE 8]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="sidebar"><div class="sidebar-menu">☰</div><a href='../tempfile/index.html'><img src='https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png' alt='logo' width='100'></a><p class='location'>Crate tempfile</p><div class="sidebar-elems"><a id='all-types' href='all.html'><p>See all tempfile's items</p></a><div class="block items"><ul><li><a href="#structs">Structs</a></li><li><a href="#functions">Functions</a></li></ul></div><p class='location'></p><script>window.sidebarCurrent = {name: 'tempfile', ty: 'mod', relpath: '../'};</script></div></nav><div class="theme-picker"><button id="theme-picker" aria-label="Pick another theme!"><img src="../brush.svg" width="18" alt="Pick another theme!"></button><div id="theme-choices"></div></div><script src="../theme.js"></script><nav class="sub"><form class="search-form js-only"><div class="search-container"><div><select id="crate-search"><option value="All crates">All crates</option></select><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"></div><a id="settings-menu" href="../settings.html"><img src="../wheel.svg" width="18" alt="Change settings"></a></div></form></nav><section id="main" class="content"><h1 class='fqn'><span class='out-of-band'><span id='render-detail'><a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class='inner'>−</span>]</a></span><a class='srclink' href='../src/tempfile/lib.rs.html#1-455' title='goto source code'>[src]</a></span><span class='in-band'>Crate <a class="mod" href=''>tempfile</a></span></h1><div class='docblock'><p>Temporary files and directories.</p>
|
||
<ul>
|
||
<li>Use the <a href="fn.tempfile.html"><code>tempfile()</code></a> function for temporary files</li>
|
||
<li>Use the <a href="fn.tempdir.html"><code>tempdir()</code></a> function for temporary directories.</li>
|
||
</ul>
|
||
<h1 id="design" class="section-header"><a href="#design">Design</a></h1>
|
||
<p>This crate provides several approaches to creating temporary files and directories.
|
||
<a href="fn.tempfile.html"><code>tempfile()</code></a> relies on the OS to remove the temporary file once the last handle is closed.
|
||
<a href="struct.TempDir.html"><code>TempDir</code></a> and <a href="struct.NamedTempFile.html"><code>NamedTempFile</code></a> both rely on Rust destructors for cleanup.</p>
|
||
<p>When choosing between the temporary file variants, prefer <code>tempfile</code>
|
||
unless you either need to know the file's path or to be able to persist it.</p>
|
||
<h2 id="resource-leaking" class="section-header"><a href="#resource-leaking">Resource Leaking</a></h2>
|
||
<p><code>tempfile</code> will (almost) never fail to cleanup temporary resources, but <code>TempDir</code> and <code>NamedTempFile</code> will if
|
||
their destructors don't run. This is because <code>tempfile</code> relies on the OS to cleanup the
|
||
underlying file, while <code>TempDir</code> and <code>NamedTempFile</code> rely on their destructors to do so.</p>
|
||
<h2 id="security" class="section-header"><a href="#security">Security</a></h2>
|
||
<p>In the presence of pathological temporary file cleaner, relying on file paths is unsafe because
|
||
a temporary file cleaner could delete the temporary file which an attacker could then replace.</p>
|
||
<p><code>tempfile</code> doesn't rely on file paths so this isn't an issue. However, <code>NamedTempFile</code> does
|
||
rely on file paths.</p>
|
||
<h2 id="examples" class="section-header"><a href="#examples">Examples</a></h2>
|
||
<p>Create a temporary file and write some data into it:</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered">
|
||
<span class="kw">use</span> <span class="ident">tempfile</span>::<span class="ident">tempfile</span>;
|
||
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">io</span>::{<span class="self">self</span>, <span class="ident">Write</span>};
|
||
|
||
<span class="comment">// Create a file inside of `std::env::temp_dir()`.</span>
|
||
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">file</span> <span class="op">=</span> <span class="ident">tempfile</span>()<span class="question-mark">?</span>;
|
||
|
||
<span class="macro">writeln</span><span class="macro">!</span>(<span class="ident">file</span>, <span class="string">"Brian was here. Briefly."</span>)<span class="question-mark">?</span>;</pre></div>
|
||
<p>Create a temporary directory and add a file to it:</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered">
|
||
<span class="kw">use</span> <span class="ident">tempfile</span>::<span class="ident">tempdir</span>;
|
||
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">fs</span>::<span class="ident">File</span>;
|
||
<span class="kw">use</span> <span class="ident">std</span>::<span class="ident">io</span>::{<span class="self">self</span>, <span class="ident">Write</span>};
|
||
|
||
<span class="comment">// Create a directory inside of `std::env::temp_dir()`.</span>
|
||
<span class="kw">let</span> <span class="ident">dir</span> <span class="op">=</span> <span class="ident">tempdir</span>()<span class="question-mark">?</span>;
|
||
|
||
<span class="kw">let</span> <span class="ident">file_path</span> <span class="op">=</span> <span class="ident">dir</span>.<span class="ident">path</span>().<span class="ident">join</span>(<span class="string">"my-temporary-note.txt"</span>);
|
||
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">file</span> <span class="op">=</span> <span class="ident">File</span>::<span class="ident">create</span>(<span class="ident">file_path</span>)<span class="question-mark">?</span>;
|
||
<span class="macro">writeln</span><span class="macro">!</span>(<span class="ident">file</span>, <span class="string">"Brian was here. Briefly."</span>)<span class="question-mark">?</span>;
|
||
|
||
<span class="comment">// By closing the `TempDir` explicitly, we can check that it has</span>
|
||
<span class="comment">// been deleted successfully. If we don't close it explicitly,</span>
|
||
<span class="comment">// the directory will still be deleted when `dir` goes out</span>
|
||
<span class="comment">// of scope, but we won't know whether deleting the directory</span>
|
||
<span class="comment">// succeeded.</span>
|
||
<span class="ident">drop</span>(<span class="ident">file</span>);
|
||
<span class="ident">dir</span>.<span class="ident">close</span>()<span class="question-mark">?</span>;</pre></div>
|
||
</div><h2 id='structs' class='section-header'><a href="#structs">Structs</a></h2>
|
||
<table><tr class='module-item'><td><a class="struct" href="struct.Builder.html" title='tempfile::Builder struct'>Builder</a></td><td class='docblock-short'><p>Create a new temporary file or directory with custom parameters.</p>
|
||
</td></tr><tr class='module-item'><td><a class="struct" href="struct.NamedTempFile.html" title='tempfile::NamedTempFile struct'>NamedTempFile</a></td><td class='docblock-short'><p>A named temporary file.</p>
|
||
</td></tr><tr class='module-item'><td><a class="struct" href="struct.PathPersistError.html" title='tempfile::PathPersistError struct'>PathPersistError</a></td><td class='docblock-short'><p>Error returned when persisting a temporary file path fails.</p>
|
||
</td></tr><tr class='module-item'><td><a class="struct" href="struct.PersistError.html" title='tempfile::PersistError struct'>PersistError</a></td><td class='docblock-short'><p>Error returned when persisting a temporary file fails.</p>
|
||
</td></tr><tr class='module-item'><td><a class="struct" href="struct.SpooledTempFile.html" title='tempfile::SpooledTempFile struct'>SpooledTempFile</a></td><td class='docblock-short'><p>An object that behaves like a regular temporary file, but keeps data in
|
||
memory until it reaches a configured size, at which point the data is
|
||
written to a temporary file on disk, and further operations use the file
|
||
on disk.</p>
|
||
</td></tr><tr class='module-item'><td><a class="struct" href="struct.TempDir.html" title='tempfile::TempDir struct'>TempDir</a></td><td class='docblock-short'><p>A directory in the filesystem that is automatically deleted when
|
||
it goes out of scope.</p>
|
||
</td></tr><tr class='module-item'><td><a class="struct" href="struct.TempPath.html" title='tempfile::TempPath struct'>TempPath</a></td><td class='docblock-short'><p>A path to a named temporary file without an open file handle.</p>
|
||
</td></tr></table><h2 id='functions' class='section-header'><a href="#functions">Functions</a></h2>
|
||
<table><tr class='module-item'><td><a class="fn" href="fn.spooled_tempfile.html" title='tempfile::spooled_tempfile fn'>spooled_tempfile</a></td><td class='docblock-short'><p>Create a new spooled temporary file.</p>
|
||
</td></tr><tr class='module-item'><td><a class="fn" href="fn.tempdir.html" title='tempfile::tempdir fn'>tempdir</a></td><td class='docblock-short'><p>Create a new temporary directory.</p>
|
||
</td></tr><tr class='module-item'><td><a class="fn" href="fn.tempdir_in.html" title='tempfile::tempdir_in fn'>tempdir_in</a></td><td class='docblock-short'><p>Create a new temporary directory.</p>
|
||
</td></tr><tr class='module-item'><td><a class="fn" href="fn.tempfile.html" title='tempfile::tempfile fn'>tempfile</a></td><td class='docblock-short'><p>Create a new temporary file.</p>
|
||
</td></tr><tr class='module-item'><td><a class="fn" href="fn.tempfile_in.html" title='tempfile::tempfile_in fn'>tempfile_in</a></td><td class='docblock-short'><p>Create a new temporary file in the specified directory.</p>
|
||
</td></tr></table></section><section id="search" class="content hidden"></section><section class="footer"></section><aside id="help" class="hidden"><div><h1 class="hidden">Help</h1><div class="shortcuts"><h2>Keyboard Shortcuts</h2><dl><dt><kbd>?</kbd></dt><dd>Show this help dialog</dd><dt><kbd>S</kbd></dt><dd>Focus the search field</dd><dt><kbd>↑</kbd></dt><dd>Move up in search results</dd><dt><kbd>↓</kbd></dt><dd>Move down in search results</dd><dt><kbd>↹</kbd></dt><dd>Switch tab</dd><dt><kbd>⏎</kbd></dt><dd>Go to active search result</dd><dt><kbd>+</kbd></dt><dd>Expand all sections</dd><dt><kbd>-</kbd></dt><dd>Collapse all sections</dd></dl></div><div class="infos"><h2>Search Tricks</h2><p>Prefix searches with a type followed by a colon (e.g., <code>fn:</code>) to restrict the search to a given type.</p><p>Accepted types are: <code>fn</code>, <code>mod</code>, <code>struct</code>, <code>enum</code>, <code>trait</code>, <code>type</code>, <code>macro</code>, and <code>const</code>.</p><p>Search functions by type signature (e.g., <code>vec -> usize</code> or <code>* -> vec</code>)</p><p>Search multiple things at once by splitting your query with comma (e.g., <code>str,u8</code> or <code>String,struct:Vec,test</code>)</p></div></div></aside><script>window.rootPath = "../";window.currentCrate = "tempfile";</script><script src="../aliases.js"></script><script src="../main.js"></script><script defer src="../search-index.js"></script></body></html> |