wasmer/rustdoc/scroll/ctx/index.html
2019-09-06 15:57:44 -07:00

51 lines
13 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="API documentation for the Rust `ctx` mod in crate `scroll`."><meta name="keywords" content="rust, rustlang, rust-lang, ctx"><title>scroll::ctx - 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="../../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">&#9776;</div><a href='../../scroll/index.html'><div class='logo-container'><img src='../../rust-logo.png' alt='logo'></div></a><p class='location'>Module ctx</p><div class="sidebar-elems"><div class="block items"><ul><li><a href="#enums">Enums</a></li><li><a href="#constants">Constants</a></li><li><a href="#traits">Traits</a></li></ul></div><p class='location'><a href='../index.html'>scroll</a></p><script>window.sidebarCurrent = {name: 'ctx', ty: 'mod', 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"><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'>&#x2212;</span>]</a></span><a class='srclink' href='../../src/scroll/ctx.rs.html#1-668' title='goto source code'>[src]</a></span><span class='in-band'>Module <a href='../index.html'>scroll</a>::<wbr><a class="mod" href=''>ctx</a></span></h1><div class='docblock'><p>Generic context-aware conversion traits, for automatic <em>downstream</em> extension of <code>Pread</code>, et. al</p>
<h1 id="discussion" class="section-header"><a href="#discussion">Discussion</a></h1>
<p>Implementors of <code>TryFromCtx</code> automatically grant any client user of <code>pread, pwrite, gread, gwrite</code> the ability to parse their structure out of the source it has been implemented for, typically <code>&amp;[u8]</code>.</p>
<p>The implementor only needs to specify the error type, and the type of their size, and then implement the parsing/marshalling logic given a byte sequence, starting at the offset <code>pread</code>, et. al was called at, with the context you have implemented it for.</p>
<p>Returning the size allows dynamic content (e.g., <code>&amp;str</code>s) to be parsed alongside fixed size content (e.g., <code>u64</code>). The parsing context is any information you the implementor need to correctly parse out your datatype - this could be the endianness of the type, more offsets, or other complex data. The only requirement is that your <code>Ctx</code> be <code>Copy</code>, and hence encourages lightweight contexts (but this isn't required of course).</p>
<h1 id="example" class="section-header"><a href="#example">Example</a></h1>
<p>Suppose we have a datatype and we want to specify how to parse or serialize this datatype out of some arbitrary
byte buffer. In order to do this, we need to provide a <code>TryFromCtx</code> impl for our datatype. In particular, if we
do this for the <code>[u8]</code> target, with a &quot;parsing contex&quot;, <code>YourCtx</code>, you will automatically get access to
calling <code>pread_with::&lt;YourDatatype&gt;(offset, your_ctx)</code> on arrays of bytes.</p>
<p>In the example below, we implement <code>TryFromCtx</code> using the <code>Endian</code> parsing context provided by <code>scroll</code>, which is used to specifying the endianness at which numbers should be parsed, but you could provide anything, as long as it implements <code>Copy</code>.</p>
<div class="example-wrap"><pre class="rust rust-example-rendered">
<span class="kw">use</span> <span class="ident">scroll</span>::{<span class="self">self</span>, <span class="ident">ctx</span>, <span class="ident">Endian</span>, <span class="ident">Pread</span>, <span class="ident">BE</span>};
<span class="kw">struct</span> <span class="ident">Data</span><span class="op">&lt;</span><span class="lifetime">&#39;a</span><span class="op">&gt;</span> {
<span class="ident">name</span>: <span class="kw-2">&amp;</span><span class="lifetime">&#39;a</span> <span class="ident">str</span>,
<span class="ident">id</span>: <span class="ident">u32</span>,
}
<span class="kw">impl</span><span class="op">&lt;</span><span class="lifetime">&#39;a</span><span class="op">&gt;</span> <span class="ident">ctx</span>::<span class="ident">TryFromCtx</span><span class="op">&lt;</span><span class="lifetime">&#39;a</span>, <span class="ident">Endian</span><span class="op">&gt;</span> <span class="kw">for</span> <span class="ident">Data</span><span class="op">&lt;</span><span class="lifetime">&#39;a</span><span class="op">&gt;</span> {
<span class="kw">type</span> <span class="ident">Error</span> <span class="op">=</span> <span class="ident">scroll</span>::<span class="ident">Error</span>;
<span class="kw">type</span> <span class="ident">Size</span> <span class="op">=</span> <span class="ident">usize</span>;
<span class="kw">fn</span> <span class="ident">try_from_ctx</span> (<span class="ident">src</span>: <span class="kw-2">&amp;</span><span class="lifetime">&#39;a</span> [<span class="ident">u8</span>], <span class="ident">ctx</span>: <span class="ident">Endian</span>)
<span class="op">-</span><span class="op">&gt;</span> <span class="prelude-ty">Result</span><span class="op">&lt;</span>(<span class="self">Self</span>, <span class="self">Self</span>::<span class="ident">Size</span>), <span class="self">Self</span>::<span class="ident">Error</span><span class="op">&gt;</span> {
<span class="kw">let</span> <span class="ident">name</span> <span class="op">=</span> <span class="ident">src</span>.<span class="ident">pread</span>::<span class="op">&lt;</span><span class="kw-2">&amp;</span><span class="ident">str</span><span class="op">&gt;</span>(<span class="number">0</span>)<span class="question-mark">?</span>;
<span class="kw">let</span> <span class="ident">id</span> <span class="op">=</span> <span class="ident">src</span>.<span class="ident">pread_with</span>(<span class="ident">name</span>.<span class="ident">len</span>() <span class="op">+</span> <span class="number">1</span>, <span class="ident">ctx</span>)<span class="question-mark">?</span>;
<span class="prelude-val">Ok</span>((<span class="ident">Data</span> { <span class="ident">name</span>: <span class="ident">name</span>, <span class="ident">id</span>: <span class="ident">id</span> }, <span class="ident">name</span>.<span class="ident">len</span>() <span class="op">+</span> <span class="number">1</span> <span class="op">+</span> <span class="number">4</span>))
}
}
<span class="kw">let</span> <span class="ident">bytes</span> <span class="op">=</span> <span class="string">b&quot;UserName\x00\x01\x02\x03\x04&quot;</span>;
<span class="kw">let</span> <span class="ident">data</span> <span class="op">=</span> <span class="ident">bytes</span>.<span class="ident">pread_with</span>::<span class="op">&lt;</span><span class="ident">Data</span><span class="op">&gt;</span>(<span class="number">0</span>, <span class="ident">BE</span>).<span class="ident">unwrap</span>();
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">data</span>.<span class="ident">id</span>, <span class="number">0x01020304</span>);
<span class="macro">assert_eq</span><span class="macro">!</span>(<span class="ident">data</span>.<span class="ident">name</span>.<span class="ident">to_string</span>(), <span class="string">&quot;UserName&quot;</span>.<span class="ident">to_string</span>());
</pre></div>
</div><h2 id='enums' class='section-header'><a href="#enums">Enums</a></h2>
<table><tr class='module-item'><td><a class="enum" href="enum.StrCtx.html" title='scroll::ctx::StrCtx enum'>StrCtx</a></td><td class='docblock-short'><p>The parsing context for converting a byte sequence to a <code>&amp;str</code></p>
</td></tr></table><h2 id='constants' class='section-header'><a href="#constants">Constants</a></h2>
<table><tr class='module-item'><td><a class="constant" href="constant.NULL.html" title='scroll::ctx::NULL constant'>NULL</a></td><td class='docblock-short'><p>A C-style, null terminator based delimiter</p>
</td></tr><tr class='module-item'><td><a class="constant" href="constant.RET.html" title='scroll::ctx::RET constant'>RET</a></td><td class='docblock-short'><p>A newline-based delimiter</p>
</td></tr><tr class='module-item'><td><a class="constant" href="constant.SPACE.html" title='scroll::ctx::SPACE constant'>SPACE</a></td><td class='docblock-short'><p>A space-based delimiter</p>
</td></tr><tr class='module-item'><td><a class="constant" href="constant.TAB.html" title='scroll::ctx::TAB constant'>TAB</a></td><td class='docblock-short'><p>A tab-based delimiter</p>
</td></tr></table><h2 id='traits' class='section-header'><a href="#traits">Traits</a></h2>
<table><tr class='module-item'><td><a class="trait" href="trait.FromCtx.html" title='scroll::ctx::FromCtx trait'>FromCtx</a></td><td class='docblock-short'><p>Reads <code>Self</code> from <code>This</code> using the context <code>Ctx</code>; must <em>not</em> fail</p>
</td></tr><tr class='module-item'><td><a class="trait" href="trait.IntoCtx.html" title='scroll::ctx::IntoCtx trait'>IntoCtx</a></td><td class='docblock-short'><p>Writes <code>Self</code> into <code>This</code> using the context <code>Ctx</code></p>
</td></tr><tr class='module-item'><td><a class="trait" href="trait.MeasureWith.html" title='scroll::ctx::MeasureWith trait'>MeasureWith</a></td><td class='docblock-short'><p>A trait for measuring how large something is; for a byte sequence, it will be its length.</p>
</td></tr><tr class='module-item'><td><a class="trait" href="trait.SizeWith.html" title='scroll::ctx::SizeWith trait'>SizeWith</a></td><td class='docblock-short'><p>Gets the size of <code>Self</code> with a <code>Ctx</code>, and in <code>Self::Units</code>. Implementors can then call <code>Gread</code> related functions</p>
</td></tr><tr class='module-item'><td><a class="trait" href="trait.TryFromCtx.html" title='scroll::ctx::TryFromCtx trait'>TryFromCtx</a></td><td class='docblock-short'><p>Tries to read <code>Self</code> from <code>This</code> using the context <code>Ctx</code></p>
</td></tr><tr class='module-item'><td><a class="trait" href="trait.TryIntoCtx.html" title='scroll::ctx::TryIntoCtx trait'>TryIntoCtx</a></td><td class='docblock-short'><p>Tries to write <code>Self</code> into <code>This</code> using the context <code>Ctx</code></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>&#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><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 = "scroll";</script><script src="../../aliases.js"></script><script src="../../main.js"></script><script defer src="../../search-index.js"></script></body></html>