s3s-multipart 0.17.0

Async streaming parser for multipart/form-data
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
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2023-2026 The s3s Authors

//! Strict closing validation for a taken part stream.
//!
//! [`FinalPartDataStream`] continues to yield the part's data — including
//! data that was not yet read when [`PartDataStream::into_final`](crate::PartDataStream::into_final) was called
//! — and then validates the strict closing trailer: the `--` suffix of the
//! closing delimiter, optional transport padding, `\r\n`, and end of stream.
//!
//! An epilogue or another part is rejected (`Error::StreamPartNotLast`)
//! because callers derive the exact file length from the multipart byte
//! accounting: trailing content of unknown size would break that accounting.
//! Note that RFC 2046 allows an epilogue; this crate deliberately rejects
//! it.

use std::fmt;
use std::pin::Pin;
use std::task::{Context, Poll, ready};

use bytes::Bytes;
use futures_core::Stream;
use memchr::memmem;

use crate::Error;
use crate::buffer::StreamBuffer;
use crate::delimiter::{DataSearch, search_data};

/// The strict variant of a taken part stream.
///
/// Created with [`PartDataStream::into_final`](crate::PartDataStream::into_final), it yields the remaining data
/// chunks of the part — each one non-empty — and then validates the strict
/// closing trailer: `\r\n--boundary--\r\n` followed by end of stream, with
/// transport padding (SP / HTAB) allowed between the closing `--` and the
/// final CRLF, as RFC 2046 section 5.1.1 allows for a boundary line. An
/// epilogue or another part is rejected because callers derive the exact
/// content length from the multipart byte accounting.
pub struct FinalPartDataStream<S>
where
    S: Stream<Item = Result<Bytes, Error>> + Send + Unpin,
{
    buffer: StreamBuffer<S>,
    delimiter_finder: Box<memmem::Finder<'static>>,
    state: DataState,
    multipart_consumed: u64,
    /// The taken stream ended abnormally — with an error, or at end of stream
    /// before the delimiter — so the first poll reports an incomplete part
    /// instead of validating a trailer that was never reached.
    aborted: bool,
    terminated: bool,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum DataState {
    /// The closing delimiter has not been reached yet; data chunks are
    /// yielded as they are located.
    Data,
    /// The delimiter was consumed; the `--` suffix of the closing delimiter
    /// is expected next.
    AfterBoundary,
    /// The `--` suffix was consumed; optional transport padding and the
    /// final `\r\n` are expected.
    FinalCRLF,
    /// The closing trailer is complete; only end of stream may follow.
    Eof,
    Done,
}

impl<S> FinalPartDataStream<S>
where
    S: Stream<Item = Result<Bytes, Error>> + Send + Unpin,
{
    /// Constructs the final stream from the state of a taken part stream.
    ///
    /// `data_done` indicates whether the closing delimiter was already
    /// consumed (and the post-delimiter bytes are retained in `buffer`);
    /// `aborted` indicates that the taken stream ended abnormally, so this
    /// stream has to report the incomplete part. The two cannot both be true:
    /// reaching the delimiter ends the taken stream's data phase successfully.
    pub(super) fn new(
        buffer: StreamBuffer<S>,
        delimiter_finder: Box<memmem::Finder<'static>>,
        multipart_consumed: u64,
        data_done: bool,
        aborted: bool,
    ) -> Self {
        Self {
            buffer,
            delimiter_finder,
            state: if data_done {
                DataState::AfterBoundary
            } else {
                DataState::Data
            },
            multipart_consumed,
            aborted,
            terminated: false,
        }
    }

    /// Returns the total number of multipart bytes consumed at the moment
    /// the stream was taken, excluding bytes retained in the internal
    /// buffer.
    ///
    /// This is the value to subtract from the request `Content-Length` when
    /// deriving the exact file length. It does not count bytes yielded by
    /// this stream itself.
    #[must_use]
    pub fn multipart_consumed(&self) -> u64 {
        self.multipart_consumed
    }

    fn poll_data(&mut self, cx: &mut Context<'_>) -> Poll<Result<Option<Bytes>, Error>> {
        if self.aborted {
            // The taken stream failed before reaching the delimiter, so there is
            // no trailer to validate: report the incomplete part instead of
            // reading on, which could otherwise turn the failure into a success.
            self.terminated = true;
            return Poll::Ready(Err(Error::IncompleteStreamPart));
        }

        let delimiter_finder = &self.delimiter_finder;
        let delimiter_len = delimiter_finder.needle().len();

        loop {
            if !self.buffer.buf.is_empty() {
                match search_data(&self.buffer.buf, delimiter_finder) {
                    DataSearch::Found { index } => {
                        let data = self.buffer.buf.split_to(index).freeze();
                        let _ = self.buffer.buf.split_to(delimiter_len);
                        self.state = DataState::AfterBoundary;
                        return Poll::Ready(Ok((!data.is_empty()).then_some(data)));
                    }
                    DataSearch::Emit { end } => {
                        return Poll::Ready(Ok(Some(self.buffer.buf.split_to(end).freeze())));
                    }
                    DataSearch::KeepAll => {}
                }
            }

            match ready!(self.buffer.poll_stream(cx)) {
                Some(Ok(chunk)) => {
                    if !self.buffer.buf.is_empty() {
                        self.buffer.buf.extend_from_slice(&chunk);
                        continue;
                    }

                    match search_data(&chunk, delimiter_finder) {
                        DataSearch::Found { index } => {
                            let data = chunk.slice(..index);
                            self.buffer.buf.clear();
                            self.buffer
                                .buf
                                .extend_from_slice(&chunk[index.saturating_add(delimiter_len)..]);
                            self.state = DataState::AfterBoundary;
                            return Poll::Ready(Ok((!data.is_empty()).then_some(data)));
                        }
                        DataSearch::Emit { end } => {
                            let data = chunk.slice(..end);
                            self.buffer.buf.extend_from_slice(&chunk[end..]);
                            return Poll::Ready(Ok(Some(data)));
                        }
                        DataSearch::KeepAll => {
                            self.buffer.buf.extend_from_slice(&chunk);
                        }
                    }
                }
                Some(Err(err)) => return Poll::Ready(Err(err)),
                None => return Poll::Ready(Err(Error::IncompleteStreamPart)),
            }
        }
    }

    fn poll_trailer(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Error>> {
        loop {
            match self.state {
                DataState::AfterBoundary => {
                    ready!(self.fill_buf(2, cx))?;
                    if !self.buffer.buf.starts_with(b"--") {
                        let err = if self.buffer.buf.starts_with(b"\r")
                            || self.buffer.buf.first().is_some_and(|b| matches!(b, b' ' | b'\t'))
                        {
                            Error::StreamPartNotLast
                        } else {
                            Error::InvalidFormat
                        };
                        return Poll::Ready(Err(err));
                    }
                    let _ = self.buffer.buf.split_to(2);
                    self.state = DataState::FinalCRLF;
                }
                DataState::FinalCRLF => {
                    loop {
                        ready!(self.fill_buf(1, cx))?;

                        let first = self.buffer.buf[0];
                        match first {
                            b' ' | b'\t' => {
                                let _ = self.buffer.buf.split_to(1);
                            }
                            b'\r' => break,
                            _ => return Poll::Ready(Err(Error::InvalidFormat)),
                        }
                    }

                    ready!(self.fill_buf(2, cx))?;
                    if !self.buffer.buf.starts_with(b"\r\n") {
                        return Poll::Ready(Err(Error::InvalidFormat));
                    }
                    let _ = self.buffer.buf.split_to(2);
                    self.state = DataState::Eof;
                }
                DataState::Eof => {
                    if !self.buffer.buf.is_empty() {
                        return Poll::Ready(Err(Error::StreamPartNotLast));
                    }

                    match ready!(self.buffer.poll_stream(cx)) {
                        None => {
                            self.state = DataState::Done;
                            return Poll::Ready(Ok(()));
                        }
                        // An empty chunk is not trailing content either, and
                        // `poll_stream` never hands one out.
                        Some(Ok(_)) => return Poll::Ready(Err(Error::StreamPartNotLast)),
                        Some(Err(err)) => return Poll::Ready(Err(err)),
                    }
                }
                // `poll_trailer` is only reached in the three trailer states, so
                // this arm guards the match rather than describing a reachable
                // outcome.
                DataState::Data | DataState::Done => return Poll::Ready(Ok(())),
            }
        }
    }

    fn fill_buf(&mut self, len: usize, cx: &mut Context<'_>) -> Poll<Result<(), Error>> {
        while self.buffer.buf.len() < len {
            match ready!(self.buffer.poll_stream(cx)) {
                Some(Ok(chunk)) => self.buffer.buf.extend_from_slice(&chunk),
                Some(Err(err)) => return Poll::Ready(Err(err)),
                None => return Poll::Ready(Err(Error::IncompleteStreamPart)),
            }
        }
        Poll::Ready(Ok(()))
    }
}

impl<S> fmt::Debug for FinalPartDataStream<S>
where
    S: Stream<Item = Result<Bytes, Error>> + Send + Unpin,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("FinalPartDataStream")
            .field("state", &self.state)
            .field("multipart_consumed", &self.multipart_consumed)
            .finish_non_exhaustive()
    }
}

