49 lines
10 KiB
HTML
49 lines
10 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="This crate defines `Writeable`, a trait representing an object that can be written to a sink implementing `std::fmt::Write`. It is an alternative to `std::fmt::Display` with the addition of a function indicating the number of bytes to be written."><title>writeable - Rust</title><script>if(window.location.protocol!=="file:")document.head.insertAdjacentHTML("beforeend","SourceSerif4-Regular-6b053e98.ttf.woff2,FiraSans-Italic-81dc35de.woff2,FiraSans-Regular-0fe48ade.woff2,FiraSans-MediumItalic-ccf7e434.woff2,FiraSans-Medium-e1aa3f0a.woff2,SourceCodePro-Regular-8badfe75.ttf.woff2,SourceCodePro-Semibold-aa29a496.ttf.woff2".split(",").map(f=>`<link rel="preload" as="font" type="font/woff2"href="../static.files/${f}">`).join(""))</script><link rel="stylesheet" href="../static.files/normalize-9960930a.css"><link rel="stylesheet" href="../static.files/rustdoc-ca0dd0c4.css"><meta name="rustdoc-vars" data-root-path="../" data-static-root-path="../static.files/" data-current-crate="writeable" data-themes="" data-resource-suffix="" data-rustdoc-version="1.93.1 (01f6ddf75 2026-02-11) (Arch Linux rust 1:1.93.1-1)" data-channel="1.93.1" data-search-js="search-9e2438ea.js" data-stringdex-js="stringdex-a3946164.js" data-settings-js="settings-c38705f0.js" ><script src="../static.files/storage-e2aeef58.js"></script><script defer src="../crates.js"></script><script defer src="../static.files/main-a410ff4d.js"></script><noscript><link rel="stylesheet" href="../static.files/noscript-263c88ec.css"></noscript><link rel="alternate icon" type="image/png" href="../static.files/favicon-32x32-eab170b8.png"><link rel="icon" type="image/svg+xml" href="../static.files/favicon-044be391.svg"></head><body class="rustdoc mod crate"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><rustdoc-topbar><h2><a href="#">Crate writeable</a></h2></rustdoc-topbar><nav class="sidebar"><div class="sidebar-crate"><h2><a href="../writeable/index.html">writeable</a><span class="version">0.6.2</span></h2></div><div class="sidebar-elems"><ul class="block"><li><a id="all-types" href="all.html">All Items</a></li></ul><section id="rustdoc-toc"><h3><a href="#">Sections</a></h3><ul class="block top-toc"><li><a href="#benchmarks" title="Benchmarks">Benchmarks</a></li><li><a href="#examples" title="Examples">Examples</a></li></ul><h3><a href="#modules">Crate Items</a></h3><ul class="block"><li><a href="#modules" title="Modules">Modules</a></li><li><a href="#macros" title="Macros">Macros</a></li><li><a href="#structs" title="Structs">Structs</a></li><li><a href="#traits" title="Traits">Traits</a></li><li><a href="#functions" title="Functions">Functions</a></li></ul></section><div id="rustdoc-modnav"></div></div></nav><div class="sidebar-resizer" title="Drag to resize sidebar"></div><main><div class="width-limiter"><section id="main-content" class="content"><div class="main-heading"><h1>Crate <span>writeable</span> <button id="copy-path" title="Copy item path to clipboard">Copy item path</button></h1><rustdoc-toolbar></rustdoc-toolbar><span class="sub-heading"><a class="src" href="../src/writeable/lib.rs.html#6-509">Source</a> </span></div><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>This crate defines <a href="trait.Writeable.html" title="trait writeable::Writeable"><code>Writeable</code></a>, a trait representing an object that can be written to a
|
|
sink implementing <code>std::fmt::Write</code>. It is an alternative to <code>std::fmt::Display</code> with the
|
|
addition of a function indicating the number of bytes to be written.</p>
|
|
<p><code>Writeable</code> improves upon <code>std::fmt::Display</code> in two ways:</p>
|
|
<ol>
|
|
<li>More efficient, since the sink can pre-allocate bytes.</li>
|
|
<li>Smaller code, since the format machinery can be short-circuited.</li>
|
|
</ol>
|
|
<p>This crate also exports <a href="trait.TryWriteable.html" title="trait writeable::TryWriteable"><code>TryWriteable</code></a>, a writeable that supports a custom error.</p>
|
|
<h2 id="benchmarks"><a class="doc-anchor" href="#benchmarks">§</a>Benchmarks</h2>
|
|
<p>The benchmarks to generate the following data can be found in the <code>benches</code> directory.</p>
|
|
<div><table><thead><tr><th>Case</th><th><code>Writeable</code></th><th><code>Display</code></th></tr></thead><tbody>
|
|
<tr><td>Create string from single-string message (139 chars)</td><td>15.642 ns</td><td>19.251 ns</td></tr>
|
|
<tr><td>Create string from complex message</td><td>35.830 ns</td><td>89.478 ns</td></tr>
|
|
<tr><td>Write complex message to buffer</td><td>57.336 ns</td><td>64.408 ns</td></tr>
|
|
</tbody></table>
|
|
</div><h2 id="examples"><a class="doc-anchor" href="#examples">§</a>Examples</h2>
|
|
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>std::fmt;
|
|
<span class="kw">use </span>writeable::assert_writeable_eq;
|
|
<span class="kw">use </span>writeable::LengthHint;
|
|
<span class="kw">use </span>writeable::Writeable;
|
|
|
|
<span class="kw">struct </span>WelcomeMessage<<span class="lifetime">'s</span>> {
|
|
<span class="kw">pub </span>name: <span class="kw-2">&</span><span class="lifetime">'s </span>str,
|
|
}
|
|
|
|
<span class="kw">impl</span><<span class="lifetime">'s</span>> Writeable <span class="kw">for </span>WelcomeMessage<<span class="lifetime">'s</span>> {
|
|
<span class="kw">fn </span>write_to<W: fmt::Write + <span class="question-mark">?</span>Sized>(<span class="kw-2">&</span><span class="self">self</span>, sink: <span class="kw-2">&mut </span>W) -> fmt::Result {
|
|
sink.write_str(<span class="string">"Hello, "</span>)<span class="question-mark">?</span>;
|
|
sink.write_str(<span class="self">self</span>.name)<span class="question-mark">?</span>;
|
|
sink.write_char(<span class="string">'!'</span>)<span class="question-mark">?</span>;
|
|
<span class="prelude-val">Ok</span>(())
|
|
}
|
|
|
|
<span class="kw">fn </span>writeable_length_hint(<span class="kw-2">&</span><span class="self">self</span>) -> LengthHint {
|
|
<span class="comment">// "Hello, " + '!' + length of name
|
|
</span>LengthHint::exact(<span class="number">8 </span>+ <span class="self">self</span>.name.len())
|
|
}
|
|
}
|
|
|
|
<span class="kw">let </span>message = WelcomeMessage { name: <span class="string">"Alice" </span>};
|
|
<span class="macro">assert_writeable_eq!</span>(<span class="kw-2">&</span>message, <span class="string">"Hello, Alice!"</span>);
|
|
|
|
<span class="comment">// Types implementing `Writeable` are recommended to also implement `fmt::Display`.
|
|
// This can be simply done by redirecting to the `Writeable` implementation:
|
|
</span><span class="macro">writeable::impl_display_with_writeable!</span>(WelcomeMessage<<span class="lifetime">'_</span>>);
|
|
<span class="macro">assert_eq!</span>(message.to_string(), <span class="string">"Hello, Alice!"</span>);</code></pre></div></div></details><h2 id="modules" class="section-header">Modules<a href="#modules" class="anchor">§</a></h2><dl class="item-table"><dt><a class="mod" href="adapters/index.html" title="mod writeable::adapters">adapters</a></dt><dd>Helper types for trait impls.</dd></dl><h2 id="macros" class="section-header">Macros<a href="#macros" class="anchor">§</a></h2><dl class="item-table"><dt><a class="macro" href="macro.assert_try_writeable_eq.html" title="macro writeable::assert_try_writeable_eq">assert_<wbr>try_<wbr>writeable_<wbr>eq</a></dt><dd>Testing macros for types implementing <a href="trait.TryWriteable.html" title="trait writeable::TryWriteable"><code>TryWriteable</code></a>.</dd><dt><a class="macro" href="macro.assert_try_writeable_parts_eq.html" title="macro writeable::assert_try_writeable_parts_eq">assert_<wbr>try_<wbr>writeable_<wbr>parts_<wbr>eq</a></dt><dd>See <a href="macro.assert_try_writeable_eq.html" title="macro writeable::assert_try_writeable_eq"><code>assert_try_writeable_eq</code></a>.</dd><dt><a class="macro" href="macro.impl_display_with_writeable.html" title="macro writeable::impl_display_with_writeable">impl_<wbr>display_<wbr>with_<wbr>writeable</a></dt><dd>Implements <a href="https://doc.rust-lang.org/1.93.1/core/fmt/trait.Display.html" title="trait core::fmt::Display"><code>Display</code></a> for types that implement <a href="trait.Writeable.html" title="trait writeable::Writeable"><code>Writeable</code></a>.</dd></dl><h2 id="structs" class="section-header">Structs<a href="#structs" class="anchor">§</a></h2><dl class="item-table"><dt><a class="struct" href="struct.LengthHint.html" title="struct writeable::LengthHint">Length<wbr>Hint</a></dt><dd>A hint to help consumers of <code>Writeable</code> pre-allocate bytes before they call
|
|
<a href="trait.Writeable.html#method.write_to" title="method writeable::Writeable::write_to"><code>write_to</code></a>.</dd><dt><a class="struct" href="struct.Part.html" title="struct writeable::Part">Part</a></dt><dd><a href="struct.Part.html" title="struct writeable::Part"><code>Part</code></a>s are used as annotations for formatted strings.</dd></dl><h2 id="traits" class="section-header">Traits<a href="#traits" class="anchor">§</a></h2><dl class="item-table"><dt><a class="trait" href="trait.PartsWrite.html" title="trait writeable::PartsWrite">Parts<wbr>Write</a></dt><dd>A sink that supports annotating parts of the string with <a href="struct.Part.html" title="struct writeable::Part"><code>Part</code></a>s.</dd><dt><a class="trait" href="trait.TryWriteable.html" title="trait writeable::TryWriteable">TryWriteable</a></dt><dd>A writeable object that can fail while writing.</dd><dt><a class="trait" href="trait.Writeable.html" title="trait writeable::Writeable">Writeable</a></dt><dd><code>Writeable</code> is an alternative to <code>std::fmt::Display</code> with the addition of a length function.</dd></dl><h2 id="functions" class="section-header">Functions<a href="#functions" class="anchor">§</a></h2><dl class="item-table"><dt><a class="fn" href="fn.cmp_str.html" title="fn writeable::cmp_str">cmp_str</a></dt><dd>Compares the contents of a <code>Writeable</code> to the given bytes
|
|
without allocating a String to hold the <code>Writeable</code> contents.</dd><dt><a class="fn" href="fn.cmp_utf8.html" title="fn writeable::cmp_utf8">cmp_<wbr>utf8</a></dt><dd>Compares the contents of a <a href="trait.Writeable.html" title="trait writeable::Writeable"><code>Writeable</code></a> to the given UTF-8 bytes without allocating memory.</dd></dl></section></div></main></body></html> |