Files
rust-gpu/api/rustix/buffer/trait.Buffer.html
2026-04-13 16:52:47 +00:00

54 lines
14 KiB
HTML
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!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="A memory buffer that may be uninitialized."><title>Buffer in rustix::buffer - 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-17e0aaed.css"><meta name="rustdoc-vars" data-root-path="../../" data-static-root-path="../../static.files/" data-current-crate="rustix" data-themes="" data-resource-suffix="" data-rustdoc-version="1.96.0-nightly (02c7f9bec 2026-04-10)" data-channel="nightly" data-search-js="search-b5634cc7.js" data-stringdex-js="stringdex-2da4960a.js" data-settings-js="settings-170eb4bf.js" ><script src="../../static.files/storage-41dd4d93.js"></script><script defer src="sidebar-items.js"></script><script defer src="../../static.files/main-5013f961.js"></script><noscript><link rel="stylesheet" href="../../static.files/noscript-f7c3ffd8.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 trait"><a class="skip-main-content" href="#main-content">Skip to main content</a><!--[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="#">Buffer</a></h2></rustdoc-topbar><nav class="sidebar"><div class="sidebar-crate"><h2><a href="../../rustix/index.html">rustix</a><span class="version">1.1.3</span></h2></div><div class="sidebar-elems"><section id="rustdoc-toc"><h2 class="location"><a href="#">Buffer</a></h2><h3><a href="#">Sections</a></h3><ul class="block top-toc"><li><a href="#examples" title="Examples">Examples</a></li><li><a href="#guide-to-error-messages" title="Guide to error messages">Guide to error messages</a></li></ul><h3><a href="#foreign-impls">Implementations on Foreign Types</a></h3><ul class="block"><li><a href="#impl-Buffer%3CT%3E-for-%26mut+Vec%3CMaybeUninit%3CT%3E%3E" title="&#38;mut Vec&#60;MaybeUninit&#60;T&#62;&#62;">&#38;mut Vec&#60;MaybeUninit&#60;T&#62;&#62;</a></li><li><a href="#impl-Buffer%3CT%3E-for-%26mut+Vec%3CT%3E" title="&#38;mut Vec&#60;T&#62;">&#38;mut Vec&#60;T&#62;</a></li><li><a href="#impl-Buffer%3CT%3E-for-%26mut+%5BMaybeUninit%3CT%3E;+N%5D" title="&#38;mut [MaybeUninit&#60;T&#62;; N]">&#38;mut [MaybeUninit&#60;T&#62;; N]</a></li><li><a href="#impl-Buffer%3CT%3E-for-%26mut+%5BMaybeUninit%3CT%3E%5D" title="&#38;mut [MaybeUninit&#60;T&#62;]">&#38;mut [MaybeUninit&#60;T&#62;]</a></li><li><a href="#impl-Buffer%3CT%3E-for-%26mut+%5BT;+N%5D" title="&#38;mut [T; N]">&#38;mut [T; N]</a></li><li><a href="#impl-Buffer%3CT%3E-for-%26mut+%5BT%5D" title="&#38;mut [T]">&#38;mut [T]</a></li></ul><h3><a href="#implementors">Implementors</a></h3></section><div id="rustdoc-modnav"><h2><a href="index.html">In rustix::<wbr>buffer</a></h2></div></div></nav><div class="sidebar-resizer" title="Drag to resize sidebar"></div><main><div class="width-limiter"><section id="main-content" class="content" tabindex="-1"><div class="main-heading"><div class="rustdoc-breadcrumbs"><a href="../index.html">rustix</a>::<wbr><a href="index.html">buffer</a></div><h1>Trait <span class="trait">Buffer</span>&nbsp;<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/rustix/buffer.rs.html#106">Source</a> </span></div><pre class="rust item-decl"><code>pub trait Buffer&lt;T&gt;: Sealed&lt;T&gt; { }</code></pre><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>A memory buffer that may be uninitialized.</p>
<p>There are three types that implement the <code>Buffer</code> trait, and the type you
use determines the return type of the functions that use it:</p>
<div><table><thead><tr><th>If you pass a…</th><th>You get back a…</th></tr></thead><tbody>
<tr><td><code>&amp;mut [u8]</code></td><td><code>usize</code>, indicating the number of elements initialized.</td></tr>
<tr><td><code>&amp;mut [MaybeUninit&lt;u8&gt;]</code></td><td><code>(&amp;mut [u8], &amp;mut [MaybeUninit&lt;u8&gt;])</code>, holding the initialized and uninitialized subslices.</td></tr>
<tr><td><a href="struct.SpareCapacity.html" title="struct rustix::buffer::SpareCapacity"><code>SpareCapacity</code></a></td><td><code>usize</code>, indicating the number of elements initialized. And the <code>Vec</code> is extended.</td></tr>
</tbody></table>
</div><h2 id="examples"><a class="doc-anchor" href="#examples">§</a>Examples</h2>
<p>Passing a <code>&amp;mut [u8]</code>:</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">let </span><span class="kw-2">mut </span>buf = [<span class="number">0_u8</span>; <span class="number">64</span>];
<span class="kw">let </span>nread = read(fd, <span class="kw-2">&amp;mut </span>buf)<span class="question-mark">?</span>;
<span class="comment">// `nread` is the number of bytes read.</span></code></pre></div>
<p>Passing a <code>&amp;mut [MaybeUninit&lt;u8&gt;]</code>:</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">let </span><span class="kw-2">mut </span>buf = [MaybeUninit::&lt;u8&gt;::uninit(); <span class="number">64</span>];
<span class="kw">let </span>(init, uninit) = read(fd, <span class="kw-2">&amp;mut </span>buf)<span class="question-mark">?</span>;
<span class="comment">// `init` is a `&amp;mut [u8]` with the initialized bytes.
// `uninit` is a `&amp;mut [MaybeUninit&lt;u8&gt;]` with the remaining bytes.</span></code></pre></div>
<p>Passing a <a href="struct.SpareCapacity.html" title="struct rustix::buffer::SpareCapacity"><code>SpareCapacity</code></a>, via the <a href="fn.spare_capacity.html" title="fn rustix::buffer::spare_capacity"><code>spare_capacity</code></a> helper function:</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">let </span><span class="kw-2">mut </span>buf = Vec::with_capacity(<span class="number">64</span>);
<span class="kw">let </span>nread = read(fd, spare_capacity(<span class="kw-2">&amp;mut </span>buf))<span class="question-mark">?</span>;
<span class="comment">// `nread` is the number of bytes read.
// Also, `buf.len()` is now `nread` elements longer than it was before.</span></code></pre></div><h2 id="guide-to-error-messages"><a class="doc-anchor" href="#guide-to-error-messages">§</a>Guide to error messages</h2>
<p>Sometimes code using <code>Buffer</code> can encounter non-obvious error messages.
Here are some weve encountered, along with ways to fix them.</p>
<p>If you see errors like
“cannot move out of <code>self</code> which is behind a mutable reference”
and
“move occurs because <code>x</code> has type <code>&amp;mut [u8]</code>, which does not implement the <code>Copy</code> trait”,
replace <code>x</code> with <code>&amp;mut *x</code>. See <code>error_buffer_wrapper</code> in
examples/buffer_errors.rs.</p>
<p>If you see errors like
“type annotations needed”
and
“cannot infer type of the type parameter <code>Buf</code> declared on the function <code>read</code>”,
you may need to change a <code>&amp;mut []</code> to <code>&amp;mut [0_u8; 0]</code>. See
<code>error_empty_slice</code> in examples/buffer_errors.rs.</p>
<p>If you see errors like
“the trait bound <code>[MaybeUninit&lt;u8&gt;; 1]: Buffer&lt;u8&gt;</code> is not satisfied”,
add a <code>&amp;mut</code> to pass the array by reference instead of by value. See
<code>error_array_by_value</code> in examples/buffer_errors.rs.</p>
<p>If you see errors like
“cannot move out of <code>x</code>, a captured variable in an <code>FnMut</code> closure”,
try replacing <code>x</code> with <code>&amp;mut *x</code>, or, if that doesnt work, try moving a
<code>let</code> into the closure body. See <code>error_retry_closure</code> and
<code>error_retry_indirect_closure</code> in examples/buffer_errors.rs.</p>
<p>If you see errors like
“captured variable cannot escape <code>FnMut</code> closure body”,
use an explicit loop instead of <code>retry_on_intr</code>, assuming youre using
that. See <code>error_retry_closure_uninit</code> in examples/buffer_errors.rs.</p>
</div></details><h2 id="foreign-impls" class="section-header">Implementations on Foreign Types<a href="#foreign-impls" class="anchor">§</a></h2><section id="impl-Buffer%3CT%3E-for-%26mut+%5BT%5D" class="impl"><a class="src rightside" href="../../src/rustix/buffer.rs.html#109">Source</a><a href="#impl-Buffer%3CT%3E-for-%26mut+%5BT%5D" class="anchor">§</a><h3 class="code-header">impl&lt;T&gt; <a class="trait" href="trait.Buffer.html" title="trait rustix::buffer::Buffer">Buffer</a>&lt;T&gt; for &amp;mut <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.slice.html">[T]</a></h3></section><section id="impl-Buffer%3CT%3E-for-%26mut+Vec%3CMaybeUninit%3CT%3E%3E" class="impl"><a class="src rightside" href="../../src/rustix/buffer.rs.html#116">Source</a><a href="#impl-Buffer%3CT%3E-for-%26mut+Vec%3CMaybeUninit%3CT%3E%3E" class="anchor">§</a><h3 class="code-header">impl&lt;T&gt; <a class="trait" href="trait.Buffer.html" title="trait rustix::buffer::Buffer">Buffer</a>&lt;T&gt; for &amp;mut <a class="struct" href="https://doc.rust-lang.org/nightly/alloc/vec/struct.Vec.html" title="struct alloc::vec::Vec">Vec</a>&lt;<a class="union" href="https://doc.rust-lang.org/nightly/core/mem/maybe_uninit/union.MaybeUninit.html" title="union core::mem::maybe_uninit::MaybeUninit">MaybeUninit</a>&lt;T&gt;&gt;</h3><span class="item-info"><div class="stab portability">Available on <strong>crate feature <code>alloc</code></strong> only.</div></span></section><section id="impl-Buffer%3CT%3E-for-%26mut+Vec%3CT%3E" class="impl"><a class="src rightside" href="../../src/rustix/buffer.rs.html#112">Source</a><a href="#impl-Buffer%3CT%3E-for-%26mut+Vec%3CT%3E" class="anchor">§</a><h3 class="code-header">impl&lt;T&gt; <a class="trait" href="trait.Buffer.html" title="trait rustix::buffer::Buffer">Buffer</a>&lt;T&gt; for &amp;mut <a class="struct" href="https://doc.rust-lang.org/nightly/alloc/vec/struct.Vec.html" title="struct alloc::vec::Vec">Vec</a>&lt;T&gt;</h3><span class="item-info"><div class="stab portability">Available on <strong>crate feature <code>alloc</code></strong> only.</div></span></section><section id="impl-Buffer%3CT%3E-for-%26mut+%5BMaybeUninit%3CT%3E%5D" class="impl"><a class="src rightside" href="../../src/rustix/buffer.rs.html#113">Source</a><a href="#impl-Buffer%3CT%3E-for-%26mut+%5BMaybeUninit%3CT%3E%5D" class="anchor">§</a><h3 class="code-header">impl&lt;T&gt; <a class="trait" href="trait.Buffer.html" title="trait rustix::buffer::Buffer">Buffer</a>&lt;T&gt; for &amp;mut [<a class="union" href="https://doc.rust-lang.org/nightly/core/mem/maybe_uninit/union.MaybeUninit.html" title="union core::mem::maybe_uninit::MaybeUninit">MaybeUninit</a>&lt;T&gt;]</h3></section><section id="impl-Buffer%3CT%3E-for-%26mut+%5BT;+N%5D" class="impl"><a class="src rightside" href="../../src/rustix/buffer.rs.html#110">Source</a><a href="#impl-Buffer%3CT%3E-for-%26mut+%5BT;+N%5D" class="anchor">§</a><h3 class="code-header">impl&lt;T, const N: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a>&gt; <a class="trait" href="trait.Buffer.html" title="trait rustix::buffer::Buffer">Buffer</a>&lt;T&gt; for &amp;mut <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.array.html">[T; N]</a></h3></section><section id="impl-Buffer%3CT%3E-for-%26mut+%5BMaybeUninit%3CT%3E;+N%5D" class="impl"><a class="src rightside" href="../../src/rustix/buffer.rs.html#114">Source</a><a href="#impl-Buffer%3CT%3E-for-%26mut+%5BMaybeUninit%3CT%3E;+N%5D" class="anchor">§</a><h3 class="code-header">impl&lt;T, const N: <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.usize.html">usize</a>&gt; <a class="trait" href="trait.Buffer.html" title="trait rustix::buffer::Buffer">Buffer</a>&lt;T&gt; for &amp;mut [<a class="union" href="https://doc.rust-lang.org/nightly/core/mem/maybe_uninit/union.MaybeUninit.html" title="union core::mem::maybe_uninit::MaybeUninit">MaybeUninit</a>&lt;T&gt;; <a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.array.html">N</a>]</h3></section><h2 id="implementors" class="section-header">Implementors<a href="#implementors" class="anchor">§</a></h2><div id="implementors-list"><div class="negative-marker"></div><section id="impl-Buffer%3CT%3E-for-SpareCapacity%3C'a,+T%3E" class="impl"><a class="src rightside" href="../../src/rustix/buffer.rs.html#118">Source</a><a href="#impl-Buffer%3CT%3E-for-SpareCapacity%3C'a,+T%3E" class="anchor">§</a><h3 class="code-header">impl&lt;'a, T&gt; <a class="trait" href="trait.Buffer.html" title="trait rustix::buffer::Buffer">Buffer</a>&lt;T&gt; for <a class="struct" href="struct.SpareCapacity.html" title="struct rustix::buffer::SpareCapacity">SpareCapacity</a>&lt;'a, T&gt;</h3><span class="item-info"><div class="stab portability">Available on <strong>crate feature <code>alloc</code></strong> only.</div></span></section></div><script src="../../trait.impl/rustix/buffer/trait.Buffer.js" data-ignore-extern-crates="std,alloc" async></script></section></div></main></body></html>