ntex-io 4.1.0

Utilities for abstracting io streams
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
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
use std::{cell::Cell, fmt, io, task::Poll};

use ntex_bytes::{BytePageSize, BytePages, BytesMut};

use crate::{IoConfig, IoRef};

pub(crate) struct Stack {
    buffers: Vec<Buffer>,
}

#[derive(Default)]
struct Buffer {
    read: Cell<Option<BytesMut>>,
    write: Cell<Option<BytePages>>,
}

impl fmt::Debug for Stack {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Stack")
            .field("len", &self.buffers.len())
            .finish()
    }
}

impl Stack {
    pub(crate) fn new(size: BytePageSize) -> Self {
        // room for two filter layers, e.g. TLS over a proxy protocol,
        // without reallocating
        let mut buffers = Vec::with_capacity(4);
        buffers.push(Buffer {
            read: Cell::new(None),
            write: Cell::new(Some(BytePages::new(size))),
        });
        // Only exposed as the inner side of `FilterBuf` when no layer
        // is installed; its write pages are allocated on demand.
        buffers.push(Buffer::default());
        Self { buffers }
    }

    pub(crate) fn set_page_size(&self, size: BytePageSize) {
        for b in &self.buffers {
            b.with_write_if_set(|b| b.set_page_size(size));
        }
    }

    pub(crate) fn add_layer(&mut self, page_size: BytePageSize) {
        self.buffers.insert(
            0,
            Buffer {
                read: Cell::new(None),
                write: Cell::new(Some(BytePages::new(page_size))),
            },
        );
    }

    fn with_last<F, R>(&self, f: F) -> R
    where
        F: FnOnce(&Buffer) -> R,
    {
        f(&self.buffers[self.buffers.len() - 2])
    }

    pub(crate) fn with_read_src<F, R>(&self, io: &IoRef, f: F) -> R
    where
        F: FnOnce(&mut BytesMut) -> R,
    {
        self.with_last(|buf| buf.with_read(io, f))
    }

    pub(crate) fn with_read_dst<F, R>(&self, io: &IoRef, f: F) -> R
    where
        F: FnOnce(&mut BytesMut) -> R,
    {
        self.buffers[0].with_read(io, f)
    }

    pub(crate) fn write_buf_size(&self) -> usize {
        // check size for first level because delayed filter processing
        if self.buffers.len() == 2 {
            self.buffers[0].write_len()
        } else {
            self.buffers[0].write_len() + self.buffers[self.buffers.len() - 2].write_len()
        }
    }

    /// Returns the size of the transport-facing write buffer.
    pub(crate) fn write_dst_size(&self) -> usize {
        self.with_last(Buffer::write_len)
    }

    pub(crate) fn with_write_src<F, R>(&self, f: F) -> R
    where
        F: FnOnce(&mut BytePages) -> R,
    {
        self.buffers[0].with_write(f)
    }

    pub(crate) fn with_write_dst<F, R>(&self, f: F) -> R
    where
        F: FnOnce(&mut BytePages) -> R,
    {
        self.buffers[self.buffers.len() - 2].with_write(f)
    }

    pub(crate) fn read_dst_size(&self) -> usize {
        self.buffers[0].read_len()
    }

