1
0
mirror of https://github.com/fafhrd91/actix-net synced 2025-01-18 18:51:49 +01:00
actix-net/actix_service/trait.Transform.html
2024-10-01 00:01:00 +00:00

68 lines
21 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="Defines the interface of a service factory that wraps inner service during construction."><title>Transform in actix_service - Rust</title><script>if(window.location.protocol!=="file:")document.head.insertAdjacentHTML("beforeend","SourceSerif4-Regular-46f98efaafac5295.ttf.woff2,FiraSans-Regular-018c141bf0843ffd.woff2,FiraSans-Medium-8f9a781e4970d388.woff2,SourceCodePro-Regular-562dcc5011b6de7d.ttf.woff2,SourceCodePro-Semibold-d899c5a5c4aeb14a.ttf.woff2".split(",").map(f=>`<link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/${f}">`).join(""))</script><link rel="stylesheet" href="../static.files/normalize-76eba96aa4d2e634.css"><link rel="stylesheet" href="../static.files/rustdoc-b778ab399e080a4b.css"><meta name="rustdoc-vars" data-root-path="../" data-static-root-path="../static.files/" data-current-crate="actix_service" data-themes="" data-resource-suffix="" data-rustdoc-version="1.83.0-nightly (7608018cb 2024-09-29)" data-channel="nightly" data-search-js="search-e056c65ede92db13.js" data-settings-js="settings-805db61a62df4bd2.js" ><script src="../static.files/storage-1d39b6787ed640ff.js"></script><script defer src="sidebar-items.js"></script><script defer src="../static.files/main-54bc299d2a5e4e43.js"></script><noscript><link rel="stylesheet" href="../static.files/noscript-0111fcff984fae8f.css"></noscript><link rel="icon" href="https://actix.rs/favicon.ico"></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" title="show sidebar"></button><a class="logo-container" href="../actix_service/index.html"><img src="https://actix.rs/img/logo.png" alt=""></a></nav><nav class="sidebar"><div class="sidebar-crate"><a class="logo-container" href="../actix_service/index.html"><img src="https://actix.rs/img/logo.png" alt="logo"></a><h2><a href="../actix_service/index.html">actix_<wbr>service</a><span class="version">2.0.2</span></h2></div><div class="sidebar-elems"><section id="rustdoc-toc"><h2 class="location"><a href="#">Transform</a></h2><h3><a href="#required-associated-types">Required Associated Types</a></h3><ul class="block"><li><a href="#associatedtype.Error" title="Error">Error</a></li><li><a href="#associatedtype.Future" title="Future">Future</a></li><li><a href="#associatedtype.InitError" title="InitError">InitError</a></li><li><a href="#associatedtype.Response" title="Response">Response</a></li><li><a href="#associatedtype.Transform" title="Transform">Transform</a></li></ul><h3><a href="#required-methods">Required Methods</a></h3><ul class="block"><li><a href="#tymethod.new_transform" title="new_transform">new_transform</a></li></ul><h3><a href="#foreign-impls">Implementations on Foreign Types</a></h3><ul class="block"><li><a href="#impl-Transform%3CS,+Req%3E-for-Arc%3CT%3E" title="Arc&#60;T&#62;">Arc&#60;T&#62;</a></li><li><a href="#impl-Transform%3CS,+Req%3E-for-Rc%3CT%3E" title="Rc&#60;T&#62;">Rc&#60;T&#62;</a></li></ul><h3><a href="#implementors">Implementors</a></h3></section><div id="rustdoc-modnav"><h2 class="in-crate"><a href="index.html">In crate actix_<wbr>service</a></h2></div></div></nav><div class="sidebar-resizer"></div><main><div class="width-limiter"><rustdoc-search></rustdoc-search><section id="main-content" class="content"><div class="main-heading"><span class="rustdoc-breadcrumbs"><a href="index.html">actix_service</a></span><h1>Trait <span class="trait">Transform</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/actix_service/transform.rs.html#83-101">source</a> </span></div><pre class="rust item-decl"><code>pub trait Transform&lt;S, Req&gt; {
type <a href="#associatedtype.Response" class="associatedtype">Response</a>;
type <a href="#associatedtype.Error" class="associatedtype">Error</a>;
type <a href="#associatedtype.Transform" class="associatedtype">Transform</a>: <a class="trait" href="trait.Service.html" title="trait actix_service::Service">Service</a>&lt;Req, Response = Self::<a class="associatedtype" href="trait.Transform.html#associatedtype.Response" title="type actix_service::Transform::Response">Response</a>, Error = Self::<a class="associatedtype" href="trait.Transform.html#associatedtype.Error" title="type actix_service::Transform::Error">Error</a>&gt;;
type <a href="#associatedtype.InitError" class="associatedtype">InitError</a>;
type <a href="#associatedtype.Future" class="associatedtype">Future</a>: <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;Self::<a class="associatedtype" href="trait.Transform.html#associatedtype.Transform" title="type actix_service::Transform::Transform">Transform</a>, Self::<a class="associatedtype" href="trait.Transform.html#associatedtype.InitError" title="type actix_service::Transform::InitError">InitError</a>&gt;&gt;;
// Required method
fn <a href="#tymethod.new_transform" class="fn">new_transform</a>(&amp;self, service: S) -&gt; Self::<a class="associatedtype" href="trait.Transform.html#associatedtype.Future" title="type actix_service::Transform::Future">Future</a>;
}</code></pre><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>Defines the interface of a service factory that wraps inner service during construction.</p>
<p>Transformers wrap an inner service and runs during inbound and/or outbound processing in the
service lifecycle. It may modify request and/or response.</p>
<p>For example, a timeout service wrapper:</p>
<div class="example-wrap ignore"><a href="#" class="tooltip" title="This example is not tested"></a><pre class="rust rust-example-rendered"><code><span class="kw">pub struct </span>Timeout&lt;S&gt; {
service: S,
timeout: Duration,
}
<span class="kw">impl</span>&lt;S: Service&lt;Req&gt;, Req&gt; Service&lt;Req&gt; <span class="kw">for </span>Timeout&lt;S&gt; {
<span class="kw">type </span>Response = S::Response;
<span class="kw">type </span>Error = TimeoutError&lt;S::Error&gt;;
<span class="kw">type </span>Future = TimeoutServiceResponse&lt;S&gt;;
<span class="macro">actix_service::forward_ready!</span>(service);
<span class="kw">fn </span>call(<span class="kw-2">&amp;</span><span class="self">self</span>, req: Req) -&gt; <span class="self">Self</span>::Future {
TimeoutServiceResponse {
fut: <span class="self">self</span>.service.call(req),
sleep: Sleep::new(clock::now() + <span class="self">self</span>.timeout),
}
}
}</code></pre></div>
<p>This wrapper service is decoupled from the underlying service implementation and could be
applied to any service.</p>
<p>The <code>Transform</code> trait defines the interface of a service wrapper. <code>Transform</code> is often
implemented for middleware, defining how to construct a middleware Service. A Service that is
constructed by the factory takes the Service that follows it during execution as a parameter,
assuming ownership of the next Service.</p>
<p>A transform for the <code>Timeout</code> middleware could look like this:</p>
<div class="example-wrap ignore"><a href="#" class="tooltip" title="This example is not tested"></a><pre class="rust rust-example-rendered"><code><span class="kw">pub struct </span>TimeoutTransform {
timeout: Duration,
}
<span class="kw">impl</span>&lt;S: Service&lt;Req&gt;, Req&gt; Transform&lt;S, Req&gt; <span class="kw">for </span>TimeoutTransform {
<span class="kw">type </span>Response = S::Response;
<span class="kw">type </span>Error = TimeoutError&lt;S::Error&gt;;
<span class="kw">type </span>InitError = S::Error;
<span class="kw">type </span>Transform = Timeout&lt;S&gt;;
<span class="kw">type </span>Future = Ready&lt;<span class="prelude-ty">Result</span>&lt;<span class="self">Self</span>::Transform, <span class="self">Self</span>::InitError&gt;&gt;;
<span class="kw">fn </span>new_transform(<span class="kw-2">&amp;</span><span class="self">self</span>, service: S) -&gt; <span class="self">Self</span>::Future {
ready(<span class="prelude-val">Ok</span>(Timeout {
service,
timeout: <span class="self">self</span>.timeout,
}))
}
}</code></pre></div>
</div></details><h2 id="required-associated-types" class="section-header">Required Associated Types<a href="#required-associated-types" class="anchor">§</a></h2><div class="methods"><details class="toggle" open><summary><section id="associatedtype.Response" class="method"><a class="src rightside" href="../src/actix_service/transform.rs.html#85">source</a><h4 class="code-header">type <a href="#associatedtype.Response" class="associatedtype">Response</a></h4></section></summary><div class="docblock"><p>Responses produced by the service.</p>
</div></details><details class="toggle" open><summary><section id="associatedtype.Error" class="method"><a class="src rightside" href="../src/actix_service/transform.rs.html#88">source</a><h4 class="code-header">type <a href="#associatedtype.Error" class="associatedtype">Error</a></h4></section></summary><div class="docblock"><p>Errors produced by the service.</p>
</div></details><details class="toggle" open><summary><section id="associatedtype.Transform" class="method"><a class="src rightside" href="../src/actix_service/transform.rs.html#91">source</a><h4 class="code-header">type <a href="#associatedtype.Transform" class="associatedtype">Transform</a>: <a class="trait" href="trait.Service.html" title="trait actix_service::Service">Service</a>&lt;Req, Response = Self::<a class="associatedtype" href="trait.Transform.html#associatedtype.Response" title="type actix_service::Transform::Response">Response</a>, Error = Self::<a class="associatedtype" href="trait.Transform.html#associatedtype.Error" title="type actix_service::Transform::Error">Error</a>&gt;</h4></section></summary><div class="docblock"><p>The <code>TransformService</code> value created by this factory</p>
</div></details><details class="toggle" open><summary><section id="associatedtype.InitError" class="method"><a class="src rightside" href="../src/actix_service/transform.rs.html#94">source</a><h4 class="code-header">type <a href="#associatedtype.InitError" class="associatedtype">InitError</a></h4></section></summary><div class="docblock"><p>Errors produced while building a transform service.</p>
</div></details><details class="toggle" open><summary><section id="associatedtype.Future" class="method"><a class="src rightside" href="../src/actix_service/transform.rs.html#97">source</a><h4 class="code-header">type <a href="#associatedtype.Future" class="associatedtype">Future</a>: <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;Self::<a class="associatedtype" href="trait.Transform.html#associatedtype.Transform" title="type actix_service::Transform::Transform">Transform</a>, Self::<a class="associatedtype" href="trait.Transform.html#associatedtype.InitError" title="type actix_service::Transform::InitError">InitError</a>&gt;&gt;</h4></section></summary><div class="docblock"><p>The future response value.</p>
</div></details></div><h2 id="required-methods" class="section-header">Required Methods<a href="#required-methods" class="anchor">§</a></h2><div class="methods"><details class="toggle method-toggle" open><summary><section id="tymethod.new_transform" class="method"><a class="src rightside" href="../src/actix_service/transform.rs.html#100">source</a><h4 class="code-header">fn <a href="#tymethod.new_transform" class="fn">new_transform</a>(&amp;self, service: S) -&gt; Self::<a class="associatedtype" href="trait.Transform.html#associatedtype.Future" title="type actix_service::Transform::Future">Future</a></h4></section></summary><div class="docblock"><p>Creates and returns a new Transform component, asynchronously</p>
</div></details></div><h2 id="foreign-impls" class="section-header">Implementations on Foreign Types<a href="#foreign-impls" class="anchor">§</a></h2><details class="toggle implementors-toggle"><summary><section id="impl-Transform%3CS,+Req%3E-for-Rc%3CT%3E" class="impl"><a class="src rightside" href="../src/actix_service/transform.rs.html#103-116">source</a><a href="#impl-Transform%3CS,+Req%3E-for-Rc%3CT%3E" class="anchor">§</a><h3 class="code-header">impl&lt;T, S, Req&gt; <a class="trait" href="trait.Transform.html" title="trait actix_service::Transform">Transform</a>&lt;S, Req&gt; for <a class="struct" href="https://doc.rust-lang.org/nightly/alloc/rc/struct.Rc.html" title="struct alloc::rc::Rc">Rc</a>&lt;T&gt;<div class="where">where
T: <a class="trait" href="trait.Transform.html" title="trait actix_service::Transform">Transform</a>&lt;S, Req&gt;,</div></h3></section></summary><div class="impl-items"><section id="associatedtype.Response-1" class="associatedtype trait-impl"><a class="src rightside" href="../src/actix_service/transform.rs.html#107">source</a><a href="#associatedtype.Response-1" class="anchor">§</a><h4 class="code-header">type <a href="#associatedtype.Response" class="associatedtype">Response</a> = &lt;T as <a class="trait" href="trait.Transform.html" title="trait actix_service::Transform">Transform</a>&lt;S, Req&gt;&gt;::<a class="associatedtype" href="trait.Transform.html#associatedtype.Response" title="type actix_service::Transform::Response">Response</a></h4></section><section id="associatedtype.Error-1" class="associatedtype trait-impl"><a class="src rightside" href="../src/actix_service/transform.rs.html#108">source</a><a href="#associatedtype.Error-1" class="anchor">§</a><h4 class="code-header">type <a href="#associatedtype.Error" class="associatedtype">Error</a> = &lt;T as <a class="trait" href="trait.Transform.html" title="trait actix_service::Transform">Transform</a>&lt;S, Req&gt;&gt;::<a class="associatedtype" href="trait.Transform.html#associatedtype.Error" title="type actix_service::Transform::Error">Error</a></h4></section><section id="associatedtype.Transform-1" class="associatedtype trait-impl"><a class="src rightside" href="../src/actix_service/transform.rs.html#109">source</a><a href="#associatedtype.Transform-1" class="anchor">§</a><h4 class="code-header">type <a href="#associatedtype.Transform" class="associatedtype">Transform</a> = &lt;T as <a class="trait" href="trait.Transform.html" title="trait actix_service::Transform">Transform</a>&lt;S, Req&gt;&gt;::<a class="associatedtype" href="trait.Transform.html#associatedtype.Transform" title="type actix_service::Transform::Transform">Transform</a></h4></section><section id="associatedtype.InitError-1" class="associatedtype trait-impl"><a class="src rightside" href="../src/actix_service/transform.rs.html#110">source</a><a href="#associatedtype.InitError-1" class="anchor">§</a><h4 class="code-header">type <a href="#associatedtype.InitError" class="associatedtype">InitError</a> = &lt;T as <a class="trait" href="trait.Transform.html" title="trait actix_service::Transform">Transform</a>&lt;S, Req&gt;&gt;::<a class="associatedtype" href="trait.Transform.html#associatedtype.InitError" title="type actix_service::Transform::InitError">InitError</a></h4></section><section id="associatedtype.Future-1" class="associatedtype trait-impl"><a class="src rightside" href="../src/actix_service/transform.rs.html#111">source</a><a href="#associatedtype.Future-1" class="anchor">§</a><h4 class="code-header">type <a href="#associatedtype.Future" class="associatedtype">Future</a> = &lt;T as <a class="trait" href="trait.Transform.html" title="trait actix_service::Transform">Transform</a>&lt;S, Req&gt;&gt;::<a class="associatedtype" href="trait.Transform.html#associatedtype.Future" title="type actix_service::Transform::Future">Future</a></h4></section><section id="method.new_transform" class="method trait-impl"><a class="src rightside" href="../src/actix_service/transform.rs.html#113-115">source</a><a href="#method.new_transform" class="anchor">§</a><h4 class="code-header">fn <a href="#tymethod.new_transform" class="fn">new_transform</a>(&amp;self, service: S) -&gt; T::<a class="associatedtype" href="trait.Transform.html#associatedtype.Future" title="type actix_service::Transform::Future">Future</a></h4></section></div></details><details class="toggle implementors-toggle"><summary><section id="impl-Transform%3CS,+Req%3E-for-Arc%3CT%3E" class="impl"><a class="src rightside" href="../src/actix_service/transform.rs.html#118-131">source</a><a href="#impl-Transform%3CS,+Req%3E-for-Arc%3CT%3E" class="anchor">§</a><h3 class="code-header">impl&lt;T, S, Req&gt; <a class="trait" href="trait.Transform.html" title="trait actix_service::Transform">Transform</a>&lt;S, Req&gt; for <a class="struct" href="https://doc.rust-lang.org/nightly/alloc/sync/struct.Arc.html" title="struct alloc::sync::Arc">Arc</a>&lt;T&gt;<div class="where">where
T: <a class="trait" href="trait.Transform.html" title="trait actix_service::Transform">Transform</a>&lt;S, Req&gt;,</div></h3></section></summary><div class="impl-items"><section id="associatedtype.Response-2" class="associatedtype trait-impl"><a class="src rightside" href="../src/actix_service/transform.rs.html#122">source</a><a href="#associatedtype.Response-2" class="anchor">§</a><h4 class="code-header">type <a href="#associatedtype.Response" class="associatedtype">Response</a> = &lt;T as <a class="trait" href="trait.Transform.html" title="trait actix_service::Transform">Transform</a>&lt;S, Req&gt;&gt;::<a class="associatedtype" href="trait.Transform.html#associatedtype.Response" title="type actix_service::Transform::Response">Response</a></h4></section><section id="associatedtype.Error-2" class="associatedtype trait-impl"><a class="src rightside" href="../src/actix_service/transform.rs.html#123">source</a><a href="#associatedtype.Error-2" class="anchor">§</a><h4 class="code-header">type <a href="#associatedtype.Error" class="associatedtype">Error</a> = &lt;T as <a class="trait" href="trait.Transform.html" title="trait actix_service::Transform">Transform</a>&lt;S, Req&gt;&gt;::<a class="associatedtype" href="trait.Transform.html#associatedtype.Error" title="type actix_service::Transform::Error">Error</a></h4></section><section id="associatedtype.Transform-2" class="associatedtype trait-impl"><a class="src rightside" href="../src/actix_service/transform.rs.html#124">source</a><a href="#associatedtype.Transform-2" class="anchor">§</a><h4 class="code-header">type <a href="#associatedtype.Transform" class="associatedtype">Transform</a> = &lt;T as <a class="trait" href="trait.Transform.html" title="trait actix_service::Transform">Transform</a>&lt;S, Req&gt;&gt;::<a class="associatedtype" href="trait.Transform.html#associatedtype.Transform" title="type actix_service::Transform::Transform">Transform</a></h4></section><section id="associatedtype.InitError-2" class="associatedtype trait-impl"><a class="src rightside" href="../src/actix_service/transform.rs.html#125">source</a><a href="#associatedtype.InitError-2" class="anchor">§</a><h4 class="code-header">type <a href="#associatedtype.InitError" class="associatedtype">InitError</a> = &lt;T as <a class="trait" href="trait.Transform.html" title="trait actix_service::Transform">Transform</a>&lt;S, Req&gt;&gt;::<a class="associatedtype" href="trait.Transform.html#associatedtype.InitError" title="type actix_service::Transform::InitError">InitError</a></h4></section><section id="associatedtype.Future-2" class="associatedtype trait-impl"><a class="src rightside" href="../src/actix_service/transform.rs.html#126">source</a><a href="#associatedtype.Future-2" class="anchor">§</a><h4 class="code-header">type <a href="#associatedtype.Future" class="associatedtype">Future</a> = &lt;T as <a class="trait" href="trait.Transform.html" title="trait actix_service::Transform">Transform</a>&lt;S, Req&gt;&gt;::<a class="associatedtype" href="trait.Transform.html#associatedtype.Future" title="type actix_service::Transform::Future">Future</a></h4></section><section id="method.new_transform-1" class="method trait-impl"><a class="src rightside" href="../src/actix_service/transform.rs.html#128-130">source</a><a href="#method.new_transform-1" class="anchor">§</a><h4 class="code-header">fn <a href="#tymethod.new_transform" class="fn">new_transform</a>(&amp;self, service: S) -&gt; T::<a class="associatedtype" href="trait.Transform.html#associatedtype.Future" title="type actix_service::Transform::Future">Future</a></h4></section></div></details><h2 id="implementors" class="section-header">Implementors<a href="#implementors" class="anchor">§</a></h2><div id="implementors-list"></div><script src="../trait.impl/actix_service/transform/trait.Transform.js" data-ignore-extern-crates="alloc" async></script></section></div></main></body></html>