hyperlane-core 22.7.0

A lightweight, high-performance, and cross-platform Rust HTTP server library built on Tokio. It simplifies modern web service development by providing built-in support for middleware, WebSocket, Server-Sent Events (SSE), and raw TCP communication. With a unified and ergonomic API across Windows, Linux, and MacOS, it enables developers to build robust, scalable, and event-driven network applications with minimal overhead and maximum flexibility.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
use super::*;

/// Inherent implementation of [`TlsTransport`].
impl<T> TlsTransport<T>
where
    T: AsyncRead + AsyncWrite + Unpin,
{
    /// Returns the ALPN protocol negotiated during the TLS handshake, if any.
    ///
    /// # Returns
    ///
    /// - `Option<&[u8]>` - The negotiated ALPN protocol bytes.
    #[inline(always)]
    pub(crate) fn alpn_protocol(&self) -> Option<&[u8]> {
        let Self { conn, .. } = self;
        conn.alpn_protocol()
    }

    /// Discards `count` consumed bytes from the front of `in_buf`.
    ///
    /// # Arguments
    ///
    /// - `usize` - Number of leading bytes to drop.
    #[inline(always)]
    fn discard_incoming(&mut self, count: usize) {
        if count == 0 {
            return;
        }
        let Self { in_buf, in_len, .. } = self;
        if count >= *in_len {
            *in_len = 0;
        } else {
            in_buf.copy_within(count..*in_len, 0);
            *in_len -= count;
        }
    }

    /// Flushes any pending encrypted bytes in `out_buf[out_pos..out_len]` to the TCP socket.
    ///
    /// # Arguments
    ///
    /// - `&mut task::Context<'_>` - The current task context.
    ///
    /// # Returns
    ///
    /// - `Poll<io::Result<()>>` - `Ready(Ok(()))` once `out_buf` is fully written.
    #[inline(always)]
    fn poll_flush_out(&mut self, context: &mut task::Context<'_>) -> Poll<io::Result<()>> {
        let Self {
            tcp,
            out_buf,
            out_pos,
            out_len,
            ..
        } = self;
        while *out_pos < *out_len {
            match Pin::new(&mut *tcp).poll_write(context, &out_buf[*out_pos..*out_len]) {
                Poll::Ready(Ok(0)) => {
                    return Poll::Ready(Err(io::Error::new(
                        io::ErrorKind::WriteZero,
                        "tcp write zero during tls flush",
                    )));
                }
                Poll::Ready(Ok(written)) => {
                    *out_pos += written;
                }
                Poll::Ready(Err(error)) => return Poll::Ready(Err(error)),
                Poll::Pending => return Poll::Pending,
            }
        }
        *out_pos = 0;
        *out_len = 0;
        Poll::Ready(Ok(()))
    }

    /// Encrypts one plaintext slice into `out_buf` and immediately writes it to `tcp`.
    ///
    /// # Arguments
    ///
    /// - `&mut task::Context<'_>` - The current task context.
    /// - `&[u8]` - Plaintext bytes (up to `TLS_MAX_PLAINTEXT_CHUNK`).
    /// - `bool` - Whether to emit close notification instead of application data.
    ///
    /// # Returns
    ///
    /// - `Poll<io::Result<usize>>` - Number of plaintext bytes accepted.
    #[inline(always)]
    fn poll_encrypt_and_write(
        &mut self,
        context: &mut task::Context<'_>,
        plaintext: &[u8],
        close_notify: bool,
    ) -> Poll<io::Result<usize>> {
        let Self { write_closed, .. } = self;
        if *write_closed {
            return Poll::Ready(Err(io::Error::new(
                io::ErrorKind::BrokenPipe,
                "tls write side closed",
            )));
        }
        match self.poll_flush_out(context) {
            Poll::Ready(Ok(())) => {}
            Poll::Ready(Err(error)) => return Poll::Ready(Err(error)),
            Poll::Pending => return Poll::Pending,
        }
        if plaintext.is_empty() && !close_notify {
            return Poll::Ready(Ok(0));
        }
        let chunk: &[u8] = &plaintext[..plaintext.len().min(TLS_MAX_PLAINTEXT_CHUNK)];
        loop {
            let Self {
                tcp,
                conn,
                plain_buf,
                out_buf,
                out_pos,
                out_len,
                peer_closed,
                write_closed,
                ..
            } = self;
            let UnbufferedStatus { state, .. } = conn.process_tls_records(&mut []);
            match state {
                Ok(ConnectionState::WriteTraffic(mut wt)) => {
                    let encrypted_len: usize = loop {
                        let encoded: Result<usize, EncryptError> = if close_notify {
                            wt.queue_close_notify(&mut out_buf[*out_len..])
                        } else {
                            wt.encrypt(chunk, &mut out_buf[*out_len..])
                        };
                        match encoded {
                            Ok(n) => break n,
                            Err(EncryptError::InsufficientSize(need)) => {
                                out_buf.resize(*out_len + need.required_size, 0);
                            }
                            Err(error) => {
                                return Poll::Ready(Err(io::Error::other(format!("{error:?}"))));
                            }
                        }
                    };
                    *out_len += encrypted_len;
                    *write_closed = close_notify;
                    match Pin::new(&mut *tcp).poll_write(context, &out_buf[*out_pos..*out_len]) {
                        Poll::Ready(Ok(0)) => {
                            return Poll::Ready(Err(io::Error::new(
                                io::ErrorKind::WriteZero,
                                TLS_TRANSMIT_WRITE_ZERO,
                            )));
                        }
                        Poll::Ready(Ok(written)) => {
                            *out_pos += written;
                            if *out_pos >= *out_len {
                                *out_pos = 0;
                                *out_len = 0;
                            }
                            return Poll::Ready(Ok(chunk.len()));
                        }
                        Poll::Pending => return Poll::Ready(Ok(chunk.len())),
                        Poll::Ready(Err(error)) => return Poll::Ready(Err(error)),
                    }
                }
                Ok(ConnectionState::EncodeTlsData(mut enc)) => loop {
                    match enc.encode(&mut out_buf[*out_len..]) {
                        Ok(n) => {
                            *out_len += n;
                            break;
                        }
                        Err(EncodeError::InsufficientSize(need)) => {
                            out_buf.resize(*out_len + need.required_size, 0);
                        }
                        Err(error) => {
                            return Poll::Ready(Err(io::Error::other(format!("{error:?}"))));
                        }
                    }
                },
                Ok(ConnectionState::TransmitTlsData(transmit)) => {
                    while *out_pos < *out_len {
                        match Pin::new(&mut *tcp).poll_write(context, &out_buf[*out_pos..*out_len])
                        {
                            Poll::Ready(Ok(0)) => {
                                transmit.done();
                                return Poll::Ready(Err(io::Error::new(
                                    io::ErrorKind::WriteZero,
                                    TLS_TRANSMIT_WRITE_ZERO,
                                )));
                            }
                            Poll::Ready(Ok(written)) => {
                                *out_pos += written;
                            }
                            Poll::Ready(Err(error)) => {
                                transmit.done();
                                return Poll::Ready(Err(error));
                            }
                            Poll::Pending => return Poll::Pending,
                        }
                    }
                    *out_pos = 0;
                    *out_len = 0;
                    transmit.done();
                }
                Ok(ConnectionState::ReadTraffic(mut rt)) => {
                    while let Some(rec) = rt.next_record() {
                        match rec {
                            Ok(app) => plain_buf.extend_from_slice(app.payload),
                            Err(error) => return Poll::Ready(Err(io::Error::other(error))),
                        }
                    }
                }
                Ok(ConnectionState::PeerClosed) => {
                    *peer_closed = true;
                }
                Ok(ConnectionState::Closed) if close_notify => return Poll::Ready(Ok(0)),
                Ok(ConnectionState::Closed) => {
                    return Poll::Ready(Err(io::Error::new(
                        io::ErrorKind::BrokenPipe,
                        "tls connection closed",
                    )));
                }
                Err(error) => return Poll::Ready(Err(io::Error::other(error))),
                Ok(_) => {
                    return Poll::Ready(Err(io::Error::other("unexpected tls state on write")));
                }
            }
        }
    }
}