    pub(crate) fn with_filter<F, R>(&self, io: &IoRef, f: F) -> R
    where
        F: FnOnce(&mut FilterCtx<'_>) -> R,
    {
        let mut ctx = FilterCtx {
            io,
            idx: 0,
            stack: self,
            st: FilterUpdates { wants_write: false },
        };
        f(&mut ctx)
    }

    pub(crate) fn get_read_buf(&self) -> Option<BytesMut> {
        self.with_last(|buffer| buffer.read.take())
    }

    pub(crate) fn set_read_buf(&self, buf: BytesMut, cfg: &IoConfig) {
        self.with_last(move |buffer| {
            if let Some(mut first_buf) = buffer.read.take() {
                // grow through the configured policy, so the merged buffer
                // stays cacheable when the data fits
                cfg.read_buf().resize_min(&mut first_buf, buf.len());
                first_buf.extend_from_slice(&buf);
                cfg.read_buf().release(buf);
                buffer.read.set(Some(first_buf));
            } else if !buf.is_empty() {
                buffer.read.set(Some(buf));
            } else {
                cfg.read_buf().release(buf);
            }
        });
    }

    pub(crate) fn process_read_buf(&self, io: &IoRef) -> io::Result<FilterUpdates> {
        let mut ctx = FilterCtx {
            io,
            idx: 0,
            stack: self,
            st: FilterUpdates { wants_write: false },
        };
        io.with_callbacks(|cb| cb.before_processing(io));
        let result = io.filter().process_read_buf(&mut ctx);
        io.with_callbacks(|cb| cb.after_processing(io));

        result.map(|()| ctx.st)
    }

    pub(crate) fn process_read_buf_no_cb(&self, io: &IoRef) -> io::Result<FilterUpdates> {
        let mut ctx = FilterCtx {
            io,
            idx: 0,
            stack: self,
            st: FilterUpdates { wants_write: false },
        };
        io.filter().process_read_buf(&mut ctx).map(|()| ctx.st)
    }

    pub(crate) fn process_write_buf(&self, io: &IoRef) -> io::Result<()> {
        if self.buffers[0].is_write_empty() {
            Ok(())
        } else {
            let mut ctx = FilterCtx {
                io,
                idx: 0,
                stack: self,
                st: FilterUpdates { wants_write: true },
            };
            io.with_callbacks(|cb| cb.before_processing(io));
            let res = io.filter().process_write_buf(&mut ctx);
            io.with_callbacks(|cb| cb.after_processing(io));

            res
        }
    }

    pub(crate) fn process_write_buf_no_cb(&self, io: &IoRef) -> io::Result<()> {
        if self.buffers[0].is_write_empty() {
            Ok(())
        } else {
            let mut ctx = FilterCtx {
                io,
                idx: 0,
                stack: self,
                st: FilterUpdates { wants_write: true },
            };
            io.filter().process_write_buf(&mut ctx)
        }
    }

    pub(crate) fn process_write_buf_force(&self, io: &IoRef) -> io::Result<()> {
        let mut ctx = FilterCtx {
            io,
            idx: 0,
            stack: self,
            st: FilterUpdates { wants_write: true },
        };
        io.with_callbacks(|cb| cb.before_processing(io));
        let res = io.filter().process_write_buf(&mut ctx);
        io.with_callbacks(|cb| cb.after_processing(io));

        res
    }

    pub(crate) fn process_shutdown(&self, io: &IoRef) -> io::Result<Poll<()>> {
        self.process_write_buf(io)?;
        io.with_callbacks(|cb| cb.before_processing(io));
        let res = self.with_filter(io, |ctx| io.filter().shutdown(ctx));
        io.with_callbacks(|cb| cb.after_processing(io));

        res
    }

    /// Releases the data of every buffer once nothing can consume it anymore.
    ///
    /// Read buffers go back to the cache and write pages are freed, the
    /// buffers themselves stay usable.
    pub(crate) fn release(&self, cfg: &IoConfig) {
        for b in &self.buffers {
            if let Some(buf) = b.read.take() {
                cfg.read_buf().release(buf);
            }
            b.with_write_if_set(BytePages::clear);
        }
    }
}

impl Buffer {
    fn with_write_if_set(&self, f: impl FnOnce(&mut BytePages)) {
        if let Some(mut wb) = self.write.take() {
            f(&mut wb);
            self.write.set(Some(wb));
        }
    }

    fn is_write_empty(&self) -> bool {
        self.with_write(|b| b.is_empty())
    }

    fn read_len(&self) -> usize {
        if let Some(rb) = self.read.take() {
            let l = rb.len();
            self.read.set(Some(rb));
            l
        } else {
            0
        }
    }

    fn write_len(&self) -> usize {
        self.with_write(|b| b.len())
    }

    fn with_read<F, R>(&self, io: &IoRef, f: F) -> R
    where
        F: FnOnce(&mut BytesMut) -> R,
    {
        let mut rb = self
            .read
            .take()
            .unwrap_or_else(|| io.cfg().read_buf().get());
        let result = f(&mut rb);

        // check nested updates
        if self.read.take().is_some() {
            log::error!("Nested read io operation is detected");
            io.terminate();
        }

        if rb.is_empty() {
            io.cfg().read_buf().release(rb);
        } else {
            self.read.set(Some(rb));
        }
        result
    }

    fn with_write<F, R>(&self, f: F) -> R
    where
        F: FnOnce(&mut BytePages) -> R,
    {
        let mut wb = self.write.take().unwrap();
        let result = f(&mut wb);
        self.write.set(Some(wb));
        result
    }
}

#[derive(Copy, Clone, Debug)]
pub(crate) struct FilterUpdates {
    pub(crate) wants_write: bool,
}

#[derive(Debug)]
/// Context used while traversing a complete filter chain.
///
/// A context tracks the current layer and the write activity accumulated while
/// traversing it. [`with_next`](Self::with_next) advances to the inner layer,
/// while [`with_buffer`](Self::with_buffer) exposes the buffers adjacent to the
/// current layer.
pub struct FilterCtx<'a> {
    io: &'a IoRef,
    idx: usize,
    stack: &'a Stack,
    st: FilterUpdates,
}

