43 lines
7.5 KiB
HTML
43 lines
7.5 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="Capture the connection for a given request"><title>capture_connection in hyper_util::client::legacy::connect - 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="hyper_util" 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="sidebar-items.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 fn"><!--[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="#">capture_connection</a></h2></rustdoc-topbar><nav class="sidebar"><div class="sidebar-crate"><h2><a href="../../../../hyper_util/index.html">hyper_<wbr>util</a><span class="version">0.1.20</span></h2></div><div class="sidebar-elems"><section id="rustdoc-toc"><h2 class="location"><a href="#">capture_<wbr>connection</a></h2><h3><a href="#">Sections</a></h3><ul class="block top-toc"><li><a href="#examples" title="Examples">Examples</a></li></ul></section><div id="rustdoc-modnav"><h2><a href="index.html">In hyper_<wbr>util::<wbr>client::<wbr>legacy::<wbr>connect</a></h2></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"><div class="rustdoc-breadcrumbs"><a href="../../../index.html">hyper_util</a>::<wbr><a href="../../index.html">client</a>::<wbr><a href="../index.html">legacy</a>::<wbr><a href="index.html">connect</a></div><h1>Function <span class="fn">capture_<wbr>connection</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/hyper_util/client/legacy/connect/capture.rs.html#72-76">Source</a> </span></div><pre class="rust item-decl"><code>pub fn capture_connection<B>(request: &mut <a class="struct" href="../../../../http/request/struct.Request.html" title="struct http::request::Request">Request</a><B>) -> <a class="struct" href="struct.CaptureConnection.html" title="struct hyper_util::client::legacy::connect::CaptureConnection">CaptureConnection</a></code></pre><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>Capture the connection for a given request</p>
|
|
<p>When making a request with Hyper, the underlying connection must implement the <a href="trait.Connection.html" title="trait hyper_util::client::legacy::connect::Connection"><code>Connection</code></a> trait.
|
|
<a href="fn.capture_connection.html" title="fn hyper_util::client::legacy::connect::capture_connection"><code>capture_connection</code></a> allows a caller to capture the returned <a href="struct.Connected.html" title="struct hyper_util::client::legacy::connect::Connected"><code>Connected</code></a> structure as soon
|
|
as the connection is established.</p>
|
|
<p><em>Note</em>: If establishing a connection fails, <a href="struct.CaptureConnection.html#method.connection_metadata" title="method hyper_util::client::legacy::connect::CaptureConnection::connection_metadata"><code>CaptureConnection::connection_metadata</code></a> will always return none.</p>
|
|
<h2 id="examples"><a class="doc-anchor" href="#examples">§</a>Examples</h2>
|
|
<p><strong>Synchronous access</strong>:
|
|
The <a href="struct.CaptureConnection.html#method.connection_metadata" title="method hyper_util::client::legacy::connect::CaptureConnection::connection_metadata"><code>CaptureConnection::connection_metadata</code></a> method allows callers to check if a connection has been
|
|
established. This is ideal for situations where you are certain the connection has already
|
|
been established (e.g. after the response future has already completed).</p>
|
|
|
|
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>hyper_util::client::legacy::connect::capture_connection;
|
|
<span class="kw">let </span><span class="kw-2">mut </span>request = http::Request::builder()
|
|
.uri(<span class="string">"http://foo.com"</span>)
|
|
.body(())
|
|
.unwrap();
|
|
|
|
<span class="kw">let </span>captured_connection = capture_connection(<span class="kw-2">&mut </span>request);
|
|
<span class="comment">// some time later after the request has been sent...
|
|
</span><span class="kw">let </span>connection_info = captured_connection.connection_metadata();
|
|
<span class="macro">println!</span>(<span class="string">"we are connected! {:?}"</span>, connection_info.as_ref());</code></pre></div>
|
|
<p><strong>Asynchronous access</strong>:
|
|
The <a href="struct.CaptureConnection.html#method.wait_for_connection_metadata" title="method hyper_util::client::legacy::connect::CaptureConnection::wait_for_connection_metadata"><code>CaptureConnection::wait_for_connection_metadata</code></a> method returns a future resolves as soon as the
|
|
connection is available.</p>
|
|
|
|
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>hyper_util::client::legacy::connect::capture_connection;
|
|
<span class="kw">use </span>hyper_util::client::legacy::Client;
|
|
<span class="kw">use </span>hyper_util::rt::TokioExecutor;
|
|
<span class="kw">use </span>bytes::Bytes;
|
|
<span class="kw">use </span>http_body_util::Empty;
|
|
<span class="kw">let </span><span class="kw-2">mut </span>request = http::Request::builder()
|
|
.uri(<span class="string">"http://foo.com"</span>)
|
|
.body(Empty::<Bytes>::new())
|
|
.unwrap();
|
|
|
|
<span class="kw">let </span><span class="kw-2">mut </span>captured = capture_connection(<span class="kw-2">&mut </span>request);
|
|
tokio::task::spawn(<span class="kw">async move </span>{
|
|
<span class="kw">let </span>connection_info = captured.wait_for_connection_metadata().<span class="kw">await</span>;
|
|
<span class="macro">println!</span>(<span class="string">"we are connected! {:?}"</span>, connection_info.as_ref());
|
|
});
|
|
|
|
<span class="kw">let </span>client = Client::builder(TokioExecutor::new()).build_http();
|
|
client.request(request).<span class="kw">await</span>.expect(<span class="string">"request failed"</span>);</code></pre></div></div></details></section></div></main></body></html> |