blob: 2d15a5fc03045f034fbe5bd4dd851d74798129f1 [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="An extension trait for `Service`s that provides a variety of convenient adapters"><meta name="keywords" content="rust, rustlang, rust-lang, ServiceExt"><title>ServiceExt in 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="sidebar-items.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 trait"><!--[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="#">ServiceExt</a></h2><div class="sidebar-elems"><section><h3><a href="#provided-methods">Provided Methods</a></h3><ul class="block"><li><a href="#method.and_then">and_then</a></li><li><a href="#method.boxed">boxed</a></li><li><a href="#method.boxed_clone">boxed_clone</a></li><li><a href="#method.call_all">call_all</a></li><li><a href="#method.map_err">map_err</a></li><li><a href="#method.map_future">map_future</a></li><li><a href="#method.map_request">map_request</a></li><li><a href="#method.map_response">map_response</a></li><li><a href="#method.map_result">map_result</a></li><li><a href="#method.oneshot">oneshot</a></li><li><a href="#method.ready">ready</a></li><li><a href="#method.ready_and">ready_and</a></li><li><a href="#method.ready_oneshot">ready_oneshot</a></li><li><a href="#method.then">then</a></li></ul><h3><a href="#implementors">Implementors</a></h3></section><h2><a href="index.html">In tower</a></h2></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">Trait <a href="index.html">tower</a>::<wbr><a class="trait" href="#">ServiceExt</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/util/mod.rs.html#65-1053">source</a> · <a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class="inner">&#x2212;</span>]</a></span></div><div class="item-decl"><pre class="rust trait"><code>pub trait ServiceExt&lt;Request&gt;: <a class="trait" href="trait.Service.html" title="trait tower::Service">Service</a>&lt;Request&gt; {
<details class="rustdoc-toggle type-contents-toggle"><summary class="hideme"><span>Show 14 methods</span></summary> fn <a href="#method.ready" class="fnname">ready</a>(&amp;mut self) -&gt; <a class="struct" href="util/struct.Ready.html" title="struct tower::util::Ready">Ready</a>&lt;'_, Self, Request&gt;<span class="notable-traits"><span class="notable-traits-tooltip"><span class="notable-traits-tooltiptext"><span class="docblock"><span class="notable">Notable traits for <a class="struct" href="util/struct.Ready.html" title="struct tower::util::Ready">Ready</a>&lt;'a, T, Request&gt;</span><code class="content"><span class="where fmt-newline">impl&lt;'a, T, Request&gt; <a class="trait" href="https://doc.rust-lang.org/nightly/core/future/future/trait.Future.html" title="trait core::future::future::Future">Future</a> for <a class="struct" href="util/struct.Ready.html" title="struct tower::util::Ready">Ready</a>&lt;'a, T, Request&gt;<span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;T: <a class="trait" href="trait.Service.html" title="trait tower::Service">Service</a>&lt;Request&gt;,</span></span><span class="where fmt-newline"> type <a href="https://doc.rust-lang.org/nightly/core/future/future/trait.Future.html#associatedtype.Output" class="associatedtype">Output</a> = <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.reference.html">&amp;'a mut </a>T, T::<a class="associatedtype" href="trait.Service.html#associatedtype.Error" title="type tower::Service::Error">Error</a>&gt;;</span></code></span></span></span></span><br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="where">where<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Self: <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a></span>,
{ ... }
<span class="item-spacer"></span> fn <a href="#method.ready_and" class="fnname">ready_and</a>(&amp;mut self) -&gt; <a class="type" href="util/type.ReadyAnd.html" title="type tower::util::ReadyAnd">ReadyAnd</a>&lt;'_, Self, Request&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="where">where<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Self: <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a></span>,
{ ... }
<span class="item-spacer"></span> fn <a href="#method.ready_oneshot" class="fnname">ready_oneshot</a>(self) -&gt; <a class="struct" href="util/struct.ReadyOneshot.html" title="struct tower::util::ReadyOneshot">ReadyOneshot</a>&lt;Self, Request&gt;<span class="notable-traits"><span class="notable-traits-tooltip"><span class="notable-traits-tooltiptext"><span class="docblock"><span class="notable">Notable traits for <a class="struct" href="util/struct.ReadyOneshot.html" title="struct tower::util::ReadyOneshot">ReadyOneshot</a>&lt;T, Request&gt;</span><code class="content"><span class="where fmt-newline">impl&lt;T, Request&gt; <a class="trait" href="https://doc.rust-lang.org/nightly/core/future/future/trait.Future.html" title="trait core::future::future::Future">Future</a> for <a class="struct" href="util/struct.ReadyOneshot.html" title="struct tower::util::ReadyOneshot">ReadyOneshot</a>&lt;T, Request&gt;<span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;T: <a class="trait" href="trait.Service.html" title="trait tower::Service">Service</a>&lt;Request&gt;,</span></span><span class="where fmt-newline"> type <a href="https://doc.rust-lang.org/nightly/core/future/future/trait.Future.html#associatedtype.Output" class="associatedtype">Output</a> = <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;T, T::<a class="associatedtype" href="trait.Service.html#associatedtype.Error" title="type tower::Service::Error">Error</a>&gt;;</span></code></span></span></span></span><br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="where">where<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Self: <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a></span>,
{ ... }
<span class="item-spacer"></span> fn <a href="#method.oneshot" class="fnname">oneshot</a>(self, req: Request) -&gt; <a class="struct" href="util/struct.Oneshot.html" title="struct tower::util::Oneshot">Oneshot</a>&lt;Self, Request&gt;<span class="notable-traits"><span class="notable-traits-tooltip"><span class="notable-traits-tooltiptext"><span class="docblock"><span class="notable">Notable traits for <a class="struct" href="util/struct.Oneshot.html" title="struct tower::util::Oneshot">Oneshot</a>&lt;S, Req&gt;</span><code class="content"><span class="where fmt-newline">impl&lt;S, Req&gt; <a class="trait" href="https://doc.rust-lang.org/nightly/core/future/future/trait.Future.html" title="trait core::future::future::Future">Future</a> for <a class="struct" href="util/struct.Oneshot.html" title="struct tower::util::Oneshot">Oneshot</a>&lt;S, Req&gt;<span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;S: <a class="trait" href="trait.Service.html" title="trait tower::Service">Service</a>&lt;Req&gt;,</span></span><span class="where fmt-newline"> type <a href="https://doc.rust-lang.org/nightly/core/future/future/trait.Future.html#associatedtype.Output" class="associatedtype">Output</a> = <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;S::<a class="associatedtype" href="trait.Service.html#associatedtype.Response" title="type tower::Service::Response">Response</a>, S::<a class="associatedtype" href="trait.Service.html#associatedtype.Error" title="type tower::Service::Error">Error</a>&gt;;</span></code></span></span></span></span><br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="where">where<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Self: <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a></span>,
{ ... }
<span class="item-spacer"></span> fn <a href="#method.call_all" class="fnname">call_all</a>&lt;S&gt;(self, reqs: S) -&gt; <a class="struct" href="util/struct.CallAll.html" title="struct tower::util::CallAll">CallAll</a>&lt;Self, S&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="where">where<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Self: <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Self::<a class="associatedtype" href="trait.Service.html#associatedtype.Error" title="type tower::Service::Error">Error</a>: <a class="trait" href="https://doc.rust-lang.org/nightly/core/convert/trait.Into.html" title="trait core::convert::Into">Into</a>&lt;<a class="type" href="type.BoxError.html" title="type tower::BoxError">BoxError</a>&gt;,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;S: <a class="trait" href="../futures_core/stream/trait.Stream.html" title="trait futures_core::stream::Stream">Stream</a>&lt;Item = Request&gt;</span>,
{ ... }
<span class="item-spacer"></span> fn <a href="#method.and_then" class="fnname">and_then</a>&lt;F&gt;(self, f: F) -&gt; <a class="struct" href="util/struct.AndThen.html" title="struct tower::util::AndThen">AndThen</a>&lt;Self, F&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="where">where<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Self: <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;F: <a class="trait" href="https://doc.rust-lang.org/nightly/core/clone/trait.Clone.html" title="trait core::clone::Clone">Clone</a></span>,
{ ... }
<span class="item-spacer"></span> fn <a href="#method.map_response" class="fnname">map_response</a>&lt;F, Response&gt;(self, f: F) -&gt; <a class="struct" href="util/struct.MapResponse.html" title="struct tower::util::MapResponse">MapResponse</a>&lt;Self, F&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="where">where<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Self: <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;F: <a class="trait" href="https://doc.rust-lang.org/nightly/core/ops/function/trait.FnOnce.html" title="trait core::ops::function::FnOnce">FnOnce</a>(Self::<a class="associatedtype" href="trait.Service.html#associatedtype.Response" title="type tower::Service::Response">Response</a>) -&gt; Response + <a class="trait" href="https://doc.rust-lang.org/nightly/core/clone/trait.Clone.html" title="trait core::clone::Clone">Clone</a></span>,
{ ... }
<span class="item-spacer"></span> fn <a href="#method.map_err" class="fnname">map_err</a>&lt;F, Error&gt;(self, f: F) -&gt; <a class="struct" href="util/struct.MapErr.html" title="struct tower::util::MapErr">MapErr</a>&lt;Self, F&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="where">where<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Self: <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;F: <a class="trait" href="https://doc.rust-lang.org/nightly/core/ops/function/trait.FnOnce.html" title="trait core::ops::function::FnOnce">FnOnce</a>(Self::<a class="associatedtype" href="trait.Service.html#associatedtype.Error" title="type tower::Service::Error">Error</a>) -&gt; Error + <a class="trait" href="https://doc.rust-lang.org/nightly/core/clone/trait.Clone.html" title="trait core::clone::Clone">Clone</a></span>,
{ ... }
<span class="item-spacer"></span> fn <a href="#method.map_result" class="fnname">map_result</a>&lt;F, Response, Error&gt;(self, f: F) -&gt; <a class="struct" href="util/struct.MapResult.html" title="struct tower::util::MapResult">MapResult</a>&lt;Self, F&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="where">where<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Self: <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Error: <a class="trait" href="https://doc.rust-lang.org/nightly/core/convert/trait.From.html" title="trait core::convert::From">From</a>&lt;Self::<a class="associatedtype" href="trait.Service.html#associatedtype.Error" title="type tower::Service::Error">Error</a>&gt;,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;F: <a class="trait" href="https://doc.rust-lang.org/nightly/core/ops/function/trait.FnOnce.html" title="trait core::ops::function::FnOnce">FnOnce</a>(<a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;Self::<a class="associatedtype" href="trait.Service.html#associatedtype.Response" title="type tower::Service::Response">Response</a>, Self::<a class="associatedtype" href="trait.Service.html#associatedtype.Error" title="type tower::Service::Error">Error</a>&gt;) -&gt; <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;Response, Error&gt; + <a class="trait" href="https://doc.rust-lang.org/nightly/core/clone/trait.Clone.html" title="trait core::clone::Clone">Clone</a></span>,
{ ... }
<span class="item-spacer"></span> fn <a href="#method.map_request" class="fnname">map_request</a>&lt;F, NewRequest&gt;(self, f: F) -&gt; <a class="struct" href="util/struct.MapRequest.html" title="struct tower::util::MapRequest">MapRequest</a>&lt;Self, F&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="where">where<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Self: <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;F: <a class="trait" href="https://doc.rust-lang.org/nightly/core/ops/function/trait.FnMut.html" title="trait core::ops::function::FnMut">FnMut</a>(NewRequest) -&gt; Request</span>,
{ ... }
<span class="item-spacer"></span> fn <a href="#method.then" class="fnname">then</a>&lt;F, Response, Error, Fut&gt;(self, f: F) -&gt; <a class="struct" href="util/struct.Then.html" title="struct tower::util::Then">Then</a>&lt;Self, F&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="where">where<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Self: <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Error: <a class="trait" href="https://doc.rust-lang.org/nightly/core/convert/trait.From.html" title="trait core::convert::From">From</a>&lt;Self::<a class="associatedtype" href="trait.Service.html#associatedtype.Error" title="type tower::Service::Error">Error</a>&gt;,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;F: <a class="trait" href="https://doc.rust-lang.org/nightly/core/ops/function/trait.FnOnce.html" title="trait core::ops::function::FnOnce">FnOnce</a>(<a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;Self::<a class="associatedtype" href="trait.Service.html#associatedtype.Response" title="type tower::Service::Response">Response</a>, Self::<a class="associatedtype" href="trait.Service.html#associatedtype.Error" title="type tower::Service::Error">Error</a>&gt;) -&gt; Fut + <a class="trait" href="https://doc.rust-lang.org/nightly/core/clone/trait.Clone.html" title="trait core::clone::Clone">Clone</a>,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Fut: <a class="trait" href="https://doc.rust-lang.org/nightly/core/future/future/trait.Future.html" title="trait core::future::future::Future">Future</a>&lt;Output = <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;Response, Error&gt;&gt;</span>,
{ ... }
<span class="item-spacer"></span> fn <a href="#method.map_future" class="fnname">map_future</a>&lt;F, Fut, Response, Error&gt;(self, f: F) -&gt; <a class="struct" href="util/struct.MapFuture.html" title="struct tower::util::MapFuture">MapFuture</a>&lt;Self, F&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="where">where<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Self: <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;F: <a class="trait" href="https://doc.rust-lang.org/nightly/core/ops/function/trait.FnMut.html" title="trait core::ops::function::FnMut">FnMut</a>(Self::<a class="associatedtype" href="trait.Service.html#associatedtype.Future" title="type tower::Service::Future">Future</a>) -&gt; Fut,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Error: <a class="trait" href="https://doc.rust-lang.org/nightly/core/convert/trait.From.html" title="trait core::convert::From">From</a>&lt;Self::<a class="associatedtype" href="trait.Service.html#associatedtype.Error" title="type tower::Service::Error">Error</a>&gt;,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Fut: <a class="trait" href="https://doc.rust-lang.org/nightly/core/future/future/trait.Future.html" title="trait core::future::future::Future">Future</a>&lt;Output = <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;Response, Error&gt;&gt;</span>,
{ ... }
<span class="item-spacer"></span> fn <a href="#method.boxed" class="fnname">boxed</a>(self) -&gt; <a class="struct" href="util/struct.BoxService.html" title="struct tower::util::BoxService">BoxService</a>&lt;Request, Self::<a class="associatedtype" href="trait.Service.html#associatedtype.Response" title="type tower::Service::Response">Response</a>, Self::<a class="associatedtype" href="trait.Service.html#associatedtype.Error" title="type tower::Service::Error">Error</a>&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="where">where<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Self: <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a> + <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Send.html" title="trait core::marker::Send">Send</a> + 'static,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Self::<a class="associatedtype" href="trait.Service.html#associatedtype.Future" title="type tower::Service::Future">Future</a>: <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Send.html" title="trait core::marker::Send">Send</a> + 'static</span>,
{ ... }
<span class="item-spacer"></span> fn <a href="#method.boxed_clone" class="fnname">boxed_clone</a>(<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self<br>&nbsp;&nbsp;&nbsp;&nbsp;) -&gt; <a class="struct" href="util/struct.BoxCloneService.html" title="struct tower::util::BoxCloneService">BoxCloneService</a>&lt;Request, Self::<a class="associatedtype" href="trait.Service.html#associatedtype.Response" title="type tower::Service::Response">Response</a>, Self::<a class="associatedtype" href="trait.Service.html#associatedtype.Error" title="type tower::Service::Error">Error</a>&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="where">where<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Self: <a class="trait" href="https://doc.rust-lang.org/nightly/core/clone/trait.Clone.html" title="trait core::clone::Clone">Clone</a> + <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a> + <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Send.html" title="trait core::marker::Send">Send</a> + 'static,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Self::<a class="associatedtype" href="trait.Service.html#associatedtype.Future" title="type tower::Service::Future">Future</a>: <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Send.html" title="trait core::marker::Send">Send</a> + 'static</span>,
{ ... }
</details>}</code></pre></div><details class="rustdoc-toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>An extension trait for <code>Service</code>s that provides a variety of convenient
adapters</p>
</div></details><h2 id="provided-methods" class="small-section-header">Provided Methods<a href="#provided-methods" class="anchor"></a></h2><div class="methods"><details class="rustdoc-toggle method-toggle" open><summary><section id="method.ready" class="method has-srclink"><a class="srclink rightside" href="../src/tower/util/mod.rs.html#67-72">source</a><h4 class="code-header">fn <a href="#method.ready" class="fnname">ready</a>(&amp;mut self) -&gt; <a class="struct" href="util/struct.Ready.html" title="struct tower::util::Ready">Ready</a>&lt;'_, Self, Request&gt;<span class="notable-traits"><span class="notable-traits-tooltip"><span class="notable-traits-tooltiptext"><span class="docblock"><span class="notable">Notable traits for <a class="struct" href="util/struct.Ready.html" title="struct tower::util::Ready">Ready</a>&lt;'a, T, Request&gt;</span><code class="content"><span class="where fmt-newline">impl&lt;'a, T, Request&gt; <a class="trait" href="https://doc.rust-lang.org/nightly/core/future/future/trait.Future.html" title="trait core::future::future::Future">Future</a> for <a class="struct" href="util/struct.Ready.html" title="struct tower::util::Ready">Ready</a>&lt;'a, T, Request&gt;<span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;T: <a class="trait" href="trait.Service.html" title="trait tower::Service">Service</a>&lt;Request&gt;,</span></span><span class="where fmt-newline"> type <a href="https://doc.rust-lang.org/nightly/core/future/future/trait.Future.html#associatedtype.Output" class="associatedtype">Output</a> = <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;<a class="primitive" href="https://doc.rust-lang.org/nightly/std/primitive.reference.html">&amp;'a mut </a>T, T::<a class="associatedtype" href="trait.Service.html#associatedtype.Error" title="type tower::Service::Error">Error</a>&gt;;</span></code></span></span></span></span><span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;Self: <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,</span></h4></section></summary><div class="docblock"><p>Yields a mutable reference to the service when it is ready to accept a request.</p>
</div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.ready_and" class="method has-srclink"><a class="srclink rightside" href="../src/tower/util/mod.rs.html#80-85">source</a><h4 class="code-header">fn <a href="#method.ready_and" class="fnname">ready_and</a>(&amp;mut self) -&gt; <a class="type" href="util/type.ReadyAnd.html" title="type tower::util::ReadyAnd">ReadyAnd</a>&lt;'_, Self, Request&gt;<span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;Self: <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,</span></h4></section></summary><span class="item-info"><div class="stab deprecated"><span class="emoji">👎</span><span>Deprecated since 0.4.6: please use the <code>ServiceExt::ready</code> method instead</span></div></span><div class="docblock"><p>Yields a mutable reference to the service when it is ready to accept a request.</p>
</div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.ready_oneshot" class="method has-srclink"><a class="srclink rightside" href="../src/tower/util/mod.rs.html#88-93">source</a><h4 class="code-header">fn <a href="#method.ready_oneshot" class="fnname">ready_oneshot</a>(self) -&gt; <a class="struct" href="util/struct.ReadyOneshot.html" title="struct tower::util::ReadyOneshot">ReadyOneshot</a>&lt;Self, Request&gt;<span class="notable-traits"><span class="notable-traits-tooltip"><span class="notable-traits-tooltiptext"><span class="docblock"><span class="notable">Notable traits for <a class="struct" href="util/struct.ReadyOneshot.html" title="struct tower::util::ReadyOneshot">ReadyOneshot</a>&lt;T, Request&gt;</span><code class="content"><span class="where fmt-newline">impl&lt;T, Request&gt; <a class="trait" href="https://doc.rust-lang.org/nightly/core/future/future/trait.Future.html" title="trait core::future::future::Future">Future</a> for <a class="struct" href="util/struct.ReadyOneshot.html" title="struct tower::util::ReadyOneshot">ReadyOneshot</a>&lt;T, Request&gt;<span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;T: <a class="trait" href="trait.Service.html" title="trait tower::Service">Service</a>&lt;Request&gt;,</span></span><span class="where fmt-newline"> type <a href="https://doc.rust-lang.org/nightly/core/future/future/trait.Future.html#associatedtype.Output" class="associatedtype">Output</a> = <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;T, T::<a class="associatedtype" href="trait.Service.html#associatedtype.Error" title="type tower::Service::Error">Error</a>&gt;;</span></code></span></span></span></span><span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;Self: <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,</span></h4></section></summary><div class="docblock"><p>Yields the service when it is ready to accept a request.</p>
</div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.oneshot" class="method has-srclink"><a class="srclink rightside" href="../src/tower/util/mod.rs.html#96-101">source</a><h4 class="code-header">fn <a href="#method.oneshot" class="fnname">oneshot</a>(self, req: Request) -&gt; <a class="struct" href="util/struct.Oneshot.html" title="struct tower::util::Oneshot">Oneshot</a>&lt;Self, Request&gt;<span class="notable-traits"><span class="notable-traits-tooltip"><span class="notable-traits-tooltiptext"><span class="docblock"><span class="notable">Notable traits for <a class="struct" href="util/struct.Oneshot.html" title="struct tower::util::Oneshot">Oneshot</a>&lt;S, Req&gt;</span><code class="content"><span class="where fmt-newline">impl&lt;S, Req&gt; <a class="trait" href="https://doc.rust-lang.org/nightly/core/future/future/trait.Future.html" title="trait core::future::future::Future">Future</a> for <a class="struct" href="util/struct.Oneshot.html" title="struct tower::util::Oneshot">Oneshot</a>&lt;S, Req&gt;<span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;S: <a class="trait" href="trait.Service.html" title="trait tower::Service">Service</a>&lt;Req&gt;,</span></span><span class="where fmt-newline"> type <a href="https://doc.rust-lang.org/nightly/core/future/future/trait.Future.html#associatedtype.Output" class="associatedtype">Output</a> = <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;S::<a class="associatedtype" href="trait.Service.html#associatedtype.Response" title="type tower::Service::Response">Response</a>, S::<a class="associatedtype" href="trait.Service.html#associatedtype.Error" title="type tower::Service::Error">Error</a>&gt;;</span></code></span></span></span></span><span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;Self: <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,</span></h4></section></summary><div class="docblock"><p>Consume this <code>Service</code>, calling with the providing request once it is ready.</p>
</div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.call_all" class="method has-srclink"><a class="srclink rightside" href="../src/tower/util/mod.rs.html#111-118">source</a><h4 class="code-header">fn <a href="#method.call_all" class="fnname">call_all</a>&lt;S&gt;(self, reqs: S) -&gt; <a class="struct" href="util/struct.CallAll.html" title="struct tower::util::CallAll">CallAll</a>&lt;Self, S&gt;<span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;Self: <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,<br>&nbsp;&nbsp;&nbsp;&nbsp;Self::<a class="associatedtype" href="trait.Service.html#associatedtype.Error" title="type tower::Service::Error">Error</a>: <a class="trait" href="https://doc.rust-lang.org/nightly/core/convert/trait.Into.html" title="trait core::convert::Into">Into</a>&lt;<a class="type" href="type.BoxError.html" title="type tower::BoxError">BoxError</a>&gt;,<br>&nbsp;&nbsp;&nbsp;&nbsp;S: <a class="trait" href="../futures_core/stream/trait.Stream.html" title="trait futures_core::stream::Stream">Stream</a>&lt;Item = Request&gt;,</span></h4></section></summary><div class="docblock"><p>Process all requests from the given <a href="https://docs.rs/futures/latest/futures/stream/trait.Stream.html"><code>Stream</code></a>, and produce a <a href="https://docs.rs/futures/latest/futures/stream/trait.Stream.html"><code>Stream</code></a> of their responses.</p>
<p>This is essentially <a href="https://docs.rs/futures/latest/futures/stream/trait.Stream.html"><code>Stream&lt;Item = Request&gt;</code></a> + <code>Self</code> =&gt; <a href="https://docs.rs/futures/latest/futures/stream/trait.Stream.html"><code>Stream&lt;Item = Response&gt;</code></a>. See the documentation for <a href="util/struct.CallAll.html" title="CallAll"><code>CallAll</code></a> for
details.</p>
</div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.and_then" class="method has-srclink"><a class="srclink rightside" href="../src/tower/util/mod.rs.html#180-186">source</a><h4 class="code-header">fn <a href="#method.and_then" class="fnname">and_then</a>&lt;F&gt;(self, f: F) -&gt; <a class="struct" href="util/struct.AndThen.html" title="struct tower::util::AndThen">AndThen</a>&lt;Self, F&gt;<span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;Self: <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,<br>&nbsp;&nbsp;&nbsp;&nbsp;F: <a class="trait" href="https://doc.rust-lang.org/nightly/core/clone/trait.Clone.html" title="trait core::clone::Clone">Clone</a>,</span></h4></section></summary><div class="docblock"><p>Executes a new future after this service’s future resolves. This does
not alter the behaviour of the <a href="trait.Service.html#tymethod.poll_ready"><code>poll_ready</code></a> method.</p>
<p>This method can be used to change the <a href="trait.Service.html#associatedtype.Response"><code>Response</code></a> type of the service
into a different type. You can use this method to chain along a computation once the
service’s response has been resolved.</p>
<h5 id="example"><a href="#example">Example</a></h5>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="comment">// A service returning Result&lt;Record, _&gt;
</span><span class="kw">let </span>service = DatabaseService::new(<span class="string">&quot;127.0.0.1:8080&quot;</span>);
<span class="comment">// Map the response into a new response
</span><span class="kw">let </span><span class="kw-2">mut </span>new_service = service.and_then(|record: Record| <span class="kw">async move </span>{
<span class="kw">let </span>name = record.name;
avatar_lookup(name).<span class="kw">await
</span>});
<span class="comment">// Call the new service
</span><span class="kw">let </span>id = <span class="number">13</span>;
<span class="kw">let </span>avatar = new_service.call(id).<span class="kw">await</span>.unwrap();</code></pre></div>
</div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.map_response" class="method has-srclink"><a class="srclink rightside" href="../src/tower/util/mod.rs.html#249-255">source</a><h4 class="code-header">fn <a href="#method.map_response" class="fnname">map_response</a>&lt;F, Response&gt;(self, f: F) -&gt; <a class="struct" href="util/struct.MapResponse.html" title="struct tower::util::MapResponse">MapResponse</a>&lt;Self, F&gt;<span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;Self: <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,<br>&nbsp;&nbsp;&nbsp;&nbsp;F: <a class="trait" href="https://doc.rust-lang.org/nightly/core/ops/function/trait.FnOnce.html" title="trait core::ops::function::FnOnce">FnOnce</a>(Self::<a class="associatedtype" href="trait.Service.html#associatedtype.Response" title="type tower::Service::Response">Response</a>) -&gt; Response + <a class="trait" href="https://doc.rust-lang.org/nightly/core/clone/trait.Clone.html" title="trait core::clone::Clone">Clone</a>,</span></h4></section></summary><div class="docblock"><p>Maps this service’s response value to a different value. This does not
alter the behaviour of the <a href="trait.Service.html#tymethod.poll_ready"><code>poll_ready</code></a> method.</p>
<p>This method can be used to change the <a href="trait.Service.html#associatedtype.Response"><code>Response</code></a> type of the service
into a different type. It is similar to the <a href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html#method.map" title="Result::map"><code>Result::map</code></a>
method. You can use this method to chain along a computation once the
service’s response has been resolved.</p>
<h5 id="example-1"><a href="#example-1">Example</a></h5>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="comment">// A service returning Result&lt;Record, _&gt;
</span><span class="kw">let </span>service = DatabaseService::new(<span class="string">&quot;127.0.0.1:8080&quot;</span>);
<span class="comment">// Map the response into a new response
</span><span class="kw">let </span><span class="kw-2">mut </span>new_service = service.map_response(|record| record.name);
<span class="comment">// Call the new service
</span><span class="kw">let </span>id = <span class="number">13</span>;
<span class="kw">let </span>name = new_service
.ready()
.<span class="kw">await</span><span class="question-mark">?
</span>.call(id)
.<span class="kw">await</span><span class="question-mark">?</span>;</code></pre></div>
</div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.map_err" class="method has-srclink"><a class="srclink rightside" href="../src/tower/util/mod.rs.html#317-323">source</a><h4 class="code-header">fn <a href="#method.map_err" class="fnname">map_err</a>&lt;F, Error&gt;(self, f: F) -&gt; <a class="struct" href="util/struct.MapErr.html" title="struct tower::util::MapErr">MapErr</a>&lt;Self, F&gt;<span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;Self: <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,<br>&nbsp;&nbsp;&nbsp;&nbsp;F: <a class="trait" href="https://doc.rust-lang.org/nightly/core/ops/function/trait.FnOnce.html" title="trait core::ops::function::FnOnce">FnOnce</a>(Self::<a class="associatedtype" href="trait.Service.html#associatedtype.Error" title="type tower::Service::Error">Error</a>) -&gt; Error + <a class="trait" href="https://doc.rust-lang.org/nightly/core/clone/trait.Clone.html" title="trait core::clone::Clone">Clone</a>,</span></h4></section></summary><div class="docblock"><p>Maps this service’s error value to a different value. This does not
alter the behaviour of the <a href="trait.Service.html#tymethod.poll_ready"><code>poll_ready</code></a> method.</p>
<p>This method can be used to change the <a href="trait.Service.html#associatedtype.Error"><code>Error</code></a> type of the service
into a different type. It is similar to the <a href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html#method.map_err" title="Result::map_err"><code>Result::map_err</code></a> method.</p>
<h5 id="example-2"><a href="#example-2">Example</a></h5>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="comment">// A service returning Result&lt;_, Error&gt;
</span><span class="kw">let </span>service = DatabaseService::new(<span class="string">&quot;127.0.0.1:8080&quot;</span>);
<span class="comment">// Map the error to a new error
</span><span class="kw">let </span><span class="kw-2">mut </span>new_service = service.map_err(|err| err.code);
<span class="comment">// Call the new service
</span><span class="kw">let </span>id = <span class="number">13</span>;
<span class="kw">let </span>code = new_service
.ready()
.<span class="kw">await</span><span class="question-mark">?
</span>.call(id)
.<span class="kw">await
</span>.unwrap_err();</code></pre></div>
</div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.map_result" class="method has-srclink"><a class="srclink rightside" href="../src/tower/util/mod.rs.html#546-553">source</a><h4 class="code-header">fn <a href="#method.map_result" class="fnname">map_result</a>&lt;F, Response, Error&gt;(self, f: F) -&gt; <a class="struct" href="util/struct.MapResult.html" title="struct tower::util::MapResult">MapResult</a>&lt;Self, F&gt;<span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;Self: <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,<br>&nbsp;&nbsp;&nbsp;&nbsp;Error: <a class="trait" href="https://doc.rust-lang.org/nightly/core/convert/trait.From.html" title="trait core::convert::From">From</a>&lt;Self::<a class="associatedtype" href="trait.Service.html#associatedtype.Error" title="type tower::Service::Error">Error</a>&gt;,<br>&nbsp;&nbsp;&nbsp;&nbsp;F: <a class="trait" href="https://doc.rust-lang.org/nightly/core/ops/function/trait.FnOnce.html" title="trait core::ops::function::FnOnce">FnOnce</a>(<a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;Self::<a class="associatedtype" href="trait.Service.html#associatedtype.Response" title="type tower::Service::Response">Response</a>, Self::<a class="associatedtype" href="trait.Service.html#associatedtype.Error" title="type tower::Service::Error">Error</a>&gt;) -&gt; <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;Response, Error&gt; + <a class="trait" href="https://doc.rust-lang.org/nightly/core/clone/trait.Clone.html" title="trait core::clone::Clone">Clone</a>,</span></h4></section></summary><div class="docblock"><p>Maps this service’s result type (<code>Result&lt;Self::Response, Self::Error&gt;</code>)
to a different value, regardless of whether the future succeeds or
fails.</p>
<p>This is similar to the <a href="trait.ServiceExt.html#method.map_response"><code>map_response</code></a> and <a href="trait.ServiceExt.html#method.map_err"><code>map_err</code></a> combinators,
except that the <em>same</em> function is invoked when the service’s future
completes, whether it completes successfully or fails. This function
takes the <a href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="Result"><code>Result</code></a> returned by the service’s future, and returns a
<a href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="Result"><code>Result</code></a>.</p>
<p>Like the standard library’s <a href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html#method.and_then" title="Result::and_then"><code>Result::and_then</code></a>, this method can be
used to implement control flow based on <code>Result</code> values. For example, it
may be used to implement error recovery, by turning some <a href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html#variant.Err" title="Err"><code>Err</code></a>
responses from the service into <a href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html#variant.Ok" title="Ok"><code>Ok</code></a> responses. Similarly, some
successful responses from the service could be rejected, by returning an
<a href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html#variant.Err" title="Err"><code>Err</code></a> conditionally, depending on the value inside the [<code>Ok</code>.] Finally,
this method can also be used to implement behaviors that must run when a
service’s future completes, regardless of whether it succeeded or failed.</p>
<p>This method can be used to change the <a href="trait.Service.html#associatedtype.Response"><code>Response</code></a> type of the service
into a different type. It can also be used to change the <a href="trait.Service.html#associatedtype.Error"><code>Error</code></a> type
of the service. However, because the <a href="trait.ServiceExt.html#method.map_result"><code>map_result</code></a> function is not applied
to the errors returned by the service’s <a href="trait.Service.html#tymethod.poll_ready"><code>poll_ready</code></a> method, it must
be possible to convert the service’s <a href="trait.Service.html#associatedtype.Error"><code>Error</code></a> type into the error type
returned by the <a href="trait.ServiceExt.html#method.map_result"><code>map_result</code></a> function. This is trivial when the function
returns the same error type as the service, but in other cases, it can
be useful to use <a href="type.BoxError.html"><code>BoxError</code></a> to erase differing error types.</p>
<h5 id="examples"><a href="#examples">Examples</a></h5>
<p>Recovering from certain errors:</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="comment">// A service returning Result&lt;Vec&lt;Record&gt;, DbError&gt;
</span><span class="kw">let </span>service = DatabaseService::new(<span class="string">&quot;127.0.0.1:8080&quot;</span>);
<span class="comment">// If the database returns no records for the query, we just want an empty `Vec`.
</span><span class="kw">let </span><span class="kw-2">mut </span>new_service = service.map_result(|result| <span class="kw">match </span>result {
<span class="comment">// If the error indicates that no records matched the query, return an empty
// `Vec` instead.
</span><span class="prelude-val">Err</span>(DbError::NoRecordsFound) =&gt; <span class="prelude-val">Ok</span>(Vec::new()),
<span class="comment">// Propagate all other responses (`Ok` and `Err`) unchanged
</span>x =&gt; x,
});
<span class="comment">// Call the new service
</span><span class="kw">let </span>id = <span class="number">13</span>;
<span class="kw">let </span>name = new_service
.ready()
.<span class="kw">await</span><span class="question-mark">?
</span>.call(id)
.<span class="kw">await</span><span class="question-mark">?</span>;</code></pre></div>
<p>Rejecting some <code>Ok</code> responses:</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tower::BoxError;
<span class="comment">// A service returning Result&lt;Record, DbError&gt;
</span><span class="kw">let </span>service = DatabaseService::new(<span class="string">&quot;127.0.0.1:8080&quot;</span>);
<span class="comment">// If the user is zero years old, return an error.
</span><span class="kw">let </span><span class="kw-2">mut </span>new_service = service.map_result(|result| {
<span class="kw">let </span>record = result<span class="question-mark">?</span>;
<span class="kw">if </span>record.age == <span class="number">0 </span>{
<span class="comment">// Users must have been born to use our app!
</span><span class="kw">let </span>app_error = AppError::from(<span class="string">&quot;users cannot be 0 years old!&quot;</span>);
<span class="comment">// Box the error to erase its type (as it can be an `AppError`
// *or* the inner service&#39;s `DbError`).
</span><span class="kw">return </span><span class="prelude-val">Err</span>(BoxError::from(app_error));
}
<span class="comment">// Otherwise, return the record.
</span><span class="prelude-val">Ok</span>(record)
});
<span class="comment">// Call the new service
</span><span class="kw">let </span>id = <span class="number">13</span>;
<span class="kw">let </span>record = new_service
.ready()
.<span class="kw">await</span><span class="question-mark">?
</span>.call(id)
.<span class="kw">await</span><span class="question-mark">?</span>;</code></pre></div>
<p>Performing an action that must be run for both successes and failures:</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="comment">// A service returning Result&lt;Record, DbError&gt;
</span><span class="kw">let </span>service = DatabaseService::new(<span class="string">&quot;127.0.0.1:8080&quot;</span>);
<span class="comment">// Print a message whenever a query completes.
</span><span class="kw">let </span><span class="kw-2">mut </span>new_service = service.map_result(|result| {
<span class="macro">println!</span>(<span class="string">&quot;query completed; success={}&quot;</span>, result.is_ok());
result
});
<span class="comment">// Call the new service
</span><span class="kw">let </span>id = <span class="number">13</span>;
<span class="kw">let </span>response = new_service
.ready()
.<span class="kw">await</span><span class="question-mark">?
</span>.call(id)
.<span class="kw">await</span>;</code></pre></div>
</div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.map_request" class="method has-srclink"><a class="srclink rightside" href="../src/tower/util/mod.rs.html#606-612">source</a><h4 class="code-header">fn <a href="#method.map_request" class="fnname">map_request</a>&lt;F, NewRequest&gt;(self, f: F) -&gt; <a class="struct" href="util/struct.MapRequest.html" title="struct tower::util::MapRequest">MapRequest</a>&lt;Self, F&gt;<span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;Self: <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,<br>&nbsp;&nbsp;&nbsp;&nbsp;F: <a class="trait" href="https://doc.rust-lang.org/nightly/core/ops/function/trait.FnMut.html" title="trait core::ops::function::FnMut">FnMut</a>(NewRequest) -&gt; Request,</span></h4></section></summary><div class="docblock"><p>Composes a function <em>in front of</em> the service.</p>
<p>This adapter produces a new service that passes each value through the
given function <code>f</code> before sending it to <code>self</code>.</p>
<h5 id="example-3"><a href="#example-3">Example</a></h5>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="comment">// A service taking a String as a request
</span><span class="kw">let </span>service = DatabaseService::new(<span class="string">&quot;127.0.0.1:8080&quot;</span>);
<span class="comment">// Map the request to a new request
</span><span class="kw">let </span><span class="kw-2">mut </span>new_service = service.map_request(|id: u32| id.to_string());
<span class="comment">// Call the new service
</span><span class="kw">let </span>id = <span class="number">13</span>;
<span class="kw">let </span>response = new_service
.ready()
.<span class="kw">await</span><span class="question-mark">?
</span>.call(id)
.<span class="kw">await</span>;</code></pre></div>
</div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.then" class="method has-srclink"><a class="srclink rightside" href="../src/tower/util/mod.rs.html#873-881">source</a><h4 class="code-header">fn <a href="#method.then" class="fnname">then</a>&lt;F, Response, Error, Fut&gt;(self, f: F) -&gt; <a class="struct" href="util/struct.Then.html" title="struct tower::util::Then">Then</a>&lt;Self, F&gt;<span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;Self: <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,<br>&nbsp;&nbsp;&nbsp;&nbsp;Error: <a class="trait" href="https://doc.rust-lang.org/nightly/core/convert/trait.From.html" title="trait core::convert::From">From</a>&lt;Self::<a class="associatedtype" href="trait.Service.html#associatedtype.Error" title="type tower::Service::Error">Error</a>&gt;,<br>&nbsp;&nbsp;&nbsp;&nbsp;F: <a class="trait" href="https://doc.rust-lang.org/nightly/core/ops/function/trait.FnOnce.html" title="trait core::ops::function::FnOnce">FnOnce</a>(<a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;Self::<a class="associatedtype" href="trait.Service.html#associatedtype.Response" title="type tower::Service::Response">Response</a>, Self::<a class="associatedtype" href="trait.Service.html#associatedtype.Error" title="type tower::Service::Error">Error</a>&gt;) -&gt; Fut + <a class="trait" href="https://doc.rust-lang.org/nightly/core/clone/trait.Clone.html" title="trait core::clone::Clone">Clone</a>,<br>&nbsp;&nbsp;&nbsp;&nbsp;Fut: <a class="trait" href="https://doc.rust-lang.org/nightly/core/future/future/trait.Future.html" title="trait core::future::future::Future">Future</a>&lt;Output = <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;Response, Error&gt;&gt;,</span></h4></section></summary><div class="docblock"><p>Composes an asynchronous function <em>after</em> this service.</p>
<p>This takes a function or closure returning a future, and returns a new
<code>Service</code> that chains that function after this service’s <a href="trait.Service.html#associatedtype.Future"><code>Future</code></a>. The
new <code>Service</code>’s future will consist of this service’s future, followed
by the future returned by calling the chained function with the future’s
<a href="https://doc.rust-lang.org/nightly/core/future/future/trait.Future.html#associatedtype.Output"><code>Output</code></a> type. The chained function is called regardless of whether
this service’s future completes with a successful response or with an
error.</p>
<p>This method can be thought of as an equivalent to the <a href="https://docs.rs/futures"><code>futures</code>
crate</a>’s <a href="https://docs.rs/futures/latest/futures/future/trait.FutureExt.html#method.then"><code>FutureExt::then</code></a> combinator, but acting on <code>Service</code>s that
<em>return</em> futures, rather than on an individual future. Similarly to that
combinator, <a href="trait.ServiceExt.html#method.then" title="ServiceExt::then"><code>ServiceExt::then</code></a> can be used to implement asynchronous
error recovery, by calling some asynchronous function with errors
returned by this service. Alternatively, it may also be used to call a
fallible async function with the successful response of this service.</p>
<p>This method can be used to change the <a href="trait.Service.html#associatedtype.Response"><code>Response</code></a> type of the service
into a different type. It can also be used to change the <a href="trait.Service.html#associatedtype.Error"><code>Error</code></a> type
of the service. However, because the <code>then</code> function is not applied
to the errors returned by the service’s <a href="trait.Service.html#tymethod.poll_ready"><code>poll_ready</code></a> method, it must
be possible to convert the service’s <a href="trait.Service.html#associatedtype.Error"><code>Error</code></a> type into the error type
returned by the <code>then</code> future. This is trivial when the function
returns the same error type as the service, but in other cases, it can
be useful to use <a href="type.BoxError.html"><code>BoxError</code></a> to erase differing error types.</p>
<h5 id="examples-1"><a href="#examples-1">Examples</a></h5>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="comment">// A service returning Result&lt;Record, DbError&gt;
</span><span class="kw">let </span>service = DatabaseService::new(<span class="string">&quot;127.0.0.1:8080&quot;</span>);
<span class="comment">// An async function that attempts to recover from errors returned by the
// database.
</span><span class="kw">async fn </span>recover_from_error(error: DbError) -&gt; <span class="prelude-ty">Result</span>&lt;Record, DbError&gt; {
<span class="comment">// ...
</span>}
<span class="comment">// If the database service returns an error, attempt to recover by
// calling `recover_from_error`. Otherwise, return the successful response.
</span><span class="kw">let </span><span class="kw-2">mut </span>new_service = service.then(|result| <span class="kw">async move </span>{
<span class="kw">match </span>result {
<span class="prelude-val">Ok</span>(record) =&gt; <span class="prelude-val">Ok</span>(record),
<span class="prelude-val">Err</span>(e) =&gt; recover_from_error(e).<span class="kw">await</span>,
}
});
<span class="comment">// Call the new service
</span><span class="kw">let </span>id = <span class="number">13</span>;
<span class="kw">let </span>record = new_service
.ready()
.<span class="kw">await</span><span class="question-mark">?
</span>.call(id)
.<span class="kw">await</span><span class="question-mark">?</span>;</code></pre></div>
</div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.map_future" class="method has-srclink"><a class="srclink rightside" href="../src/tower/util/mod.rs.html#947-955">source</a><h4 class="code-header">fn <a href="#method.map_future" class="fnname">map_future</a>&lt;F, Fut, Response, Error&gt;(self, f: F) -&gt; <a class="struct" href="util/struct.MapFuture.html" title="struct tower::util::MapFuture">MapFuture</a>&lt;Self, F&gt;<span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;Self: <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>,<br>&nbsp;&nbsp;&nbsp;&nbsp;F: <a class="trait" href="https://doc.rust-lang.org/nightly/core/ops/function/trait.FnMut.html" title="trait core::ops::function::FnMut">FnMut</a>(Self::<a class="associatedtype" href="trait.Service.html#associatedtype.Future" title="type tower::Service::Future">Future</a>) -&gt; Fut,<br>&nbsp;&nbsp;&nbsp;&nbsp;Error: <a class="trait" href="https://doc.rust-lang.org/nightly/core/convert/trait.From.html" title="trait core::convert::From">From</a>&lt;Self::<a class="associatedtype" href="trait.Service.html#associatedtype.Error" title="type tower::Service::Error">Error</a>&gt;,<br>&nbsp;&nbsp;&nbsp;&nbsp;Fut: <a class="trait" href="https://doc.rust-lang.org/nightly/core/future/future/trait.Future.html" title="trait core::future::future::Future">Future</a>&lt;Output = <a class="enum" href="https://doc.rust-lang.org/nightly/core/result/enum.Result.html" title="enum core::result::Result">Result</a>&lt;Response, Error&gt;&gt;,</span></h4></section></summary><div class="docblock"><p>Composes a function that transforms futures produced by the service.</p>
<p>This takes a function or closure returning a future computed from the future returned by
the service’s <a href="trait.Service.html#tymethod.call"><code>call</code></a> method, as opposed to the responses produced by the future.</p>
<h5 id="examples-2"><a href="#examples-2">Examples</a></h5>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>std::time::Duration;
<span class="kw">use </span>tokio::time::timeout;
<span class="comment">// A service returning Result&lt;Record, DbError&gt;
</span><span class="kw">let </span>service = DatabaseService::new(<span class="string">&quot;127.0.0.1:8080&quot;</span>);
<span class="kw">let </span><span class="kw-2">mut </span>new_service = service.map_future(|future| <span class="kw">async move </span>{
<span class="kw">let </span>res = timeout(Duration::from_secs(<span class="number">1</span>), future).<span class="kw">await</span><span class="question-mark">?</span>;
<span class="prelude-val">Ok</span>::&lt;<span class="kw">_</span>, BoxError&gt;(res)
});
<span class="comment">// Call the new service
</span><span class="kw">let </span>id = <span class="number">13</span>;
<span class="kw">let </span>record = new_service
.ready()
.<span class="kw">await</span><span class="question-mark">?
</span>.call(id)
.<span class="kw">await</span><span class="question-mark">?</span>;</code></pre></div>
<p>Note that normally you wouldn’t implement timeouts like this and instead use <a href="timeout/struct.Timeout.html"><code>Timeout</code></a>.</p>
</div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.boxed" class="method has-srclink"><a class="srclink rightside" href="../src/tower/util/mod.rs.html#997-1003">source</a><h4 class="code-header">fn <a href="#method.boxed" class="fnname">boxed</a>(self) -&gt; <a class="struct" href="util/struct.BoxService.html" title="struct tower::util::BoxService">BoxService</a>&lt;Request, Self::<a class="associatedtype" href="trait.Service.html#associatedtype.Response" title="type tower::Service::Response">Response</a>, Self::<a class="associatedtype" href="trait.Service.html#associatedtype.Error" title="type tower::Service::Error">Error</a>&gt;<span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;Self: <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a> + <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Send.html" title="trait core::marker::Send">Send</a> + 'static,<br>&nbsp;&nbsp;&nbsp;&nbsp;Self::<a class="associatedtype" href="trait.Service.html#associatedtype.Future" title="type tower::Service::Future">Future</a>: <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Send.html" title="trait core::marker::Send">Send</a> + 'static,</span></h4></section></summary><div class="docblock"><p>Convert the service into a <a href="trait.Service.html"><code>Service</code></a> + <a href="https://doc.rust-lang.org/nightly/core/marker/trait.Send.html" title="Send"><code>Send</code></a> trait object.</p>
<p>See <a href="util/struct.BoxService.html" title="BoxService"><code>BoxService</code></a> for more details.</p>
<p>If <code>Self</code> implements the <a href="https://doc.rust-lang.org/nightly/core/clone/trait.Clone.html" title="Clone"><code>Clone</code></a> trait, the <a href="trait.ServiceExt.html#method.boxed_clone"><code>boxed_clone</code></a> method
can be used instead, to produce a boxed service which will also
implement <a href="https://doc.rust-lang.org/nightly/core/clone/trait.Clone.html" title="Clone"><code>Clone</code></a>.</p>
<h5 id="example-4"><a href="#example-4">Example</a></h5>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tower::{Service, ServiceExt, BoxError, service_fn, util::BoxService};
<span class="kw">let </span>service = service_fn(|req: Request| <span class="kw">async </span>{
<span class="prelude-val">Ok</span>::&lt;<span class="kw">_</span>, BoxError&gt;(Response::new())
});
<span class="kw">let </span>service: BoxService&lt;Request, Response, BoxError&gt; = service
.map_request(|req| {
<span class="macro">println!</span>(<span class="string">&quot;received request&quot;</span>);
req
})
.map_response(|res| {
<span class="macro">println!</span>(<span class="string">&quot;response produced&quot;</span>);
res
})
.boxed();</code></pre></div>
</div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.boxed_clone" class="method has-srclink"><a class="srclink rightside" href="../src/tower/util/mod.rs.html#1046-1052">source</a><h4 class="code-header">fn <a href="#method.boxed_clone" class="fnname">boxed_clone</a>(self) -&gt; <a class="struct" href="util/struct.BoxCloneService.html" title="struct tower::util::BoxCloneService">BoxCloneService</a>&lt;Request, Self::<a class="associatedtype" href="trait.Service.html#associatedtype.Response" title="type tower::Service::Response">Response</a>, Self::<a class="associatedtype" href="trait.Service.html#associatedtype.Error" title="type tower::Service::Error">Error</a>&gt;<span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;Self: <a class="trait" href="https://doc.rust-lang.org/nightly/core/clone/trait.Clone.html" title="trait core::clone::Clone">Clone</a> + <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a> + <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Send.html" title="trait core::marker::Send">Send</a> + 'static,<br>&nbsp;&nbsp;&nbsp;&nbsp;Self::<a class="associatedtype" href="trait.Service.html#associatedtype.Future" title="type tower::Service::Future">Future</a>: <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Send.html" title="trait core::marker::Send">Send</a> + 'static,</span></h4></section></summary><div class="docblock"><p>Convert the service into a <a href="trait.Service.html"><code>Service</code></a> + <a href="https://doc.rust-lang.org/nightly/core/clone/trait.Clone.html" title="Clone"><code>Clone</code></a> + <a href="https://doc.rust-lang.org/nightly/core/marker/trait.Send.html" title="Send"><code>Send</code></a> trait object.</p>
<p>This is similar to the <a href="trait.ServiceExt.html#method.boxed"><code>boxed</code></a> method, but it requires that <code>Self</code> implement
<a href="https://doc.rust-lang.org/nightly/core/clone/trait.Clone.html" title="Clone"><code>Clone</code></a>, and the returned boxed service implements <a href="https://doc.rust-lang.org/nightly/core/clone/trait.Clone.html" title="Clone"><code>Clone</code></a>.
See <a href="util/struct.BoxCloneService.html" title="BoxCloneService"><code>BoxCloneService</code></a> for more details.</p>
<h5 id="example-5"><a href="#example-5">Example</a></h5>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tower::{Service, ServiceExt, BoxError, service_fn, util::BoxCloneService};
<span class="kw">let </span>service = service_fn(|req: Request| <span class="kw">async </span>{
<span class="prelude-val">Ok</span>::&lt;<span class="kw">_</span>, BoxError&gt;(Response::new())
});
<span class="kw">let </span>service: BoxCloneService&lt;Request, Response, BoxError&gt; = service
.map_request(|req| {
<span class="macro">println!</span>(<span class="string">&quot;received request&quot;</span>);
req
})
.map_response(|res| {
<span class="macro">println!</span>(<span class="string">&quot;response produced&quot;</span>);
res
})
.boxed_clone();
<span class="comment">// The boxed service can still be cloned.
</span>service.clone();</code></pre></div>
</div></details></div><h2 id="implementors" class="small-section-header">Implementors<a href="#implementors" class="anchor"></a></h2><div id="implementors-list"><section id="impl-ServiceExt%3CRequest%3E-for-T" class="impl has-srclink"><a class="srclink rightside" href="../src/tower/util/mod.rs.html#1055">source</a><a href="#impl-ServiceExt%3CRequest%3E-for-T" class="anchor"></a><h3 class="code-header">impl&lt;T:&nbsp;?<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>, Request&gt; <a class="trait" href="trait.ServiceExt.html" title="trait tower::ServiceExt">ServiceExt</a>&lt;Request&gt; for T<span class="where fmt-newline">where<br>&nbsp;&nbsp;&nbsp;&nbsp;T: <a class="trait" href="trait.Service.html" title="trait tower::Service">Service</a>&lt;Request&gt;,</span></h3></section></div><script src="../implementors/tower/util/trait.ServiceExt.js" async></script></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>