blob: 5408f35c4a42c6b0007f314a5f26f4a1b0ec749c [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="`async fn(Request) -&gt; Result&lt;Response, Error&gt;`"><meta name="keywords" content="rust, rustlang, rust-lang, tower"><title>tower - 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="../tower/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="../tower/index.html"><div class="logo-container"><img class="rust-logo" src="../rust-logo.svg" alt="logo"></div></a><h2 class="location"><a href="#">Crate tower</a></h2><div class="sidebar-elems"><ul class="block"><li class="version">Version 0.4.13</li><li><a id="all-types" href="all.html">All Items</a></li></ul><section><ul class="block"><li><a href="#modules">Modules</a></li><li><a href="#structs">Structs</a></li><li><a href="#traits">Traits</a></li><li><a href="#functions">Functions</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="#">tower</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/tower/lib.rs.html#1-242">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><code>async fn(Request) -&gt; Result&lt;Response, Error&gt;</code></p>
<h2 id="overview"><a href="#overview">Overview</a></h2>
<p>Tower is a library of modular and reusable components for building
robust networking clients and servers.</p>
<p>Tower provides a simple core abstraction, the <a href="trait.Service.html"><code>Service</code></a> trait, which
represents an asynchronous function taking a request and returning either a
response or an error. This abstraction can be used to model both clients and
servers.</p>
<p>Generic components, like <a href="timeout/index.html">timeouts</a>, <a href="limit/rate/index.html">rate limiting</a>, and <a href="balance/index.html">load balancing</a>,
can be modeled as <a href="trait.Service.html"><code>Service</code></a>s that wrap some inner service and apply
additional behavior before or after the inner service is called. This allows
implementing these components in a protocol-agnostic, composable way. Typically,
such services are referred to as <em>middleware</em>.</p>
<p>An additional abstraction, the <a href="trait.Layer.html"><code>Layer</code></a> trait, is used to compose
middleware with <a href="trait.Service.html"><code>Service</code></a>s. If a <a href="trait.Service.html"><code>Service</code></a> can be thought of as an
asynchronous function from a request type to a response type, a <a href="trait.Layer.html"><code>Layer</code></a> is
a function taking a <a href="trait.Service.html"><code>Service</code></a> of one type and returning a <a href="trait.Service.html"><code>Service</code></a> of a
different type. The <a href="struct.ServiceBuilder.html"><code>ServiceBuilder</code></a> type is used to add middleware to a
service by composing it with multiple <a href="trait.Layer.html"><code>Layer</code></a>s.</p>
<h3 id="the-tower-ecosystem"><a href="#the-tower-ecosystem">The Tower Ecosystem</a></h3>
<p>Tower is made up of the following crates:</p>
<ul>
<li><a href="https://crates.io/crates/tower"><code>tower</code></a> (this crate)</li>
<li><a href="https://crates.io/crates/tower-service"><code>tower-service</code></a></li>
<li><a href="https://crates.io/crates/tower-layer"><code>tower-layer</code></a></li>
<li><a href="https://crates.io/crates/tower-test"><code>tower-test</code></a></li>
</ul>
<p>Since the <a href="trait.Service.html"><code>Service</code></a> and <a href="trait.Layer.html"><code>Layer</code></a> traits are important integration points
for all libraries using Tower, they are kept as stable as possible, and
breaking changes are made rarely. Therefore, they are defined in separate
crates, <a href="https://crates.io/crates/tower-service"><code>tower-service</code></a> and <a href="https://crates.io/crates/tower-layer"><code>tower-layer</code></a>. This crate contains
re-exports of those core traits, implementations of commonly-used
middleware, and <a href="trait.ServiceExt.html">utilities</a> for working with <a href="trait.Service.html"><code>Service</code></a>s and <a href="trait.Layer.html"><code>Layer</code></a>s.
Finally, the <a href="https://crates.io/crates/tower-test"><code>tower-test</code></a> crate provides tools for testing programs using
Tower.</p>
<h2 id="usage"><a href="#usage">Usage</a></h2>
<p>Tower provides an abstraction layer, and generic implementations of various
middleware. This means that the <code>tower</code> crate on its own does <em>not</em> provide
a working implementation of a network client or server. Instead, Tower’s
<a href="trait.Service.html"><code>Service</code> trait</a> provides an integration point between
application code, libraries providing middleware implementations, and
libraries that implement servers and/or clients for various network
protocols.</p>
<p>Depending on your particular use case, you might use Tower in several ways:</p>
<ul>
<li>
<p><strong>Implementing application logic</strong> for a networked program. You might
use the <a href="trait.Service.html"><code>Service</code></a> trait to model your application’s behavior, and use
the middleware <a href="#modules">provided by this crate</a> and by other libraries
to add functionality to clients and servers provided by one or more
protocol implementations.</p>
</li>
<li>
<p><strong>Implementing middleware</strong> to add custom behavior to network clients and
servers in a reusable manner. This might be general-purpose middleware
(and if it is, please consider releasing your middleware as a library for
other Tower users!) or application-specific behavior that needs to be
shared between multiple clients or servers.</p>
</li>
<li>
<p><strong>Implementing a network protocol</strong>. Libraries that implement network
protocols (such as HTTP) can depend on <code>tower-service</code> to use the
<a href="trait.Service.html"><code>Service</code></a> trait as an integration point between the protocol and user
code. For example, a client for some protocol might implement <a href="trait.Service.html"><code>Service</code></a>,
allowing users to add arbitrary Tower middleware to those clients.
Similarly, a server might be created from a user-provided <a href="trait.Service.html"><code>Service</code></a>.</p>
<p>Additionally, when a network protocol requires functionality already
provided by existing Tower middleware, a protocol implementation might use
Tower middleware internally, as well as as an integration point.</p>
</li>
</ul>
<h3 id="library-support"><a href="#library-support">Library Support</a></h3>
<p>A number of third-party libraries support Tower and the <a href="trait.Service.html"><code>Service</code></a> trait.
The following is an incomplete list of such libraries:</p>
<ul>
<li><a href="https://crates.io/crates/hyper"><code>hyper</code></a>: A fast and correct low-level HTTP implementation.</li>
<li><a href="https://crates.io/crates/tonic"><code>tonic</code></a>: A <a href="https://grpc.io">gRPC-over-HTTP/2</a> implementation built on top of
<a href="https://crates.io/crates/hyper"><code>hyper</code></a>. See <a href="https://github.com/hyperium/tonic/tree/master/examples/src/tower">here</a> for examples of using <a href="https://crates.io/crates/tonic"><code>tonic</code></a> with
Tower.</li>
<li><a href="https://crates.io/crates/warp"><code>warp</code></a>: A lightweight, composable web framework. See
<a href="https://docs.rs/warp/0.2.5/warp/fn.service.html">here</a> for details on using <a href="https://crates.io/crates/warp"><code>warp</code></a> with Tower.</li>
<li><a href="https://crates.io/crates/tower-lsp"><code>tower-lsp</code></a> and its fork, <a href="https://crates.io/crates/lspower"><code>lspower</code></a>: implementations of the <a href="https://microsoft.github.io/language-server-protocol/">Language
Server Protocol</a> based on Tower.</li>
</ul>
<p>If you’re the maintainer of a crate that supports Tower, we’d love to add
your crate to this list! Please <a href="https://github.com/tower-rs/tower/compare">open a PR</a> adding a brief description of
your library!</p>
<h3 id="getting-started"><a href="#getting-started">Getting Started</a></h3>
<p>If you’re brand new to Tower and want to start with the basics, we recommend you
check out some of our <a href="https://github.com/tower-rs/tower/tree/master/guides">guides</a>.</p>
<p>The various middleware implementations provided by this crate are feature
flagged, so that users can only compile the parts of Tower they need. By
default, all the optional middleware are disabled.</p>
<p>To get started using all of Tower’s optional middleware, add this to your
<code>Cargo.toml</code>:</p>
<div class="example-wrap"><pre class="language-toml"><code>tower = { version = &quot;0.4&quot;, features = [&quot;full&quot;] }</code></pre></div>
<p>Alternatively, you can only enable some features. For example, to enable
only the <a href="crate::retry"><code>retry</code></a> and <a href="timeout/index.html"><code>timeout</code></a> middleware, write:</p>
<div class="example-wrap"><pre class="language-toml"><code>tower = { version = &quot;0.4&quot;, features = [&quot;retry&quot;, &quot;timeout&quot;] }</code></pre></div>
<p>See <a href="#modules">here</a> for a complete list of all middleware provided by
Tower.</p>
<h3 id="supported-rust-versions"><a href="#supported-rust-versions">Supported Rust Versions</a></h3>
<p>Tower will keep a rolling MSRV (minimum supported Rust version) policy of <strong>at
least</strong> 6 months. When increasing the MSRV, the new Rust version must have been
released at least six months ago. The current MSRV is 1.49.0.</p>
</div></details><h2 id="modules" class="small-section-header"><a href="#modules">Modules</a></h2><div class="item-table"><div class="item-row"><div class="item-left module-item"><a class="mod" href="balance/index.html" title="tower::balance mod">balance</a></div><div class="item-right docblock-short">Middleware that allows balancing load among multiple services.</div></div><div class="item-row"><div class="item-left module-item"><a class="mod" href="buffer/index.html" title="tower::buffer mod">buffer</a></div><div class="item-right docblock-short">Middleware that provides a buffered mpsc channel to a service.</div></div><div class="item-row"><div class="item-left module-item"><a class="mod" href="builder/index.html" title="tower::builder mod">builder</a></div><div class="item-right docblock-short">Builder types to compose layers and services</div></div><div class="item-row"><div class="item-left module-item"><a class="mod" href="discover/index.html" title="tower::discover mod">discover</a></div><div class="item-right docblock-short">Service discovery</div></div><div class="item-row"><div class="item-left module-item"><a class="mod" href="layer/index.html" title="tower::layer mod">layer</a></div><div class="item-right docblock-short">A collection of <a href="trait.Layer.html"><code>Layer</code></a> based tower services</div></div><div class="item-row"><div class="item-left module-item"><a class="mod" href="limit/index.html" title="tower::limit mod">limit</a></div><div class="item-right docblock-short">Tower middleware for limiting requests.</div></div><div class="item-row"><div class="item-left module-item"><a class="mod" href="load/index.html" title="tower::load mod">load</a></div><div class="item-right docblock-short">Service load measurement</div></div><div class="item-row"><div class="item-left module-item"><a class="mod" href="make/index.html" title="tower::make mod">make</a></div><div class="item-right docblock-short">Trait aliases for Services that produce specific types of Responses.</div></div><div class="item-row"><div class="item-left module-item"><a class="mod" href="ready_cache/index.html" title="tower::ready_cache mod">ready_cache</a></div><div class="item-right docblock-short">A cache of services</div></div><div class="item-row"><div class="item-left module-item"><a class="mod" href="timeout/index.html" title="tower::timeout mod">timeout</a></div><div class="item-right docblock-short">Middleware that applies a timeout to requests.</div></div><div class="item-row"><div class="item-left module-item"><a class="mod" href="util/index.html" title="tower::util mod">util</a></div><div class="item-right docblock-short">Various utility types and functions that are generally used with Tower.</div></div></div><h2 id="structs" class="small-section-header"><a href="#structs">Structs</a></h2><div class="item-table"><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.ServiceBuilder.html" title="tower::ServiceBuilder struct">ServiceBuilder</a></div><div class="item-right docblock-short">Declaratively construct <a href="trait.Service.html"><code>Service</code></a> values.</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.Layer.html" title="tower::Layer trait">Layer</a></div><div class="item-right docblock-short">Decorates a <a href="https://docs.rs/tower/latest/tower/trait.Service.html"><code>Service</code></a>, transforming either the request or the response.</div></div><div class="item-row"><div class="item-left module-item"><a class="trait" href="trait.MakeService.html" title="tower::MakeService trait">MakeService</a></div><div class="item-right docblock-short">Creates new <a href="trait.Service.html" title="Service"><code>Service</code></a> values.</div></div><div class="item-row"><div class="item-left module-item"><a class="trait" href="trait.Service.html" title="tower::Service trait">Service</a></div><div class="item-right docblock-short">An asynchronous function from a <code>Request</code> to a <code>Response</code>.</div></div><div class="item-row"><div class="item-left module-item"><a class="trait" href="trait.ServiceExt.html" title="tower::ServiceExt trait">ServiceExt</a></div><div class="item-right docblock-short">An extension trait for <code>Service</code>s that provides a variety of convenient
adapters</div></div></div><h2 id="functions" class="small-section-header"><a href="#functions">Functions</a></h2><div class="item-table"><div class="item-row"><div class="item-left module-item"><a class="fn" href="fn.service_fn.html" title="tower::service_fn fn">service_fn</a></div><div class="item-right docblock-short">Returns a new <a href="util/struct.ServiceFn.html" title="ServiceFn"><code>ServiceFn</code></a> with the given closure.</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.BoxError.html" title="tower::BoxError type">BoxError</a></div><div class="item-right docblock-short">Alias for a type-erased error type.</div></div></div></section></div></main><div id="rustdoc-vars" data-root-path="../" data-current-crate="tower" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.66.0-nightly (5c8bff74b 2022-10-21)" ></div></body></html>