blob: 9581ee6115206f8755343de0aaf796d08147dba2 [file] [log] [blame]
<!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="github crates-io docs-rs"><meta name="keywords" content="rust, rustlang, rust-lang, inventory"><title>inventory - Rust</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceSerif4-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../FiraSans-Regular.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../FiraSans-Medium.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceCodePro-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceSerif4-Bold.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceCodePro-Semibold.ttf.woff2"><link rel="stylesheet" href="../normalize.css"><link rel="stylesheet" href="../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" href="../ayu.css" disabled><link rel="stylesheet" href="../dark.css" disabled><link rel="stylesheet" href="../light.css" id="themeStyle"><script id="default-settings" ></script><script src="../storage.js"></script><script defer src="../crates.js"></script><script defer src="../main.js"></script><noscript><link rel="stylesheet" href="../noscript.css"></noscript><link rel="alternate icon" type="image/png" href="../favicon-16x16.png"><link rel="alternate icon" type="image/png" href="../favicon-32x32.png"><link rel="icon" type="image/svg+xml" href="../favicon.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]--><nav class="mobile-topbar"><button class="sidebar-menu-toggle">&#9776;</button><a class="sidebar-logo" href="../inventory/index.html"><div class="logo-container"><img class="rust-logo" src="../rust-logo.svg" alt="logo"></div></a><h2></h2></nav><nav class="sidebar"><a class="sidebar-logo" href="../inventory/index.html"><div class="logo-container"><img class="rust-logo" src="../rust-logo.svg" alt="logo"></div></a><h2 class="location"><a href="#">Crate inventory</a></h2><div class="sidebar-elems"><ul class="block"><li class="version">Version 0.1.11</li><li><a id="all-types" href="all.html">All Items</a></li></ul><section><ul class="block"><li><a href="#macros">Macros</a></li><li><a href="#traits">Traits</a></li><li><a href="#types">Type Definitions</a></li></ul></section></div></nav><main><div class="width-limiter"><nav class="sub"><form class="search-form"><div class="search-container"><span></span><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><div id="help-button" title="help" tabindex="-1"><a href="../help.html">?</a></div><div id="settings-menu" tabindex="-1"><a href="../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../wheel.svg"></a></div></div></form></nav><section id="main-content" class="content"><div class="main-heading"><h1 class="fqn">Crate <a class="mod" href="#">inventory</a><button id="copy-path" onclick="copy_path(this)" title="Copy item path to clipboard"><img src="../clipboard.svg" width="19" height="18" alt="Copy item path"></button></h1><span class="out-of-band"><a class="srclink" href="../src/inventory/lib.rs.html#1-388">source</a> · <a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class="inner">&#x2212;</span>]</a></span></div><details class="rustdoc-toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p><a href="https://github.com/dtolnay/inventory"><img src="https://img.shields.io/badge/github-8da0cb?style=for-the-badge&amp;labelColor=555555&amp;logo=github" alt="github" /></a><a href="https://crates.io/crates/inventory"><img src="https://img.shields.io/badge/crates.io-fc8d62?style=for-the-badge&amp;labelColor=555555&amp;logo=rust" alt="crates-io" /></a><a href="https://docs.rs/inventory"><img src="https://img.shields.io/badge/docs.rs-66c2a5?style=for-the-badge&amp;labelColor=555555&amp;logoColor=white&amp;logo=data:image/svg+xml;base64,PHN2ZyByb2xlPSJpbWciIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgdmlld0JveD0iMCAwIDUxMiA1MTIiPjxwYXRoIGZpbGw9IiNmNWY1ZjUiIGQ9Ik00ODguNiAyNTAuMkwzOTIgMjE0VjEwNS41YzAtMTUtOS4zLTI4LjQtMjMuNC0zMy43bC0xMDAtMzcuNWMtOC4xLTMuMS0xNy4xLTMuMS0yNS4zIDBsLTEwMCAzNy41Yy0xNC4xIDUuMy0yMy40IDE4LjctMjMuNCAzMy43VjIxNGwtOTYuNiAzNi4yQzkuMyAyNTUuNSAwIDI2OC45IDAgMjgzLjlWMzk0YzAgMTMuNiA3LjcgMjYuMSAxOS45IDMyLjJsMTAwIDUwYzEwLjEgNS4xIDIyLjEgNS4xIDMyLjIgMGwxMDMuOS01MiAxMDMuOSA1MmMxMC4xIDUuMSAyMi4xIDUuMSAzMi4yIDBsMTAwLTUwYzEyLjItNi4xIDE5LjktMTguNiAxOS45LTMyLjJWMjgzLjljMC0xNS05LjMtMjguNC0yMy40LTMzLjd6TTM1OCAyMTQuOGwtODUgMzEuOXYtNjguMmw4NS0zN3Y3My4zek0xNTQgMTA0LjFsMTAyLTM4LjIgMTAyIDM4LjJ2LjZsLTEwMiA0MS40LTEwMi00MS40di0uNnptODQgMjkxLjFsLTg1IDQyLjV2LTc5LjFsODUtMzguOHY3NS40em0wLTExMmwtMTAyIDQxLjQtMTAyLTQxLjR2LS42bDEwMi0zOC4yIDEwMiAzOC4ydi42em0yNDAgMTEybC04NSA0Mi41di03OS4xbDg1LTM4Ljh2NzUuNHptMC0xMTJsLTEwMiA0MS40LTEwMi00MS40di0uNmwxMDItMzguMiAxMDIgMzguMnYuNnoiPjwvcGF0aD48L3N2Zz4K" alt="docs-rs" /></a></p>
<br>
<p><strong>Typed distributed plugin registration.</strong></p>
<p>This crate provides a way to set up a plugin registry into which plugins
can be registered from any source file linked into your application. There
does not need to be a central list of all the plugins.</p>
<h2 id="examples"><a href="#examples">Examples</a></h2>
<p>Suppose we are writing a command line flags library and want to allow any
source file in the application to register command line flags that are
relevant to it.</p>
<p>This is the flag registration style used by <a href="https://gflags.github.io/gflags/">gflags</a> and is better suited
for large scale development than maintaining a single central list of flags,
as the central list would become an endless source of merge conflicts in an
application developed simultaneously by thousands of developers.</p>
<h3 id="instantiating-the-plugin-registry"><a href="#instantiating-the-plugin-registry">Instantiating the plugin registry</a></h3>
<p>Let’s use a <code>struct Flag</code> as the plugin type, which will contain the short
name of the flag like <code>-v</code>, the full name like <code>--verbose</code>, and maybe other
information like argument type and help text. We instantiate a plugin
registry with an invocation of <code>inventory::collect!</code>.</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">pub struct </span>Flag {
short: char,
name: <span class="kw-2">&amp;</span><span class="lifetime">&#39;static </span>str,
<span class="comment">/* ... */
</span>}
<span class="kw">impl </span>Flag {
<span class="kw">pub fn </span>new(short: char, name: <span class="kw-2">&amp;</span><span class="lifetime">&#39;static </span>str) -&gt; <span class="self">Self </span>{
Flag { short, name }
}
}
<span class="macro">inventory::collect!</span>(Flag);</code></pre></div>
<p>This <code>collect!</code> call must be in the same crate that defines the plugin type.
This macro does not “run” anything so place it outside of any function body.</p>
<h3 id="registering-plugins"><a href="#registering-plugins">Registering plugins</a></h3>
<p>Now any crate with access to the <code>Flag</code> type can register flags as a plugin.
Plugins can be registered by the same crate that declares the plugin type,
or by any downstream crate.</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="macro">inventory::submit! </span>{
Flag::new(<span class="string">&#39;v&#39;</span>, <span class="string">&quot;verbose&quot;</span>)
}</code></pre></div>
<p>The <code>submit!</code> macro does not “run” anything so place it outside of any
function body. In particular, note that all <code>submit!</code> invocations across all
source files linked into your application all take effect simultaneously. A
<code>submit!</code> invocation is not a statement that needs to be called from <code>main</code>
in order to execute.</p>
<h3 id="iterating-over-plugins"><a href="#iterating-over-plugins">Iterating over plugins</a></h3>
<p>The value <code>inventory::iter::&lt;T&gt;</code> is an iterator with element type <code>&amp;'static T</code> that iterates over all plugins registered of type <code>T</code>.</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">for </span>flag <span class="kw">in </span>inventory::iter::&lt;Flag&gt; {
<span class="macro">println!</span>(<span class="string">&quot;-{}, --{}&quot;</span>, flag.short, flag.name);
}</code></pre></div>
<p>There is no guarantee about the order that plugins of the same type are
visited by the iterator. They may be visited in any order.</p>
</div></details><h2 id="macros" class="small-section-header"><a href="#macros">Macros</a></h2><div class="item-table"><div class="item-row"><div class="item-left module-item"><a class="macro" href="macro.collect.html" title="inventory::collect macro">collect</a></div><div class="item-right docblock-short">Associate a plugin registry with the specified type.</div></div><div class="item-row"><div class="item-left module-item"><a class="macro" href="macro.submit.html" title="inventory::submit macro">submit</a></div><div class="item-right docblock-short">Enter an element into the plugin registry corresponding to its type.</div></div></div><h2 id="traits" class="small-section-header"><a href="#traits">Traits</a></h2><div class="item-table"><div class="item-row"><div class="item-left module-item"><a class="trait" href="trait.Collect.html" title="inventory::Collect trait">Collect</a></div><div class="item-right docblock-short">Trait bound corresponding to types that can be iterated by inventory::iter.</div></div></div><h2 id="types" class="small-section-header"><a href="#types">Type Definitions</a></h2><div class="item-table"><div class="item-row"><div class="item-left module-item"><a class="type" href="type.iter.html" title="inventory::iter type">iter</a></div><div class="item-right docblock-short">An iterator over plugins registered of a given type.</div></div></div></section></div></main><div id="rustdoc-vars" data-root-path="../" data-current-crate="inventory" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.66.0-nightly (5c8bff74b 2022-10-21)" ></div></body></html>