107 lines
16 KiB
HTML
107 lines
16 KiB
HTML
<!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) -> Result<Response, Error>`"><title>tower - 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-ca0dd0c4.css"><meta name="rustdoc-vars" data-root-path="../" data-static-root-path="../static.files/" data-current-crate="tower" data-themes="" data-resource-suffix="" data-rustdoc-version="1.93.1 (01f6ddf75 2026-02-11) (Arch Linux rust 1:1.93.1-1)" data-channel="1.93.1" data-search-js="search-9e2438ea.js" data-stringdex-js="stringdex-a3946164.js" data-settings-js="settings-c38705f0.js" ><script src="../static.files/storage-e2aeef58.js"></script><script defer src="../crates.js"></script><script defer src="../static.files/main-a410ff4d.js"></script><noscript><link rel="stylesheet" href="../static.files/noscript-263c88ec.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 mod crate"><!--[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="#">Crate tower</a></h2></rustdoc-topbar><nav class="sidebar"><div class="sidebar-crate"><h2><a href="../tower/index.html">tower</a><span class="version">0.5.3</span></h2></div><div class="sidebar-elems"><ul class="block"><li><a id="all-types" href="all.html">All Items</a></li></ul><section id="rustdoc-toc"><h3><a href="#">Sections</a></h3><ul class="block top-toc"><li><a href="#overview" title="Overview">Overview</a><ul><li><a href="#the-tower-ecosystem" title="The Tower Ecosystem">The Tower Ecosystem</a></li></ul></li><li><a href="#usage" title="Usage">Usage</a><ul><li><a href="#library-support" title="Library Support">Library Support</a></li><li><a href="#getting-started" title="Getting Started">Getting Started</a></li><li><a href="#supported-rust-versions" title="Supported Rust Versions">Supported Rust Versions</a></li></ul></li></ul><h3><a href="#modules">Crate Items</a></h3><ul class="block"><li><a href="#modules" title="Modules">Modules</a></li><li><a href="#structs" title="Structs">Structs</a></li><li><a href="#traits" title="Traits">Traits</a></li><li><a href="#functions" title="Functions">Functions</a></li><li><a href="#types" title="Type Aliases">Type Aliases</a></li></ul></section><div id="rustdoc-modnav"></div></div></nav><div class="sidebar-resizer" title="Drag to resize sidebar"></div><main><div class="width-limiter"><section id="main-content" class="content"><div class="main-heading"><h1>Crate <span>tower</span> <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/tower/lib.rs.html#1-228">Source</a> </span></div><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p><code>async fn(Request) -> Result<Response, Error></code></p>
|
||
<h2 id="overview"><a class="doc-anchor" href="#overview">§</a>Overview</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" title="trait tower::Service"><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" title="mod tower::timeout"><code>timeout</code></a>, <a href="crate::limit::rate">rate limiting</a>, and <a href="crate::balance">load balancing</a>,
|
||
can be modeled as <a href="trait.Service.html" title="trait tower::Service"><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" title="trait tower::Layer"><code>Layer</code></a> trait, is used to compose
|
||
middleware with <a href="trait.Service.html" title="trait tower::Service"><code>Service</code></a>s. If a <a href="trait.Service.html" title="trait tower::Service"><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" title="trait tower::Layer"><code>Layer</code></a> is
|
||
a function taking a <a href="trait.Service.html" title="trait tower::Service"><code>Service</code></a> of one type and returning a <a href="trait.Service.html" title="trait tower::Service"><code>Service</code></a> of a
|
||
different type. The <a href="struct.ServiceBuilder.html" title="struct tower::ServiceBuilder"><code>ServiceBuilder</code></a> type is used to add middleware to a
|
||
service by composing it with multiple <a href="trait.Layer.html" title="trait tower::Layer"><code>Layer</code></a>s.</p>
|
||
<h3 id="the-tower-ecosystem"><a class="doc-anchor" href="#the-tower-ecosystem">§</a>The Tower Ecosystem</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" title="trait tower::Service"><code>Service</code></a> and <a href="trait.Layer.html" title="trait tower::Layer"><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" title="trait tower::ServiceExt">utilities</a> for working with <a href="trait.Service.html" title="trait tower::Service"><code>Service</code></a>s and <a href="trait.Layer.html" title="trait tower::Layer"><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 class="doc-anchor" href="#usage">§</a>Usage</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" title="trait tower::Service"><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" title="trait tower::Service"><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" title="trait tower::Service"><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" title="trait tower::Service"><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" title="trait tower::Service"><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 class="doc-anchor" href="#library-support">§</a>Library Support</h3>
|
||
<p>A number of third-party libraries support Tower and the <a href="trait.Service.html" title="trait tower::Service"><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>: 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 class="doc-anchor" href="#getting-started">§</a>Getting Started</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 = "0.4", features = ["full"] }</code></pre></div>
|
||
<p>Alternatively, you can only enable some features. For example, to enable
|
||
only the <a href="retry/index.html" title="mod tower::retry"><code>retry</code></a> and <a href="timeout/index.html" title="mod tower::timeout"><code>timeout</code></a> middleware, write:</p>
|
||
<div class="example-wrap"><pre class="language-toml"><code>tower = { version = "0.4", features = ["retry", "timeout"] }</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 class="doc-anchor" href="#supported-rust-versions">§</a>Supported Rust Versions</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.64.0.</p>
|
||
</div></details><h2 id="modules" class="section-header">Modules<a href="#modules" class="anchor">§</a></h2><dl class="item-table"><dt><a class="mod" href="builder/index.html" title="mod tower::builder">builder</a></dt><dd>Builder types to compose layers and services</dd><dt><a class="mod" href="layer/index.html" title="mod tower::layer">layer</a></dt><dd>A collection of <a href="trait.Layer.html" title="trait tower::Layer"><code>Layer</code></a> based tower services</dd><dt><a class="mod" href="make/index.html" title="mod tower::make">make</a></dt><dd>Trait aliases for Services that produce specific types of Responses.</dd><dt><a class="mod" href="retry/index.html" title="mod tower::retry">retry</a></dt><dd>Middleware for retrying “failed” requests.</dd><dt><a class="mod" href="timeout/index.html" title="mod tower::timeout">timeout</a></dt><dd>Middleware that applies a timeout to requests.</dd><dt><a class="mod" href="util/index.html" title="mod tower::util">util</a></dt><dd>Various utility types and functions that are generally used with Tower.</dd></dl><h2 id="structs" class="section-header">Structs<a href="#structs" class="anchor">§</a></h2><dl class="item-table"><dt><a class="struct" href="struct.ServiceBuilder.html" title="struct tower::ServiceBuilder">Service<wbr>Builder</a></dt><dd>Declaratively construct <a href="trait.Service.html" title="trait tower::Service"><code>Service</code></a> values.</dd></dl><h2 id="traits" class="section-header">Traits<a href="#traits" class="anchor">§</a></h2><dl class="item-table"><dt><a class="trait" href="trait.Layer.html" title="trait tower::Layer">Layer</a></dt><dd>Decorates a <a href="https://docs.rs/tower/*/tower/trait.Service.html"><code>Service</code></a>, transforming either the request or the response.</dd><dt><a class="trait" href="trait.MakeService.html" title="trait tower::MakeService">Make<wbr>Service</a></dt><dd>Creates new <a href="trait.Service.html" title="trait tower::Service"><code>Service</code></a> values.</dd><dt><a class="trait" href="trait.Service.html" title="trait tower::Service">Service</a></dt><dd>An asynchronous function from a <code>Request</code> to a <code>Response</code>.</dd><dt><a class="trait" href="trait.ServiceExt.html" title="trait tower::ServiceExt">Service<wbr>Ext</a></dt><dd>An extension trait for <code>Service</code>s that provides a variety of convenient
|
||
adapters</dd></dl><h2 id="functions" class="section-header">Functions<a href="#functions" class="anchor">§</a></h2><dl class="item-table"><dt><a class="fn" href="fn.service_fn.html" title="fn tower::service_fn">service_<wbr>fn</a></dt><dd>Returns a new <a href="util/struct.ServiceFn.html" title="struct tower::util::ServiceFn"><code>ServiceFn</code></a> with the given closure.</dd></dl><h2 id="types" class="section-header">Type Aliases<a href="#types" class="anchor">§</a></h2><dl class="item-table"><dt><a class="type" href="type.BoxError.html" title="type tower::BoxError">BoxError</a></dt><dd>Alias for a type-erased error type.</dd></dl><script type="text/json" id="notable-traits-data">{"&[u8]":"<h3>Notable traits for <code>&[<a class=\"primitive\" href=\"https://doc.rust-lang.org/1.93.1/std/primitive.u8.html\">u8</a>]</code></h3><pre><code><div class=\"where\">impl <a class=\"trait\" href=\"https://doc.rust-lang.org/1.93.1/std/io/trait.Read.html\" title=\"trait std::io::Read\">Read</a> for &[<a class=\"primitive\" href=\"https://doc.rust-lang.org/1.93.1/std/primitive.u8.html\">u8</a>]</div>"}</script></section></div></main></body></html> |