/// Implements [`fmt::Debug`] for [`TlsTransport`].
impl<T> fmt::Debug for TlsTransport<T> {
    /// Formats the unbuffered TLS transport for diagnostics.
    ///
    /// # Arguments
    ///
    /// - `&mut fmt::Formatter<'_>` - The formatter to write into.
    ///
    /// # Returns
    ///
    /// - `fmt::Result` - The formatting result.
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        let Self {
            in_len,
            out_len,
            peer_closed,
            ..
        } = self;
        formatter
            .debug_struct("TlsTransport")
            .field("in_len", in_len)
            .field("out_len", out_len)
            .field("peer_closed", peer_closed)
            .finish()
    }
}

/// Returns pooled record buffers to the worker-local pool on drop.
impl<T> Drop for TlsTransport<T> {
    /// Recycles the incoming and outgoing TLS record buffers.
    fn drop(&mut self) {
        let Self {
            in_buf, out_buf, ..
        } = self;
        return_tls_buffer(mem::take(in_buf));
        return_tls_buffer(mem::take(out_buf));
    }
}

/// Implements [`Transport`] for [`TlsTransport`].
impl<T> Transport for TlsTransport<T>
where
    T: AsyncRead + AsyncWrite + Transport + Unpin + Send + Sync + 'static,
{
    /// Reports the actual peer address.
    ///
    /// # Returns
    ///
    /// - `Result<SocketAddr, io::Error>` - The peer address.
    fn peer_addr(&self) -> Result<SocketAddr, io::Error> {
        let Self { tcp, .. } = self;
        tcp.peer_addr()
    }

    /// Reports the actual local address.
    ///
    /// # Returns
    ///
    /// - `Result<SocketAddr, io::Error>` - The local address.
    fn local_addr(&self) -> Result<SocketAddr, io::Error> {
        let Self { tcp, .. } = self;
        tcp.local_addr()
    }
}