impl FilterCtx<'_> {
    #[inline]
    /// Gets a reference to the I/O object.
    pub fn io(&self) -> &IoRef {
        self.io
    }

    #[inline]
    /// Gets the I/O tag.
    pub fn tag(&self) -> &'static str {
        self.io.tag()
    }

    #[inline]
    /// Invokes `f` with the context advanced to the next inner filter.
    ///
    /// The previous layer is restored after `f` returns.
    pub fn with_next<F, R>(&mut self, f: F) -> R
    where
        F: FnOnce(&mut Self) -> R,
    {
        self.idx += 1;
        let res = f(self);
        self.idx -= 1;
        res
    }

    #[inline]
    /// Invokes `f` with the buffers adjacent to the current filter.
    pub fn with_buffer<F, R>(&mut self, f: F) -> R
    where
        F: FnOnce(&mut FilterBuf<'_>) -> R,
    {
        let mut buf = FilterBuf {
            io: self.io,
            curr: &self.stack.buffers[self.idx],
            next: &self.stack.buffers[self.idx + 1],
            wants_write: Cell::new(self.st.wants_write),
        };
        let result = f(&mut buf);
        if buf.wants_write.get() {
            self.st.wants_write = true;
        }
        result
    }

    #[inline]
    /// Returns the size of the application-facing read buffer.
    pub fn read_dst_size(&self) -> usize {
        self.stack.buffers[0].read_len()
    }

    #[inline]
    /// Returns the size of the transport-facing write buffer.
    pub fn write_dst_size(&mut self) -> usize {
        self.stack.buffers[self.stack.buffers.len() - 2].write_len()
    }

    pub(crate) fn clear_write_buf(&mut self) {
        self.stack.buffers[self.idx].with_write(BytePages::clear);
    }
}

#[derive(Debug)]
/// Buffers and connection state adjacent to one [`FilterLayer`](crate::FilterLayer).
///
/// For reads, the source is transport-facing and the destination is
/// application-facing. For writes, the source is application-facing and the
/// destination is transport-facing. Buffers are returned to the chain after
/// each closure completes; empty read buffers may be returned to the
/// configured cache.
pub struct FilterBuf<'a> {
    io: &'a IoRef,
    curr: &'a Buffer,
    next: &'a Buffer,
    wants_write: Cell<bool>,
}

impl FilterBuf<'_> {
    #[inline]
    /// Gets a reference to the I/O object.
    pub fn io(&self) -> &IoRef {
        self.io
    }

    #[inline]
    /// Gets the I/O tag.
    pub fn tag(&self) -> &'static str {
        self.io.tag()
    }

    /// Provides mutable access to the transport-facing read source.
    ///
    /// The source is optional because no bytes may currently be allocated for
    /// this edge of the filter chain. Leaving an empty buffer in the option
    /// returns it to the configured cache.
    pub fn with_read_src<F, R>(&self, f: F) -> R
    where
        F: FnOnce(&mut Option<BytesMut>) -> R,
    {
        let mut read_src = self.next.read.take();
        let result = f(&mut read_src);

        if let Some(b) = read_src {
            if b.is_empty() {
                self.io.cfg().read_buf().release(b);
            } else {
                self.next.read.set(Some(b));
            }
        }
        result
    }

    /// Provides the transport-facing read source and application-facing
    /// destination.
    ///
    /// Implementations normally consume bytes from `src` and append decoded or
    /// transformed bytes to `dst`. Unconsumed source bytes are retained for the
    /// next invocation.
    pub fn with_read_buffers<F, R>(&self, f: F) -> R
    where
        F: FnOnce(&mut Option<BytesMut>, &mut BytesMut) -> R,
    {
        let mut read_src = self.next.read.take();
        let mut read_dst = self
            .curr
            .read
            .take()
            .unwrap_or_else(|| self.io.cfg().read_buf().get());

        let result = f(&mut read_src, &mut read_dst);

        if let Some(b) = read_src {
            if b.is_empty() {
                self.io.cfg().read_buf().release(b);
            } else {
                self.next.read.set(Some(b));
            }
        }
        if read_dst.is_empty() {
            self.io.cfg().read_buf().release(read_dst);
        } else {
            self.curr.read.set(Some(read_dst));
        }

        result
    }

    #[inline]
    /// Provides the application-facing write source and transport-facing
    /// destination.
    ///
    /// Implementations normally consume bytes from `src` and append encoded or
    /// transformed bytes to `dst`. Appending destination bytes marks the write
    /// chain as needing transport progress.
    pub fn with_write_buffers<F, R>(&self, f: F) -> R
    where
        F: FnOnce(&mut BytePages, &mut BytePages) -> R,
    {
        let mut write_curr = self.curr.write.take().unwrap();
        let (mut write_next, on_demand) = match self.next.write.take() {
            Some(b) => (b, false),
            None => (BytePages::new(write_curr.page_size()), true),
        };
        let write_len = if self.wants_write.get() {
            0
        } else {
            write_next.len()
        };

        let result = f(&mut write_curr, &mut write_next);

        if !self.wants_write.get() && write_next.len() > write_len {
            self.wants_write.set(true);
        }

        // Without a filter layer there is no transport-facing write buffer, the
        // transport writes from the application-facing one. Output written
        // here is never delivered.
        let undelivered = on_demand && !write_next.is_empty();

        self.curr.write.set(Some(write_curr));
        if !on_demand || undelivered {
            self.next.write.set(Some(write_next));
        }
        debug_assert!(
            !undelivered,
            "{}: output written to the write destination of the innermost filter buffer is never sent",
            self.io.tag()
        );
        result
    }
}