impl<S> Stream for FinalPartDataStream<S>
where
    S: Stream<Item = Result<Bytes, Error>> + Send + Unpin,
{
    type Item = Result<Bytes, Error>;

    fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        let this = self.get_mut();
        loop {
            if this.terminated {
                return Poll::Ready(None);
            }

            match this.state {
                DataState::Data => match ready!(this.poll_data(cx)) {
                    Ok(Some(item)) => return Poll::Ready(Some(Ok(item))),
                    // No data in this step: the delimiter was already reached, so
                    // go on and validate the trailer in the same poll instead of
                    // handing the caller an empty chunk.
                    Ok(None) => {}
                    Err(err) => {
                        this.terminated = true;
                        return Poll::Ready(Some(Err(err)));
                    }
                },
                DataState::AfterBoundary | DataState::FinalCRLF | DataState::Eof => {
                    if let Err(err) = ready!(this.poll_trailer(cx)) {
                        this.terminated = true;
                        return Poll::Ready(Some(Err(err)));
                    }
                }
                DataState::Done => return Poll::Ready(None),
            }
        }
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        (0, None)
    }
}

#[cfg(test)]
#[allow(
    clippy::expect_used,
    clippy::indexing_slicing,
    clippy::panic,
    clippy::unreachable,
    clippy::unwrap_used
)]
mod tests {
    use super::*;

