hyper/client/conn/http2.rs
1//! HTTP/2 client connections
2
3use std::error::Error;
4use std::fmt;
5use std::future::Future;
6use std::marker::PhantomData;
7use std::pin::Pin;
8use std::sync::Arc;
9use std::task::{Context, Poll};
10use std::time::Duration;
11
12use crate::rt::{Read, Write};
13use futures_core::ready;
14use http::{Request, Response};
15
16use super::super::dispatch::{self, TrySendError};
17use crate::body::{Body, Incoming as IncomingBody};
18use crate::common::time::Time;
19use crate::proto;
20use crate::rt::bounds::Http2ClientConnExec;
21use crate::rt::Timer;
22
23/// The sender side of an established connection.
24pub struct SendRequest<B> {
25 dispatch: dispatch::UnboundedSender<Request<B>, Response<IncomingBody>>,
26}
27
28impl<B> Clone for SendRequest<B> {
29 fn clone(&self) -> SendRequest<B> {
30 SendRequest {
31 dispatch: self.dispatch.clone(),
32 }
33 }
34}
35
36/// A future that processes all HTTP state for the IO object.
37///
38/// In most cases, this should just be spawned into an executor, so that it
39/// can process incoming and outgoing messages, notice hangups, and the like.
40///
41/// Instances of this type are typically created via the [`handshake`] function
42#[must_use = "futures do nothing unless polled"]
43pub struct Connection<T, B, E>
44where
45 T: Read + Write + Unpin,
46 B: Body + 'static,
47 E: Http2ClientConnExec<B, T> + Unpin,
48 B::Error: Into<Box<dyn Error + Send + Sync>>,
49{
50 inner: (PhantomData<T>, proto::h2::ClientTask<B, E, T>),
51}
52
53/// A builder to configure an HTTP connection.
54///
55/// After setting options, the builder is used to create a handshake future.
56///
57/// **Note**: The default values of options are *not considered stable*. They
58/// are subject to change at any time.
59#[derive(Clone, Debug)]
60pub struct Builder<Ex> {
61 pub(super) exec: Ex,
62 pub(super) timer: Time,
63 h2_builder: proto::h2::client::Config,
64}
65
66/// Returns a handshake future over some IO.
67///
68/// This is a shortcut for `Builder::new(exec).handshake(io)`.
69/// See [`client::conn`](crate::client::conn) for more.
70pub async fn handshake<E, T, B>(
71 exec: E,
72 io: T,
73) -> crate::Result<(SendRequest<B>, Connection<T, B, E>)>
74where
75 T: Read + Write + Unpin,
76 B: Body + 'static,
77 B::Data: Send,
78 B::Error: Into<Box<dyn Error + Send + Sync>>,
79 E: Http2ClientConnExec<B, T> + Unpin + Clone,
80{
81 Builder::new(exec).handshake(io).await
82}
83
84// ===== impl SendRequest
85
86impl<B> SendRequest<B> {
87 /// Polls to determine whether this sender can be used yet for a request.
88 ///
89 /// If the associated connection is closed, this returns an Error.
90 pub fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<crate::Result<()>> {
91 if self.is_closed() {
92 Poll::Ready(Err(crate::Error::new_closed()))
93 } else {
94 Poll::Ready(Ok(()))
95 }
96 }
97
98 /// Waits until the dispatcher is ready
99 ///
100 /// If the associated connection is closed, this returns an Error.
101 pub async fn ready(&mut self) -> crate::Result<()> {
102 crate::common::future::poll_fn(|cx| self.poll_ready(cx)).await
103 }
104
105 /// Checks if the connection is currently ready to send a request.
106 ///
107 /// # Note
108 ///
109 /// This is mostly a hint. Due to inherent latency of networks, it is
110 /// possible that even after checking this is ready, sending a request
111 /// may still fail because the connection was closed in the meantime.
112 pub fn is_ready(&self) -> bool {
113 self.dispatch.is_ready()
114 }
115
116 /// Checks if the connection side has been closed.
117 pub fn is_closed(&self) -> bool {
118 self.dispatch.is_closed()
119 }
120}
121
122impl<B> SendRequest<B>
123where
124 B: Body + 'static,
125{
126 /// Sends a `Request` on the associated connection.
127 ///
128 /// Returns a future that if successful, yields the `Response`.
129 ///
130 /// `req` must have a `Host` header.
131 ///
132 /// Absolute-form `Uri`s are not required. If received, they will be serialized
133 /// as-is.
134 pub fn send_request(
135 &mut self,
136 req: Request<B>,
137 ) -> impl Future<Output = crate::Result<Response<IncomingBody>>> {
138 let sent = self.dispatch.send(req);
139
140 async move {
141 match sent {
142 Ok(rx) => match rx.await {
143 Ok(Ok(resp)) => Ok(resp),
144 Ok(Err(err)) => Err(err),
145 // this is definite bug if it happens, but it shouldn't happen!
146 Err(_canceled) => panic!("dispatch dropped without returning error"),
147 },
148 Err(_req) => {
149 debug!("connection was not ready");
150
151 Err(crate::Error::new_canceled().with("connection was not ready"))
152 }
153 }
154 }
155 }
156
157 /// Sends a `Request` on the associated connection.
158 ///
159 /// Returns a future that if successful, yields the `Response`.
160 ///
161 /// # Error
162 ///
163 /// If there was an error before trying to serialize the request to the
164 /// connection, the message will be returned as part of this error.
165 pub fn try_send_request(
166 &mut self,
167 req: Request<B>,
168 ) -> impl Future<Output = Result<Response<IncomingBody>, TrySendError<Request<B>>>> {
169 let sent = self.dispatch.try_send(req);
170 async move {
171 match sent {
172 Ok(rx) => match rx.await {
173 Ok(Ok(res)) => Ok(res),
174 Ok(Err(err)) => Err(err),
175 // this is definite bug if it happens, but it shouldn't happen!
176 Err(_) => panic!("dispatch dropped without returning error"),
177 },
178 Err(req) => {
179 debug!("connection was not ready");
180 let error = crate::Error::new_canceled().with("connection was not ready");
181 Err(TrySendError {
182 error,
183 message: Some(req),
184 })
185 }
186 }
187 }
188 }
189}
190
191impl<B> fmt::Debug for SendRequest<B> {
192 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
193 f.debug_struct("SendRequest").finish()
194 }
195}
196
197// ===== impl Connection
198
199impl<T, B, E> Connection<T, B, E>
200where
201 T: Read + Write + Unpin + 'static,
202 B: Body + Unpin + 'static,
203 B::Data: Send,
204 B::Error: Into<Box<dyn Error + Send + Sync>>,
205 E: Http2ClientConnExec<B, T> + Unpin,
206{
207 /// Returns whether the [extended CONNECT protocol][1] is enabled or not.
208 ///
209 /// This setting is configured by the server peer by sending the
210 /// [`SETTINGS_ENABLE_CONNECT_PROTOCOL` parameter][2] in a `SETTINGS` frame.
211 /// This method returns the currently acknowledged value received from the
212 /// remote.
213 ///
214 /// [1]: https://datatracker.ietf.org/doc/html/rfc8441#section-4
215 /// [2]: https://datatracker.ietf.org/doc/html/rfc8441#section-3
216 pub fn is_extended_connect_protocol_enabled(&self) -> bool {
217 self.inner.1.is_extended_connect_protocol_enabled()
218 }
219
220 /// Returns the current maximum send stream count.
221 ///
222 /// This setting is configured in a [`SETTINGS_MAX_CONCURRENT_STREAMS` parameter][1] in a `SETTINGS` frame,
223 /// and may change throughout the connection lifetime.
224 ///
225 /// [1]: https://datatracker.ietf.org/doc/html/rfc7540#section-5.1.2
226 pub fn current_max_send_streams(&self) -> usize {
227 self.inner.1.current_max_send_streams()
228 }
229
230 /// Returns the current maximum receive stream count.
231 ///
232 /// This setting is configured in a [`SETTINGS_MAX_CONCURRENT_STREAMS` parameter][1] in a `SETTINGS` frame,
233 /// and may change throughout the connection lifetime.
234 ///
235 /// [1]: https://datatracker.ietf.org/doc/html/rfc7540#section-5.1.2
236 pub fn current_max_recv_streams(&self) -> usize {
237 self.inner.1.current_max_recv_streams()
238 }
239}
240
241impl<T, B, E> fmt::Debug for Connection<T, B, E>
242where
243 T: Read + Write + fmt::Debug + 'static + Unpin,
244 B: Body + 'static,
245 E: Http2ClientConnExec<B, T> + Unpin,
246 B::Error: Into<Box<dyn Error + Send + Sync>>,
247{
248 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
249 f.debug_struct("Connection").finish()
250 }
251}
252
253impl<T, B, E> Future for Connection<T, B, E>
254where
255 T: Read + Write + Unpin + 'static,
256 B: Body + 'static + Unpin,
257 B::Data: Send,
258 E: Unpin,
259 B::Error: Into<Box<dyn Error + Send + Sync>>,
260 E: Http2ClientConnExec<B, T> + Unpin,
261{
262 type Output = crate::Result<()>;
263
264 fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
265 match ready!(Pin::new(&mut self.inner.1).poll(cx))? {
266 proto::Dispatched::Shutdown => Poll::Ready(Ok(())),
267 #[cfg(feature = "http1")]
268 proto::Dispatched::Upgrade(_pending) => unreachable!("http2 cannot upgrade"),
269 }
270 }
271}
272
273// ===== impl Builder
274
275impl<Ex> Builder<Ex>
276where
277 Ex: Clone,
278{
279 /// Creates a new connection builder.
280 #[inline]
281 pub fn new(exec: Ex) -> Builder<Ex> {
282 Builder {
283 exec,
284 timer: Time::Empty,
285 h2_builder: Default::default(),
286 }
287 }
288
289 /// Provide a timer to execute background HTTP2 tasks.
290 pub fn timer<M>(&mut self, timer: M) -> &mut Builder<Ex>
291 where
292 M: Timer + Send + Sync + 'static,
293 {
294 self.timer = Time::Timer(Arc::new(timer));
295 self
296 }
297
298 /// Sets the [`SETTINGS_INITIAL_WINDOW_SIZE`][spec] option for HTTP2
299 /// stream-level flow control.
300 ///
301 /// Passing `None` will do nothing.
302 ///
303 /// If not set, hyper will use a default.
304 ///
305 /// [spec]: https://httpwg.org/specs/rfc9113.html#SETTINGS_INITIAL_WINDOW_SIZE
306 pub fn initial_stream_window_size(&mut self, sz: impl Into<Option<u32>>) -> &mut Self {
307 if let Some(sz) = sz.into() {
308 self.h2_builder.adaptive_window = false;
309 self.h2_builder.initial_stream_window_size = sz;
310 }
311 self
312 }
313
314 /// Sets the max connection-level flow control for HTTP2
315 ///
316 /// Passing `None` will do nothing.
317 ///
318 /// If not set, hyper will use a default.
319 pub fn initial_connection_window_size(&mut self, sz: impl Into<Option<u32>>) -> &mut Self {
320 if let Some(sz) = sz.into() {
321 self.h2_builder.adaptive_window = false;
322 self.h2_builder.initial_conn_window_size = sz;
323 }
324 self
325 }
326
327 /// Sets the initial maximum of locally initiated (send) streams.
328 ///
329 /// This value will be overwritten by the value included in the initial
330 /// SETTINGS frame received from the peer as part of a [connection preface].
331 ///
332 /// Passing `None` will do nothing.
333 ///
334 /// If not set, hyper will use a default.
335 ///
336 /// [connection preface]: https://httpwg.org/specs/rfc9113.html#preface
337 pub fn initial_max_send_streams(&mut self, initial: impl Into<Option<usize>>) -> &mut Self {
338 if let Some(initial) = initial.into() {
339 self.h2_builder.initial_max_send_streams = initial;
340 }
341 self
342 }
343
344 /// Sets whether to use an adaptive flow control.
345 ///
346 /// Enabling this will override the limits set in
347 /// `initial_stream_window_size` and
348 /// `initial_connection_window_size`.
349 pub fn adaptive_window(&mut self, enabled: bool) -> &mut Self {
350 use proto::h2::SPEC_WINDOW_SIZE;
351
352 self.h2_builder.adaptive_window = enabled;
353 if enabled {
354 self.h2_builder.initial_conn_window_size = SPEC_WINDOW_SIZE;
355 self.h2_builder.initial_stream_window_size = SPEC_WINDOW_SIZE;
356 }
357 self
358 }
359
360 /// Sets the maximum frame size to use for HTTP2.
361 ///
362 /// Default is currently 16KB, but can change.
363 pub fn max_frame_size(&mut self, sz: impl Into<Option<u32>>) -> &mut Self {
364 self.h2_builder.max_frame_size = sz.into();
365 self
366 }
367
368 /// Sets the max size of received header frames.
369 ///
370 /// Default is to not send the setting, allowing the peer's default.
371 pub fn max_header_list_size(&mut self, max: u32) -> &mut Self {
372 self.h2_builder.max_header_list_size = Some(max);
373 self
374 }
375
376 /// Sets the header table size.
377 ///
378 /// This setting informs the peer of the maximum size of the header compression
379 /// table used to encode header blocks, in octets. The encoder may select any value
380 /// equal to or less than the header table size specified by the sender.
381 ///
382 /// The default value of crate `h2` is 4,096.
383 pub fn header_table_size(&mut self, size: impl Into<Option<u32>>) -> &mut Self {
384 self.h2_builder.header_table_size = size.into();
385 self
386 }
387
388 /// Sets whether to enable server push.
389 ///
390 /// This setting can be used to disable server push. Clients SHOULD
391 /// set this to false, as server push is rarely used and can
392 /// be a security risk.
393 ///
394 /// The default value is `false`.
395 pub fn enable_push(&mut self, enabled: bool) -> &mut Self {
396 self.h2_builder.enable_push = enabled;
397 self
398 }
399
400 /// Sets the maximum number of concurrent streams.
401 ///
402 /// The maximum concurrent streams setting only controls the maximum number
403 /// of streams that can be initiated by the remote peer. In other words,
404 /// when this setting is set to 100, this does not limit the number of
405 /// concurrent streams that can be created by the caller.
406 ///
407 /// It is recommended that this value be no smaller than 100, so as to not
408 /// unnecessarily limit parallelism. However, any value is legal, including
409 /// 0. If `max` is set to 0, then the remote will not be permitted to
410 /// initiate streams.
411 ///
412 /// Note that streams in the reserved state, i.e., push promises that have
413 /// been reserved but the stream has not started, do not count against this
414 /// setting.
415 ///
416 /// Also note that if the remote *does* exceed the value set here, it is not
417 /// a protocol level error. Instead, the `h2` library will immediately reset
418 /// the stream.
419 ///
420 /// See [Section 5.1.2] in the HTTP/2 spec for more details.
421 ///
422 /// [Section 5.1.2]: https://httpwg.org/specs/rfc7540.html#rfc.section.5.1.2
423 pub fn max_concurrent_streams(&mut self, max: impl Into<Option<u32>>) -> &mut Self {
424 self.h2_builder.max_concurrent_streams = max.into();
425 self
426 }
427
428 /// Sets an interval for HTTP2 Ping frames should be sent to keep a
429 /// connection alive.
430 ///
431 /// Pass `None` to disable HTTP2 keep-alive.
432 ///
433 /// Default is currently disabled.
434 pub fn keep_alive_interval(&mut self, interval: impl Into<Option<Duration>>) -> &mut Self {
435 self.h2_builder.keep_alive_interval = interval.into();
436 self
437 }
438
439 /// Sets a timeout for receiving an acknowledgement of the keep-alive ping.
440 ///
441 /// If the ping is not acknowledged within the timeout, the connection will
442 /// be closed. Does nothing if `keep_alive_interval` is disabled.
443 ///
444 /// Default is 20 seconds.
445 pub fn keep_alive_timeout(&mut self, timeout: Duration) -> &mut Self {
446 self.h2_builder.keep_alive_timeout = timeout;
447 self
448 }
449
450 /// Sets whether HTTP2 keep-alive should apply while the connection is idle.
451 ///
452 /// If disabled, keep-alive pings are only sent while there are open
453 /// request/responses streams. If enabled, pings are also sent when no
454 /// streams are active. Does nothing if `keep_alive_interval` is
455 /// disabled.
456 ///
457 /// Default is `false`.
458 pub fn keep_alive_while_idle(&mut self, enabled: bool) -> &mut Self {
459 self.h2_builder.keep_alive_while_idle = enabled;
460 self
461 }
462
463 /// Sets the maximum number of HTTP2 concurrent locally reset streams.
464 ///
465 /// See the documentation of [`h2::client::Builder::max_concurrent_reset_streams`] for more
466 /// details.
467 ///
468 /// The default value is determined by the `h2` crate.
469 ///
470 /// [`h2::client::Builder::max_concurrent_reset_streams`]: https://docs.rs/h2/client/struct.Builder.html#method.max_concurrent_reset_streams
471 pub fn max_concurrent_reset_streams(&mut self, max: usize) -> &mut Self {
472 self.h2_builder.max_concurrent_reset_streams = Some(max);
473 self
474 }
475
476 /// Set the maximum write buffer size for each HTTP/2 stream.
477 ///
478 /// Default is currently 1MB, but may change.
479 ///
480 /// # Panics
481 ///
482 /// The value must be no larger than `u32::MAX`.
483 pub fn max_send_buf_size(&mut self, max: usize) -> &mut Self {
484 assert!(max <= u32::MAX as usize);
485 self.h2_builder.max_send_buffer_size = max;
486 self
487 }
488
489 /// Configures the maximum number of pending reset streams allowed before a GOAWAY will be sent.
490 ///
491 /// This will default to the default value set by the [`h2` crate](https://crates.io/crates/h2).
492 /// As of v0.4.0, it is 20.
493 ///
494 /// See <https://github.com/hyperium/hyper/issues/2877> for more information.
495 pub fn max_pending_accept_reset_streams(&mut self, max: impl Into<Option<usize>>) -> &mut Self {
496 self.h2_builder.max_pending_accept_reset_streams = max.into();
497 self
498 }
499
500 /// Sets the HTTP/2 pseudo-header field order for outgoing HEADERS frames.
501 ///
502 /// This determines the order in which pseudo-header fields (such as `:method`, `:scheme`, etc.)
503 /// are encoded in the HEADERS frame. Customizing the order may be useful for interoperability
504 /// or testing purposes.
505 pub fn headers_pseudo_order(&mut self, order: h2::frame::PseudoOrder) -> &mut Self {
506 self.h2_builder.headers_pseudo_order = Some(order);
507 self
508 }
509
510 /// Sets whether to include PRIORITY flag in HTTP/2 HEADERS frames.
511 ///
512 /// When enabled, HEADERS frames will include priority data (weight=0,
513 /// stream_dependency=0, exclusive=1) matching Chrome's HTTP/2 behavior.
514 pub fn headers_priority(&mut self, data: Option<(u8, u32, bool)>) -> &mut Self {
515 self.h2_builder.headers_priority = data;
516 self
517 }
518
519 /// Sets the HTTP/2 regular header ordering for browser fingerprinting.
520 ///
521 /// When set, headers are encoded in the specified order instead of hash-based order.
522 pub fn headers_order(&mut self, order: Vec<http::HeaderName>) -> &mut Self {
523 self.h2_builder.headers_order = Some(order);
524 self
525 }
526
527 /// Sets the HTTP/2 SETTINGS frame order for the connection preface.
528 ///
529 /// This determines the order in which settings are sent in the initial SETTINGS frame.
530 /// Customizing the order may be useful for testing or protocol compliance.
531 pub fn settings_order(&mut self, order: h2::frame::SettingsOrder) -> &mut Self {
532 self.h2_builder.settings_order = Some(order);
533 self
534 }
535
536 /// Sets whether to disable RFC 7540 priorities.
537 ///
538 /// This corresponds to the `SETTINGS_NO_RFC7540_PRIORITIES` (0x9) setting.
539 /// When enabled, the client will not send priority frames and will ignore
540 /// priority frames received from the server.
541 ///
542 /// Default is `None` (not set).
543 pub fn no_rfc7540_priorities(&mut self, enabled: bool) -> &mut Self {
544 self.h2_builder.no_rfc7540_priorities = Some(enabled);
545 self
546 }
547
548 /// Sets the `SETTINGS_ENABLE_CONNECT_PROTOCOL` value.
549 ///
550 /// This corresponds to the `SETTINGS_ENABLE_CONNECT_PROTOCOL` (0x8) setting.
551 /// When enabled, the client advertises support for Extended CONNECT (RFC 8441).
552 ///
553 /// Default is `None` (not set).
554 pub fn enable_connect_protocol(&mut self, val: bool) -> &mut Self {
555 self.h2_builder.enable_connect_protocol = Some(val);
556 self
557 }
558
559 /// Sets the initial HTTP/2 stream ID for the connection.
560 ///
561 /// Firefox starts at stream ID 3 (skipping stream 1), Chrome uses the default of 1.
562 pub fn initial_stream_id(&mut self, stream_id: u32) -> &mut Self {
563 self.h2_builder.initial_stream_id = Some(stream_id);
564 self
565 }
566
567 /// Sets extra receive window capacity to add to new locally-initiated streams.
568 ///
569 /// When set, after creating a new stream, a WINDOW_UPDATE frame will be
570 /// sent to increase the stream's receive window by this amount.
571 /// This is used for browser fingerprinting (e.g. Firefox adds 12451840
572 /// to the first stream's receive window).
573 pub fn initial_stream_window_size_increment(&mut self, size: u32) -> &mut Self {
574 self.h2_builder.initial_stream_window_increment = Some(size);
575 self
576 }
577
578 /// Configures the maximum number of local resets due to protocol errors made by the remote end.
579 ///
580 /// See the documentation of [`h2::client::Builder::max_local_error_reset_streams`] for more
581 /// details.
582 ///
583 /// The default value is 1024.
584 pub fn max_local_error_reset_streams(&mut self, max: impl Into<Option<usize>>) -> &mut Self {
585 self.h2_builder.max_local_error_reset_streams = max.into();
586 self
587 }
588
589 /// Constructs a connection with the configured options and IO.
590 /// See [`client::conn`](crate::client::conn) for more.
591 ///
592 /// Note, if [`Connection`] is not `await`-ed, [`SendRequest`] will
593 /// do nothing.
594 pub fn handshake<T, B>(
595 &self,
596 io: T,
597 ) -> impl Future<Output = crate::Result<(SendRequest<B>, Connection<T, B, Ex>)>>
598 where
599 T: Read + Write + Unpin,
600 B: Body + 'static,
601 B::Data: Send,
602 B::Error: Into<Box<dyn Error + Send + Sync>>,
603 Ex: Http2ClientConnExec<B, T> + Unpin,
604 {
605 let opts = self.clone();
606
607 async move {
608 trace!("client handshake HTTP/2");
609
610 let (tx, rx) = dispatch::channel();
611 let h2 = proto::h2::client::handshake(io, rx, &opts.h2_builder, opts.exec, opts.timer)
612 .await?;
613 Ok((
614 SendRequest {
615 dispatch: tx.unbound(),
616 },
617 Connection {
618 inner: (PhantomData, h2),
619 },
620 ))
621 }
622 }
623}
624
625#[cfg(test)]
626mod tests {
627
628 #[tokio::test]
629 #[ignore] // only compilation is checked
630 async fn send_sync_executor_of_non_send_futures() {
631 #[derive(Clone)]
632 struct LocalTokioExecutor;
633
634 impl<F> crate::rt::Executor<F> for LocalTokioExecutor
635 where
636 F: std::future::Future + 'static, // not requiring `Send`
637 {
638 fn execute(&self, fut: F) {
639 // This will spawn into the currently running `LocalSet`.
640 tokio::task::spawn_local(fut);
641 }
642 }
643
644 #[allow(unused)]
645 async fn run(io: impl crate::rt::Read + crate::rt::Write + Unpin + 'static) {
646 let (_sender, conn) = crate::client::conn::http2::handshake::<
647 _,
648 _,
649 http_body_util::Empty<bytes::Bytes>,
650 >(LocalTokioExecutor, io)
651 .await
652 .unwrap();
653
654 tokio::task::spawn_local(async move {
655 conn.await.unwrap();
656 });
657 }
658 }
659
660 #[tokio::test]
661 #[ignore] // only compilation is checked
662 async fn not_send_not_sync_executor_of_not_send_futures() {
663 #[derive(Clone)]
664 struct LocalTokioExecutor {
665 _x: std::marker::PhantomData<std::rc::Rc<()>>,
666 }
667
668 impl<F> crate::rt::Executor<F> for LocalTokioExecutor
669 where
670 F: std::future::Future + 'static, // not requiring `Send`
671 {
672 fn execute(&self, fut: F) {
673 // This will spawn into the currently running `LocalSet`.
674 tokio::task::spawn_local(fut);
675 }
676 }
677
678 #[allow(unused)]
679 async fn run(io: impl crate::rt::Read + crate::rt::Write + Unpin + 'static) {
680 let (_sender, conn) =
681 crate::client::conn::http2::handshake::<_, _, http_body_util::Empty<bytes::Bytes>>(
682 LocalTokioExecutor {
683 _x: Default::default(),
684 },
685 io,
686 )
687 .await
688 .unwrap();
689
690 tokio::task::spawn_local(async move {
691 conn.await.unwrap();
692 });
693 }
694 }
695
696 #[tokio::test]
697 #[ignore] // only compilation is checked
698 async fn send_not_sync_executor_of_not_send_futures() {
699 #[derive(Clone)]
700 struct LocalTokioExecutor {
701 _x: std::marker::PhantomData<std::cell::Cell<()>>,
702 }
703
704 impl<F> crate::rt::Executor<F> for LocalTokioExecutor
705 where
706 F: std::future::Future + 'static, // not requiring `Send`
707 {
708 fn execute(&self, fut: F) {
709 // This will spawn into the currently running `LocalSet`.
710 tokio::task::spawn_local(fut);
711 }
712 }
713
714 #[allow(unused)]
715 async fn run(io: impl crate::rt::Read + crate::rt::Write + Unpin + 'static) {
716 let (_sender, conn) =
717 crate::client::conn::http2::handshake::<_, _, http_body_util::Empty<bytes::Bytes>>(
718 LocalTokioExecutor {
719 _x: Default::default(),
720 },
721 io,
722 )
723 .await
724 .unwrap();
725
726 tokio::task::spawn_local(async move {
727 conn.await.unwrap();
728 });
729 }
730 }
731
732 #[tokio::test]
733 #[ignore] // only compilation is checked
734 async fn send_sync_executor_of_send_futures() {
735 #[derive(Clone)]
736 struct TokioExecutor;
737
738 impl<F> crate::rt::Executor<F> for TokioExecutor
739 where
740 F: std::future::Future + 'static + Send,
741 F::Output: Send + 'static,
742 {
743 fn execute(&self, fut: F) {
744 tokio::task::spawn(fut);
745 }
746 }
747
748 #[allow(unused)]
749 async fn run(io: impl crate::rt::Read + crate::rt::Write + Send + Unpin + 'static) {
750 let (_sender, conn) = crate::client::conn::http2::handshake::<
751 _,
752 _,
753 http_body_util::Empty<bytes::Bytes>,
754 >(TokioExecutor, io)
755 .await
756 .unwrap();
757
758 tokio::task::spawn(async move {
759 conn.await.unwrap();
760 });
761 }
762 }
763
764 #[tokio::test]
765 #[ignore] // only compilation is checked
766 async fn send_not_sync_executor_of_send_futures() {
767 #[derive(Clone)]
768 struct TokioExecutor {
769 // !Sync
770 _x: std::marker::PhantomData<std::cell::Cell<()>>,
771 }
772
773 impl<F> crate::rt::Executor<F> for TokioExecutor
774 where
775 F: std::future::Future + 'static + Send,
776 F::Output: Send + 'static,
777 {
778 fn execute(&self, fut: F) {
779 tokio::task::spawn(fut);
780 }
781 }
782
783 #[allow(unused)]
784 async fn run(io: impl crate::rt::Read + crate::rt::Write + Send + Unpin + 'static) {
785 let (_sender, conn) =
786 crate::client::conn::http2::handshake::<_, _, http_body_util::Empty<bytes::Bytes>>(
787 TokioExecutor {
788 _x: Default::default(),
789 },
790 io,
791 )
792 .await
793 .unwrap();
794
795 tokio::task::spawn_local(async move {
796 // can't use spawn here because when executor is !Send
797 conn.await.unwrap();
798 });
799 }
800 }
801}