/// Implements [`AsyncRead`] for [`TlsTransport`].
impl<T> AsyncRead for TlsTransport<T>
where
    T: AsyncRead + AsyncWrite + Unpin,
{
    /// Reads decrypted TLS application data into `buffer`.
    ///
    /// # Arguments
    ///
    /// - `Pin<&mut Self>` - The pinned transport.
    /// - `&mut task::Context<'_>` - The current task context.
    /// - `&mut ReadBuf<'_>` - The destination buffer.
    ///
    /// # Returns
    ///
    /// - `Poll<io::Result<()>>` - The read result.
    fn poll_read(
        self: Pin<&mut Self>,
        context: &mut task::Context<'_>,
        buffer: &mut ReadBuf<'_>,
    ) -> Poll<io::Result<()>> {
        let this: &mut Self = self.get_mut();
        match this.poll_flush_out(context) {
            Poll::Ready(Ok(())) => {}
            Poll::Ready(Err(error)) => return Poll::Ready(Err(error)),
            Poll::Pending => return Poll::Pending,
        }
        {
            let Self {
                plain_buf,
                plain_pos,
                peer_closed,
                ..
            } = this;
            if *plain_pos < plain_buf.len() {
                let avail: usize = plain_buf.len() - *plain_pos;
                let take: usize = avail.min(buffer.remaining());
                buffer.put_slice(&plain_buf[*plain_pos..*plain_pos + take]);
                *plain_pos += take;
                if *plain_pos >= plain_buf.len() {
                    plain_buf.clear();
                    *plain_pos = 0;
                }
                return Poll::Ready(Ok(()));
            }
            if *peer_closed {
                return Poll::Ready(Ok(()));
            }
        }
        if buffer.remaining() == 0 {
            return Poll::Ready(Ok(()));
        }
        let initial_filled: usize = buffer.filled().len();
        loop {
            {
                let (discard, action): (usize, Result<u8, io::Error>) = {
                    let Self {
                        conn,
                        in_buf,
                        in_len,
                        plain_buf,
                        out_buf,
                        out_len,
                        peer_closed,
                        ..
                    } = this;
                    let UnbufferedStatus { discard, state } =
                        conn.process_tls_records(&mut in_buf[..*in_len]);
                    let step: Result<u8, io::Error> = match state {
                        Ok(ConnectionState::ReadTraffic(mut rt)) => {
                            let mut err: Option<io::Error> = None;
                            while let Some(rec) = rt.next_record() {
                                match rec {
                                    Ok(app) => {
                                        let payload: &[u8] = app.payload;
                                        if plain_buf.is_empty() && buffer.remaining() > 0 {
                                            let direct: usize =
                                                payload.len().min(buffer.remaining());
                                            buffer.put_slice(&payload[..direct]);
                                            if direct < payload.len() {
                                                plain_buf.extend_from_slice(&payload[direct..]);
                                            }
                                        } else {
                                            plain_buf.extend_from_slice(payload);
                                        }
                                    }
                                    Err(error) => {
                                        err = Some(io::Error::other(error));
                                        break;
                                    }
                                }
                            }
                            match err {
                                Some(error) => Err(error),
                                None => Ok(1),
                            }
                        }
                        Ok(ConnectionState::EncodeTlsData(mut enc)) => loop {
                            match enc.encode(&mut out_buf[*out_len..]) {
                                Ok(n) => {
                                    *out_len += n;
                                    break Ok(2);
                                }
                                Err(EncodeError::InsufficientSize(need)) => {
                                    out_buf.resize(*out_len + need.required_size, 0);
                                }
                                Err(error) => {
                                    break Err(io::Error::other(format!("{error:?}")));
                                }
                            }
                        },
                        Ok(ConnectionState::TransmitTlsData(transmit)) => {
                            if *out_len == 0 {
                                transmit.done();
                                Ok(2)
                            } else {
                                Ok(3)
                            }
                        }
                        Ok(ConnectionState::PeerClosed | ConnectionState::Closed) => {
                            *peer_closed = true;
                            Ok(4)
                        }
                        Ok(ConnectionState::WriteTraffic(_)) => Ok(0),
                        Ok(ConnectionState::ReadEarlyData(mut red)) => {
                            while red.next_record().is_some() {}
                            Ok(2)
                        }
                        Ok(_) => Ok(0),
                        Err(error) => Err(io::Error::other(error)),
                    };
                    (discard, step)
                };
                this.discard_incoming(discard);
                match action {
                    Ok(1) => {
                        if buffer.filled().len() > initial_filled {
                            return Poll::Ready(Ok(()));
                        }
                        continue;
                    }
                    Ok(2) => continue,
                    Ok(3) => match this.poll_flush_out(context) {
                        Poll::Ready(Ok(())) => continue,
                        Poll::Ready(Err(error)) => return Poll::Ready(Err(error)),
                        Poll::Pending => return Poll::Pending,
                    },
                    Ok(4) => return Poll::Ready(Ok(())),
                    Ok(_) => {}
                    Err(error) => return Poll::Ready(Err(error)),
                }
            }
            let Self {
                tcp,
                in_buf,
                in_len,
                ..
            } = this;
            if *in_len == in_buf.len() {
                if in_buf.len() >= TLS_MAX_INCOMING_BUFFER {
                    return Poll::Ready(Err(io::Error::other("tls incoming record exceeds limit")));
                }
                let next_len: usize = (in_buf.len() * 2).min(TLS_MAX_INCOMING_BUFFER);
                in_buf.resize(next_len, 0);
            }
            let mut tcp_buf: ReadBuf<'_> = ReadBuf::new(&mut in_buf[*in_len..]);
            match Pin::new(&mut *tcp).poll_read(context, &mut tcp_buf) {
                Poll::Pending => return Poll::Pending,
                Poll::Ready(Err(error)) => return Poll::Ready(Err(error)),
                Poll::Ready(Ok(())) => {
                    let read_len: usize = tcp_buf.filled().len();
                    if read_len == 0 {
                        return Poll::Ready(Err(io::Error::new(
                            io::ErrorKind::UnexpectedEof,
                            "tls peer closed connection without close_notify",
                        )));
                    }
                    *in_len += read_len;
                }
            }
        }
    }
}