impl fmt::Debug for Buffer {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let read = self.read.take();
        let write = self.write.take();

        let result = f
            .debug_struct("Buffer")
            .field("read", &read)
            .field("write", &write)
            .finish();
        self.read.set(read);
        self.write.set(write);
        result
    }
}

#[cfg(test)]
mod tests {
    use ntex_bytes::BufMut;

    use super::*;
    use crate::{Io, testing::IoTest};

    #[ntex::test]
    async fn stack_buffers() {
        let (_, server) = IoTest::create();
        let io = Io::from(server);
        let ioref = io.get_ref();

        let mut stack = Stack::new(BytePageSize::Size8);
        assert!(format!("{stack:?}").contains("len: 2"));
        assert_eq!(stack.read_dst_size(), 0);
        assert_eq!(stack.write_buf_size(), 0);

        // two layers fit without reallocating
        let ptr = stack.buffers.as_ptr();
        stack.add_layer(BytePageSize::Size16);
        stack.add_layer(BytePageSize::Size16);
        assert_eq!(stack.buffers.as_ptr(), ptr);
        stack.buffers.remove(0);
        assert_eq!(stack.buffers.len(), 3);

        stack.set_page_size(BytePageSize::Size32);
        let (inner, layers) = stack.buffers.split_last().unwrap();
        for buffer in layers {
            buffer.with_write(|buf| assert_eq!(buf.page_size(), BytePageSize::Size32));
        }
        assert!(inner.write.take().is_none());

        stack.set_read_buf(BytesMut::from(&b"one"[..]), ioref.cfg());
        stack.set_read_buf(BytesMut::from(&b"-two"[..]), ioref.cfg());
        assert_eq!(stack.get_read_buf().as_deref(), Some(b"one-two".as_ref()));
        assert!(stack.get_read_buf().is_none());

        stack.set_read_buf(BytesMut::new(), ioref.cfg());
        assert!(stack.get_read_buf().is_none());
    }

    #[ntex::test]
    async fn set_read_buf_merges_into_cacheable_buffer() {
        let (_, server) = IoTest::create();
        let io = Io::from(server);
        let ioref = io.get_ref();
        let cfg = ioref.cfg().read_buf();
        let stack = Stack::new(BytePageSize::Size8);

        // unconsumed input, most of the buffer is taken by a decoded frame
        // that is still alive
        let mut first = cfg.get();
        first.extend_from_slice(&vec![1; cfg.high - 100]);
        let frame = first.split_to(cfg.high - 1100);
        stack.set_read_buf(first, ioref.cfg());

        // a read into a buffer of its own completes
        let mut second = cfg.get();
        second.extend_from_slice(&[2; 4000]);
        stack.set_read_buf(second, ioref.cfg());

        let merged = stack.get_read_buf().unwrap();
        assert_eq!(merged.len(), 5000);
        assert_eq!(&merged[..1000], &[1; 1000][..]);
        assert_eq!(&merged[1000..], &[2; 4000][..]);
        assert_eq!(merged.capacity(), cfg.high);
        assert_eq!(frame.len(), cfg.high - 1100);
    }