    use std::pin::Pin as StdPin;
    use std::task::{Context as TaskContext, Poll as TaskPoll};

    use futures_util::StreamExt;
    use futures_util::stream;
    use futures_util::task::noop_waker;

    use crate::delimiter::make_delimiter_finder;
    use crate::part_data_stream::PartDataStream;

    struct PendingStream;

    impl Stream for PendingStream {
        type Item = Result<Bytes, Error>;

        fn poll_next(self: StdPin<&mut Self>, _cx: &mut TaskContext<'_>) -> TaskPoll<Option<Self::Item>> {
            TaskPoll::Pending
        }
    }

    fn with_cx<R>(f: impl FnOnce(&mut TaskContext<'_>) -> R) -> R {
        let waker = noop_waker();
        let mut cx = TaskContext::from_waker(&waker);
        f(&mut cx)
    }

    fn final_with_buffer(
        items: Vec<Result<Bytes, Error>>,
        state: DataState,
    ) -> FinalPartDataStream<impl Stream<Item = Result<Bytes, Error>> + Send + Sync> {
        FinalPartDataStream {
            buffer: StreamBuffer::new(stream::iter(items)),
            delimiter_finder: make_delimiter_finder(b"boundary"),
            state,
            multipart_consumed: 0,
            aborted: false,
            terminated: false,
        }
    }

    fn final_with_error(
        err: Error,
        state: DataState,
    ) -> FinalPartDataStream<impl Stream<Item = Result<Bytes, Error>> + Send + Sync> {
        final_with_buffer(vec![Err(err)], state)
    }

    fn final_with_prefix(
        prefix: &[u8],
        state: DataState,
    ) -> FinalPartDataStream<impl Stream<Item = Result<Bytes, Error>> + Send + Sync> {
        let mut buffer = StreamBuffer::new(PendingStream);
        buffer.buf.extend_from_slice(prefix);
        FinalPartDataStream {
            buffer,
            delimiter_finder: make_delimiter_finder(b"boundary"),
            state,
            multipart_consumed: 0,
            aborted: false,
            terminated: false,
        }
    }

    #[test]
    fn part_data_stream_pending_polls() {
        with_cx(|cx| {
            let buffer = StreamBuffer::new(PendingStream);
            let mut stream = FinalPartDataStream {
                buffer,
                delimiter_finder: make_delimiter_finder(b"boundary"),
                state: DataState::Data,
                multipart_consumed: 0,
                aborted: false,
                terminated: false,
            };
            assert!(matches!(stream.poll_next_unpin(cx), TaskPoll::Pending));
        });
    }

    #[test]
    fn poll_data_buffered_empty_and_prefix_paths() {
        with_cx(|cx| {
            let mut ds = final_with_buffer(Vec::new(), DataState::Data);
            ds.buffer.buf.extend_from_slice(b"\r\n--boundary--\r\n");
            // No data item; the trailer is validated in the same poll.
            assert!(matches!(ds.poll_next_unpin(cx), TaskPoll::Ready(None)));

            let mut ds = final_with_buffer(Vec::new(), DataState::Data);
            ds.buffer.buf.extend_from_slice(b"0123456789abcdefghij");
            assert!(matches!(ds.poll_next_unpin(cx), TaskPoll::Ready(Some(Ok(_)))));

            let mut ds = final_with_buffer(vec![Ok(Bytes::from_static(b"tiny"))], DataState::Data);
            let poll = ds.poll_next_unpin(cx);
            assert!(matches!(poll, TaskPoll::Ready(Some(Err(Error::IncompleteStreamPart)))));
        });
    }

    #[test]
    fn fill_buf_error_and_pending_paths() {
        with_cx(|cx| {
            let mut ds = final_with_error(Error::InvalidFormat, DataState::AfterBoundary);
            assert!(matches!(ds.poll_next_unpin(cx), TaskPoll::Ready(Some(Err(Error::InvalidFormat)))));

            let mut ds = final_with_buffer(Vec::new(), DataState::AfterBoundary);
            ds.buffer.buf.extend_from_slice(b"xx");
            assert!(matches!(ds.poll_next_unpin(cx), TaskPoll::Ready(Some(Err(Error::InvalidFormat)))));

            let mut ds = final_with_buffer(vec![Ok(Bytes::from_static(b"--\r\n"))], DataState::AfterBoundary);
            assert!(matches!(ds.poll_next_unpin(cx), TaskPoll::Ready(None)));
        });
    }

    #[test]
    fn trailer_padding_error_and_pending_paths() {
        with_cx(|cx| {
            let mut ds = final_with_error(Error::InvalidFormat, DataState::FinalCRLF);
            ds.buffer.buf.extend_from_slice(b" ");
            assert!(matches!(ds.poll_next_unpin(cx), TaskPoll::Ready(Some(Err(Error::InvalidFormat)))));

            let mut ds = final_with_prefix(b" ", DataState::FinalCRLF);
            assert!(matches!(ds.poll_next_unpin(cx), TaskPoll::Pending));

            let mut ds = final_with_buffer(Vec::new(), DataState::FinalCRLF);
            ds.buffer.buf.extend_from_slice(b" ");
            assert!(matches!(ds.poll_next_unpin(cx), TaskPoll::Ready(Some(Err(Error::IncompleteStreamPart)))));

            let mut ds = final_with_error(Error::InvalidFormat, DataState::FinalCRLF);
            ds.buffer.buf.extend_from_slice(b"\r");
            assert!(matches!(ds.poll_next_unpin(cx), TaskPoll::Ready(Some(Err(Error::InvalidFormat)))));

            let mut ds = final_with_prefix(b"\r", DataState::FinalCRLF);
            assert!(matches!(ds.poll_next_unpin(cx), TaskPoll::Pending));

            let mut ds = final_with_buffer(Vec::new(), DataState::FinalCRLF);
            ds.buffer.buf.extend_from_slice(b"xx");
            assert!(matches!(ds.poll_next_unpin(cx), TaskPoll::Ready(Some(Err(Error::InvalidFormat)))));
        });
    }

    #[test]
    fn trailer_final_crlf_requires_exact_bytes() {
        with_cx(|cx| {
            let mut ds = final_with_buffer(Vec::new(), DataState::FinalCRLF);
            ds.buffer.buf.extend_from_slice(b"\rX");
            assert!(matches!(ds.poll_next_unpin(cx), TaskPoll::Ready(Some(Err(Error::InvalidFormat)))));
        });
    }

    #[test]
    fn terminated_stream_returns_none_on_second_poll() {
        with_cx(|cx| {
            let ds = final_with_error(Error::InvalidFormat, DataState::Data);
            let mut ds = Box::pin(ds);
            assert!(matches!(
                ds.as_mut().poll_next_unpin(cx),
                TaskPoll::Ready(Some(Err(Error::InvalidFormat)))
            ));
            assert!(matches!(ds.as_mut().poll_next_unpin(cx), TaskPoll::Ready(None)));
        });
    }

    #[test]
    fn yields_remaining_data_then_rejects_another_part() {
        with_cx(|cx| {
            // into_final was called before the delimiter was reached: the
            // remaining data is yielded, then the strict ending rejects the
            // part that follows.
            let mut ds = final_with_buffer(
                vec![Ok(Bytes::from_static(
                    b"hello\r\n--boundary\r\nX: y\r\n\r\nworld\r\n--boundary--\r\n",
                ))],
                DataState::Data,
            );
            assert!(matches!(
                ds.poll_next_unpin(cx),
                TaskPoll::Ready(Some(Ok(bytes))) if bytes.as_ref() == b"hello"
            ));
            assert!(matches!(ds.poll_next_unpin(cx), TaskPoll::Ready(Some(Err(Error::StreamPartNotLast)))));
            assert!(matches!(ds.poll_next_unpin(cx), TaskPoll::Ready(None)));
        });
    }

    /// A stream may hand out chunks that carry no bytes. They are not content, so
    /// the strict ending must poll past them instead of rejecting the part.
    #[test]
    fn empty_chunks_after_the_trailer_are_not_an_epilogue() {
        with_cx(|cx| {
            let mut ds = final_with_buffer(
                vec![
                    Ok(Bytes::from_static(b"")),
                    Ok(Bytes::from_static(b"")),
                    Ok(Bytes::from_static(b"")),
                ],
                DataState::Eof,
            );
            assert!(matches!(ds.poll_next_unpin(cx), TaskPoll::Ready(None)));
        });
    }

    /// The observable accessors of the strict stream: the size hint (the length
    /// cannot be known, so `(0, None)` is the honest answer), the `Debug`
    /// rendering — `boundary.rs` also pins the rendering of its own type — and
    /// the consumed offset, which `into_final` has to carry over because
    /// callers derive the exact file length from it.
    #[test]
    fn accessors_and_debug_are_stable() {
        let ds = final_with_buffer(Vec::new(), DataState::Data);
        assert_eq!(ds.size_hint(), (0, None));
        assert_eq!(ds.multipart_consumed(), 0);

        let rendered = format!("{ds:?}");
        assert!(rendered.starts_with("FinalPartDataStream"), "{rendered}");
        assert!(rendered.contains("state"), "{rendered}");
        assert!(rendered.contains("multipart_consumed"), "{rendered}");

        // A taken stream that had consumed 42 multipart bytes keeps reporting
        // that offset after the handover.
        let stream = PartDataStream::new(
            StreamBuffer::new(stream::iter(Vec::<Result<Bytes, Error>>::new())),
            make_delimiter_finder(b"boundary"),
            42,
        );
        assert_eq!(stream.multipart_consumed(), 42);
        assert_eq!(stream.into_final().multipart_consumed(), 42);
    }

    /// A taken stream that failed before the delimiter must not be able to turn
    /// into a successful strict stream: the handover keeps the failure and
    /// reports the incomplete part instead of validating a trailer that was
    /// never reached.
    #[test]
    fn a_failed_taken_stream_stays_failed() {
        with_cx(|cx| {
            let mut stream = PartDataStream::new(
                StreamBuffer::new(stream::iter(vec![
                    Ok(Bytes::from_static(b"0123456789abcdefghij")),
                    Err(Error::InvalidFormat),
                    Ok(Bytes::from_static(b"world\r\n--boundary--\r\n")),
                ])),
                make_delimiter_finder(b"boundary"),
                5,
            );
            assert!(matches!(
                stream.poll_next_unpin(cx),
                TaskPoll::Ready(Some(Ok(bytes))) if bytes.as_ref() == b"012345678"
            ));
            assert!(matches!(stream.poll_next_unpin(cx), TaskPoll::Ready(Some(Err(Error::InvalidFormat)))));

            let mut ds = stream.into_final();
            assert!(matches!(ds.poll_next_unpin(cx), TaskPoll::Ready(Some(Err(Error::IncompleteStreamPart)))));
            assert!(matches!(ds.poll_next_unpin(cx), TaskPoll::Ready(None)));
        });
    }

    /// The same handover when the taken stream ended at end of stream instead of
    /// erroring: still no trailer to validate.
    #[test]
    fn a_truncated_taken_stream_stays_failed() {
        with_cx(|cx| {
            let mut stream = PartDataStream::new(
                StreamBuffer::new(stream::iter(vec![Ok(Bytes::from_static(b"0123456789abcdefghij"))])),
                make_delimiter_finder(b"boundary"),
                0,
            );
            assert!(matches!(stream.poll_next_unpin(cx), TaskPoll::Ready(Some(Ok(_)))));
            assert!(matches!(
                stream.poll_next_unpin(cx),
                TaskPoll::Ready(Some(Err(Error::IncompleteStreamPart)))
            ));

            let mut ds = stream.into_final();
            assert!(matches!(ds.poll_next_unpin(cx), TaskPoll::Ready(Some(Err(Error::IncompleteStreamPart)))));
            assert!(matches!(ds.poll_next_unpin(cx), TaskPoll::Ready(None)));
        });
    }
}