mentat/bytes/buf/trait.FromBuf.html
2018-08-22 17:04:13 +00:00

212 lines
No EOL
14 KiB
HTML
Raw 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="API documentation for the Rust `FromBuf` trait in crate `bytes`.">
<meta name="keywords" content="rust, rustlang, rust-lang, FromBuf">
<title>bytes::buf::FromBuf - 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="../../main.css" id="themeStyle">
<script src="../../storage.js"></script>
</head>
<body class="rustdoc trait">
<!--[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">&#9776;</div>
<p class='location'>Trait FromBuf</p><div class="sidebar-elems"><div class="block items"><a class="sidebar-title" href="#required-methods">Required Methods</a><div class="sidebar-links"><a href="#tymethod.from_buf">from_buf</a></div><a class="sidebar-title" href="#foreign-impls">Implementations on Foreign Types</a><div class="sidebar-links"><a href="#impl-FromBuf">Vec&lt;u8&gt;</a></div><a class="sidebar-title" href="#implementors">Implementors</a></div><p class='location'><a href='../index.html'>bytes</a>::<wbr><a href='index.html'>buf</a></p><script>window.sidebarCurrent = {name: 'FromBuf', ty: 'trait', relpath: ''};</script><script defer src="sidebar-items.js"></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">
<input class="search-input" name="search"
autocomplete="off"
placeholder="Click or press S to search, ? for more options…"
type="search">
</div>
</form>
</nav>
<section id='main' class="content">
<h1 class='fqn'><span class='in-band'>Trait <a href='../index.html'>bytes</a>::<wbr><a href='index.html'>buf</a>::<wbr><a class="trait" href=''>FromBuf</a></span><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'>&#x2212;</span>]
</a>
</span><a class='srclink' href='../../src/bytes/buf/from_buf.rs.html#68-87' title='goto source code'>[src]</a></span></h1>
<pre class='rust trait'>pub trait FromBuf {
fn <a href='#tymethod.from_buf' class='fnname'>from_buf</a>&lt;T&gt;(buf: T) -&gt; Self<br>&nbsp;&nbsp;&nbsp; <span class="where">where<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;T: <a class="trait" href="../../bytes/buf/trait.IntoBuf.html" title="trait bytes::buf::IntoBuf">IntoBuf</a></span>;
}</pre><div class='docblock'><p>Conversion from a <a href="trait.Buf.html"><code>Buf</code></a></p>
<p>Implementing <code>FromBuf</code> for a type defines how it is created from a buffer.
This is common for types which represent byte storage of some kind.</p>
<p><a href="#method.from_buf"><code>FromBuf::from_buf</code></a> is rarely called explicitly, and it is instead used
through <a href="trait.Buf.html#method.collect"><code>Buf::collect</code></a>. See <a href="trait.Buf.html#method.collect"><code>Buf::collect</code></a> documentation for more examples.</p>
<p>See also <a href="trait.IntoBuf.html"><code>IntoBuf</code></a>.</p>
<h1 id="examples" class="section-header"><a href="#examples">Examples</a></h1>
<p>Basic usage:</p>
<pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">bytes</span>::{<span class="ident">Bytes</span>, <span class="ident">IntoBuf</span>};
<span class="kw">use</span> <span class="ident">bytes</span>::<span class="ident">buf</span>::<span class="ident">FromBuf</span>;
<span class="kw">let</span> <span class="ident">buf</span> <span class="op">=</span> <span class="ident">Bytes</span>::<span class="ident">from</span>(<span class="kw-2">&amp;</span><span class="string">b&quot;hello world&quot;</span>[..]).<span class="ident">into_buf</span>();
<span class="kw">let</span> <span class="ident">vec</span> <span class="op">=</span> <span class="ident">Vec</span>::<span class="ident">from_buf</span>(<span class="ident">buf</span>);
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">vec</span>, <span class="kw-2">&amp;</span><span class="string">b&quot;hello world&quot;</span>[..]);</pre>
<p>Using <a href="trait.Buf.html#method.collect"><code>Buf::collect</code></a> to implicitly use <code>FromBuf</code>:</p>
<pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">bytes</span>::{<span class="ident">Buf</span>, <span class="ident">Bytes</span>, <span class="ident">IntoBuf</span>};
<span class="kw">let</span> <span class="ident">buf</span> <span class="op">=</span> <span class="ident">Bytes</span>::<span class="ident">from</span>(<span class="kw-2">&amp;</span><span class="string">b&quot;hello world&quot;</span>[..]).<span class="ident">into_buf</span>();
<span class="kw">let</span> <span class="ident">vec</span>: <span class="ident">Vec</span><span class="op">&lt;</span><span class="ident">u8</span><span class="op">&gt;</span> <span class="op">=</span> <span class="ident">buf</span>.<span class="ident">collect</span>();
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">vec</span>, <span class="kw-2">&amp;</span><span class="string">b&quot;hello world&quot;</span>[..]);</pre>
<p>Implementing <code>FromBuf</code> for your type:</p>
<pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">bytes</span>::{<span class="ident">BufMut</span>, <span class="ident">Bytes</span>};
<span class="kw">use</span> <span class="ident">bytes</span>::<span class="ident">buf</span>::{<span class="ident">IntoBuf</span>, <span class="ident">FromBuf</span>};
<span class="comment">// A sample buffer, that&#39;s just a wrapper over Vec&lt;u8&gt;</span>
<span class="kw">struct</span> <span class="ident">MyBuffer</span>(<span class="ident">Vec</span><span class="op">&lt;</span><span class="ident">u8</span><span class="op">&gt;</span>);
<span class="kw">impl</span> <span class="ident">FromBuf</span> <span class="kw">for</span> <span class="ident">MyBuffer</span> {
<span class="kw">fn</span> <span class="ident">from_buf</span><span class="op">&lt;</span><span class="ident">B</span><span class="op">&gt;</span>(<span class="ident">buf</span>: <span class="ident">B</span>) <span class="op">-&gt;</span> <span class="self">Self</span> <span class="kw">where</span> <span class="ident">B</span>: <span class="ident">IntoBuf</span> {
<span class="kw">let</span> <span class="kw-2">mut</span> <span class="ident">v</span> <span class="op">=</span> <span class="ident">Vec</span>::<span class="ident">new</span>();
<span class="ident">v</span>.<span class="ident">put</span>(<span class="ident">buf</span>.<span class="ident">into_buf</span>());
<span class="ident">MyBuffer</span>(<span class="ident">v</span>)
}
}
<span class="comment">// Now we can make a new buf</span>
<span class="kw">let</span> <span class="ident">buf</span> <span class="op">=</span> <span class="ident">Bytes</span>::<span class="ident">from</span>(<span class="kw-2">&amp;</span><span class="string">b&quot;hello world&quot;</span>[..]);
<span class="comment">// And make a MyBuffer out of it</span>
<span class="kw">let</span> <span class="ident">my_buf</span> <span class="op">=</span> <span class="ident">MyBuffer</span>::<span class="ident">from_buf</span>(<span class="ident">buf</span>);
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">my_buf</span>.<span class="number">0</span>, <span class="kw-2">&amp;</span><span class="string">b&quot;hello world&quot;</span>[..]);</pre>
</div>
<h2 id='required-methods' class='small-section-header'>
Required Methods<a href='#required-methods' class='anchor'></a>
</h2>
<div class='methods'>
<h3 id='tymethod.from_buf' class='method'><span id='from_buf.v' class='invisible'><code>fn <a href='#tymethod.from_buf' class='fnname'>from_buf</a>&lt;T&gt;(buf: T) -&gt; Self <span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;T: <a class="trait" href="../../bytes/buf/trait.IntoBuf.html" title="trait bytes::buf::IntoBuf">IntoBuf</a>,&nbsp;</span></code></span></h3><div class='docblock'><p>Creates a value from a buffer.</p>
<p>See the <a href="#">type-level documentation</a> for more details.</p>
<h1 id="examples-1" class="section-header"><a href="#examples-1">Examples</a></h1>
<p>Basic usage:</p>
<pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">bytes</span>::{<span class="ident">Bytes</span>, <span class="ident">IntoBuf</span>};
<span class="kw">use</span> <span class="ident">bytes</span>::<span class="ident">buf</span>::<span class="ident">FromBuf</span>;
<span class="kw">let</span> <span class="ident">buf</span> <span class="op">=</span> <span class="ident">Bytes</span>::<span class="ident">from</span>(<span class="kw-2">&amp;</span><span class="string">b&quot;hello world&quot;</span>[..]).<span class="ident">into_buf</span>();
<span class="kw">let</span> <span class="ident">vec</span> <span class="op">=</span> <span class="ident">Vec</span>::<span class="ident">from_buf</span>(<span class="ident">buf</span>);
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">vec</span>, <span class="kw-2">&amp;</span><span class="string">b&quot;hello world&quot;</span>[..]);</pre>
</div></div>
<h2 id='foreign-impls' class='small-section-header'>
Implementations on Foreign Types<a href='#foreign-impls' class='anchor'></a>
</h2>
<h3 id='impl-FromBuf' class='impl'><span class='in-band'><code>impl <a class="trait" href="../../bytes/buf/trait.FromBuf.html" title="trait bytes::buf::FromBuf">FromBuf</a> for <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="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.u8.html">u8</a>&gt;</code><a href='#impl-FromBuf' class='anchor'></a></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/bytes/buf/from_buf.rs.html#89-98' title='goto source code'>[src]</a></span></h3>
<span class='docblock autohide'><div class='impl-items'><h4 id='method.from_buf' class="method"><span id='from_buf.v-1' class='invisible'><code>fn <a href='#method.from_buf' class='fnname'>from_buf</a>&lt;T&gt;(buf: T) -&gt; Self <span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;T: <a class="trait" href="../../bytes/buf/trait.IntoBuf.html" title="trait bytes::buf::IntoBuf">IntoBuf</a>,&nbsp;</span></code></span><span class='out-of-band'><div class='ghost'></div><a class='srclink' href='../../src/bytes/buf/from_buf.rs.html#90-97' title='goto source code'>[src]</a></span></h4>
</div></span>
<h2 id='implementors' class='small-section-header'>
Implementors<a href='#implementors' class='anchor'></a>
</h2>
<ul class='item-list' id='implementors-list'>
<li><div class='out-of-band'><a class='srclink' href='../../src/bytes/buf/from_buf.rs.html#100-106' title='goto source code'>[src]</a></div><code>impl FromBuf for <a class="struct" href="../../bytes/struct.Bytes.html" title="struct bytes::Bytes">Bytes</a></code></li>
<li><div class='out-of-band'><a class='srclink' href='../../src/bytes/buf/from_buf.rs.html#108-117' title='goto source code'>[src]</a></div><code>impl FromBuf for <a class="struct" href="../../bytes/struct.BytesMut.html" title="struct bytes::BytesMut">BytesMut</a></code></li>
</ul><script type="text/javascript" async
src="../../implementors/bytes/buf/trait.FromBuf.js">
</script></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>&#9166;</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>
</div>
</div>
</aside>
<script>
window.rootPath = "../../";
window.currentCrate = "bytes";
</script>
<script src="../../main.js"></script>
<script defer src="../../search-index.js"></script>
</body>
</html>