    #[ntex::test]
    async fn filter_read_buffers() {
        let (_, server) = IoTest::create();
        let io = Io::from(server);
        let ioref = io.get_ref();
        let mut stack = Stack::new(BytePageSize::Size8);
        stack.add_layer(BytePageSize::Size8);
        stack.set_read_buf(BytesMut::from(&b"input"[..]), ioref.cfg());

        stack.with_filter(&ioref, |ctx| {
            assert_eq!(ctx.io(), &ioref);
            assert_eq!(ctx.tag(), ioref.tag());
            assert_eq!(ctx.read_dst_size(), 0);

            ctx.with_buffer(|buf| {
                assert_eq!(buf.io(), &ioref);
                assert_eq!(buf.tag(), ioref.tag());
                buf.with_read_buffers(|src, dst| {
                    let src = src.as_mut().unwrap();
                    dst.extend_from_slice(&src.split_to(2));
                });
            });
        });

        assert_eq!(stack.read_dst_size(), 2);
        stack.with_read_dst(&ioref, |buf| assert_eq!(&buf[..], b"in"));
        assert_eq!(stack.get_read_buf().as_deref(), Some(b"put".as_ref()));

        stack.with_filter(&ioref, |ctx| {
            ctx.with_buffer(|buf| {
                buf.with_read_src(|src| {
                    *src = Some(BytesMut::from(&b"next"[..]));
                });
            });
        });
        assert_eq!(stack.get_read_buf().as_deref(), Some(b"next".as_ref()));
    }

    #[ntex::test]
    async fn innermost_write_buffer_is_on_demand() {
        let (_, server) = IoTest::create();
        let io = Io::from(server);
        let ioref = io.get_ref();
        let stack = Stack::new(BytePageSize::Size8);
        let inner = &stack.buffers[1];
        assert!(inner.write.take().is_none());
        stack.release(ioref.cfg());

        // a filter without layers sees an empty inner buffer, which is not
        // kept unless something is written to it
        stack.with_write_src(|buf| buf.put_slice(b"out"));
        stack.with_filter(&ioref, |ctx| {
            ctx.with_buffer(|buf| {
                buf.with_write_buffers(|src, dst| {
                    assert_eq!(src.len(), 3);
                    assert!(dst.is_empty());
                });
            });
        });
        assert!(inner.write.take().is_none());
    }

    #[cfg(debug_assertions)]
    #[ntex::test]
    #[should_panic(expected = "is never sent")]
    async fn innermost_write_destination_output_asserts() {
        let (_, server) = IoTest::create();
        let io = Io::from(server);
        let ioref = io.get_ref();
        let stack = Stack::new(BytePageSize::Size8);

        stack.with_write_src(|buf| buf.put_slice(b"out"));
        stack.with_filter(&ioref, |ctx| {
            ctx.with_buffer(|buf| buf.with_write_buffers(BytePages::move_to));
        });
    }

    #[ntex::test]
    async fn filter_write_buffers_and_updates() {
        let (_, server) = IoTest::create();
        let io = Io::from(server);
        let ioref = io.get_ref();
        let mut stack = Stack::new(BytePageSize::Size8);
        stack.add_layer(BytePageSize::Size8);
        stack.with_write_src(|buf| buf.put_slice(b"output"));

        let updates = stack.with_filter(&ioref, |ctx| {
            assert_eq!(ctx.write_dst_size(), 0);
            ctx.with_buffer(|buf| {
                buf.with_write_buffers(|src, dst| {
                    assert_eq!(src.len(), 6);
                    assert_eq!(dst.len(), 0);
                    src.move_to(dst);
                });
            });
            ctx.st
        });

        assert!(updates.wants_write);
        assert_eq!(stack.write_buf_size(), 6);
        assert_eq!(
            stack.with_write_dst(|buf| buf.split_to(6).freeze()),
            b"output".as_ref()
        );
        assert_eq!(stack.write_buf_size(), 0);
    }

    #[ntex::test]
    async fn buffer_debug_preserves_contents() {
        let (_, server) = IoTest::create();
        let io = Io::from(server);
        let ioref = io.get_ref();
        let buffer = Buffer {
            read: Cell::new(None),
            write: Cell::new(Some(BytePages::new(BytePageSize::Size8))),
        };

        buffer.with_read(&ioref, |buf| buf.extend_from_slice(b"read"));
        buffer.with_write(|buf| buf.put_slice(b"write"));

        let debug = format!("{buffer:?}");
        assert!(debug.contains("Buffer"));
        assert_eq!(buffer.read_len(), 4);
        assert_eq!(buffer.write_len(), 5);
    }
}