/// Implements [`AsyncWrite`] for [`TlsTransport`].
impl<T> AsyncWrite for TlsTransport<T>
where
    T: AsyncRead + AsyncWrite + Unpin,
{
    /// Encrypts `bytes` in place into the pooled record buffer and writes directly to TCP.
    ///
    /// # Arguments
    ///
    /// - `Pin<&mut Self>` - The pinned transport.
    /// - `&mut task::Context<'_>` - The current task context.
    /// - `&[u8]` - The plaintext bytes to encrypt and write.
    ///
    /// # Returns
    ///
    /// - `Poll<io::Result<usize>>` - Number of plaintext bytes accepted.
    fn poll_write(
        self: Pin<&mut Self>,
        context: &mut task::Context<'_>,
        bytes: &[u8],
    ) -> Poll<io::Result<usize>> {
        self.get_mut().poll_encrypt_and_write(context, bytes, false)
    }

    /// Coalesces multiple `IoSlice` buffers (such as HTTP/2 HEADERS + DATA frames)
    /// into a single TLS record and single TCP write when they fit within one record.
    ///
    /// # Arguments
    ///
    /// - `Pin<&mut Self>` - The pinned transport.
    /// - `&mut task::Context<'_>` - The current task context.
    /// - `&[IoSlice<'_>]` - Ordered plaintext slices to encrypt.
    ///
    /// # Returns
    ///
    /// - `Poll<io::Result<usize>>` - Total plaintext bytes accepted.
    fn poll_write_vectored(
        self: Pin<&mut Self>,
        context: &mut task::Context<'_>,
        buffers: &[IoSlice<'_>],
    ) -> Poll<io::Result<usize>> {
        let this: &mut Self = self.get_mut();
        let mut first_non_empty: Option<&[u8]> = None;
        let mut non_empty_count: usize = 0;
        let mut total_len: usize = 0;
        for slice in buffers {
            let bytes: &[u8] = slice.as_ref();
            if !bytes.is_empty() {
                if first_non_empty.is_none() {
                    first_non_empty = Some(bytes);
                }
                non_empty_count += 1;
                total_len = total_len.saturating_add(bytes.len());
            }
        }
        let Some(first) = first_non_empty else {
            return Poll::Ready(Ok(0));
        };
        if non_empty_count == 1 || first.len() >= TLS_MAX_PLAINTEXT_CHUNK {
            return this.poll_encrypt_and_write(context, first, false);
        }
        let cap: usize = total_len.min(TLS_MAX_PLAINTEXT_CHUNK);
        let mut scratch: [MaybeUninit<u8>; TLS_MAX_PLAINTEXT_CHUNK] =
            [const { MaybeUninit::uninit() }; TLS_MAX_PLAINTEXT_CHUNK];
        let mut filled: usize = 0;
        for slice_item in buffers {
            let bytes: &[u8] = slice_item.as_ref();
            if bytes.is_empty() {
                continue;
            }
            let take: usize = bytes.len().min(cap - filled);
            if take == 0 {
                break;
            }
            // SAFETY: `filled + take <= cap <= TLS_MAX_PLAINTEXT_CHUNK` and `MaybeUninit<u8>`
            // has the exact layout of `u8`.
            unsafe {
                ptr::copy_nonoverlapping(
                    bytes.as_ptr(),
                    scratch.as_mut_ptr().add(filled).cast::<u8>(),
                    take,
                );
            }
            filled += take;
            if filled == cap {
                break;
            }
        }
        // SAFETY: Exactly `0..filled` bytes were initialized in `scratch` above.
        let coalesced: &[u8] =
            unsafe { slice::from_raw_parts(scratch.as_ptr().cast::<u8>(), filled) };
        this.poll_encrypt_and_write(context, coalesced, false)
    }

    /// Advertises vectored write coalescing support so HTTP/2 batches HEADERS + DATA.
    ///
    /// # Returns
    ///
    /// - `bool` - Always `true`.
    fn is_write_vectored(&self) -> bool {
        true
    }

    /// Flushes any pending encrypted TLS bytes and flushes the underlying TCP socket.
    ///
    /// # Arguments
    ///
    /// - `Pin<&mut Self>` - The pinned transport.
    /// - `&mut task::Context<'_>` - The current task context.
    ///
    /// # Returns
    ///
    /// - `Poll<io::Result<()>>` - The flush result.
    fn poll_flush(self: Pin<&mut Self>, context: &mut task::Context<'_>) -> Poll<io::Result<()>> {
        let this: &mut Self = self.get_mut();
        match this.poll_flush_out(context) {
            Poll::Ready(Ok(())) => {
                let Self { tcp, .. } = this;
                Pin::new(&mut *tcp).poll_flush(context)
            }
            Poll::Ready(Err(error)) => Poll::Ready(Err(error)),
            Poll::Pending => Poll::Pending,
        }
    }

    /// Sends the TLS `close_notify` alert and shuts down the underlying TCP socket.
    ///
    /// # Arguments
    ///
    /// - `Pin<&mut Self>` - The pinned transport.
    /// - `&mut task::Context<'_>` - The current task context.
    ///
    /// # Returns
    ///
    /// - `Poll<io::Result<()>>` - The shutdown result.
    fn poll_shutdown(
        self: Pin<&mut Self>,
        context: &mut task::Context<'_>,
    ) -> Poll<io::Result<()>> {
        let this: &mut Self = self.get_mut();
        match this.poll_flush_out(context) {
            Poll::Ready(Ok(())) => {}
            Poll::Ready(Err(error)) => return Poll::Ready(Err(error)),
            Poll::Pending => return Poll::Pending,
        }
        let needs_close: bool = {
            let Self { write_closed, .. } = this;
            !*write_closed
        };
        if needs_close {
            match this.poll_encrypt_and_write(context, &[], true) {
                Poll::Ready(Ok(_)) => {}
                Poll::Ready(Err(error)) => return Poll::Ready(Err(error)),
                Poll::Pending => return Poll::Pending,
            }
        }
        match this.poll_flush_out(context) {
            Poll::Ready(Ok(())) => {
                let Self { tcp, .. } = this;
                Pin::new(&mut *tcp).poll_shutdown(context)
            }
            Poll::Ready(Err(error)) => Poll::Ready(Err(error)),
            Poll::Pending => Poll::Pending,
        }
    }
}