bstack_raii 0.1.0

Typed, RAII-style ownership and lifetime layer over the bstack allocation primitives
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
722
723
724
725
726
//! [`BStackDeque<T>`]: an owned double-ended queue over a contiguous ring.
//!
//! The on-disk answer to [`std::collections::VecDeque`], and the container most
//! callers reaching for [`crate::BStackLinkedList`] actually want. Its elements'
//! references live in **one contiguous ring block** — `[u64; cap]` slots indexed
//! circularly — so traversing the structure is a single sequential scan rather
//! than a pointer chase per element (each *value* still lives in its own block, so
//! resolving a value seeks once, but finding the next element does not).
//!
//! Push/pop at **both** ends are O(1) amortized. The ring's `head`/`len`/`cap`
//! and its data pointer live in the fixed handle block, so the handle never
//! moves; growth reallocates only the ring.
//!
//! # Single-ref slots, non-generic ring
//!
//! Like [`crate::BStackLinkedList`], each slot is a **single `u64` reference** to
//! an ordinary `T` block the deque owns — not `T` inlined. So the ring's on-disk
//! shape is the same for every `T` (a plain `u64` array), the handle layout
//! [`DequeOnDisk`] is non-generic, and only the tag varies by element type.
//!
//! # Atomicity
//!
//! Every push/pop is atomic per call and external-lock-free on the fast path: the
//! `head`/`len`/`cap`/`data` metadata and the target slot are read *and* written
//! inside one [`bstack::BStack::inplace_gen`] run (see
//! [`atomic_update`](super::util::atomic_update)), so a concurrent writer never
//! observes a torn ring and a crash never corrupts it. **Growth** is also atomic
//! and consistent: the new ring is allocated first (an orphan), then one
//! `inplace_gen` snapshots every live element, copies it into the new ring, and
//! swaps the descriptor — all under bstack's write lock, so it composes correctly
//! with concurrent pushes (a push that finds the ring full simply grows and
//! retries). A crash mid-growth leaks a ring, never tears the deque.

use core::cell::Cell;
use core::marker::PhantomData;
use core::mem::size_of;
use std::io;

use crate::BStackRaiiAllocator;
use bstack::{BStack, BStackRange};
use bytemuck::{Pod, Zeroable};

use super::util::{WriteBuf, alloc_image, atomic_update, read_fields, w8};
use crate::handback::ReplaceError;
use crate::io_core::{ClonePlan, TryCloneIn, dealloc_range};
use crate::primitives::{EightCC, checked_off_mul};
use crate::types::compiled::{BStackOwned, BlockHeader, HEADER_SIZE};
use crate::types::traits::{BStackBlock, BStackCast, BStackDrop};
use crate::util::{SmallBuf, io_error, read_u64};

/// The on-disk image of a [`BStackDeque`]: the block header, a pointer to the
/// ring data block (`0` = none), its capacity in slots, and the circular
/// `head`/`len`. `#[repr(C)]` with only `u64` fields after the header, so it is
/// padding-free and **non-generic** — the same layout for every element type.
#[repr(C)]
#[derive(Clone, Copy, Pod, Zeroable)]
pub struct DequeOnDisk {
    /// The 16-byte block header (size + type tag).
    pub header: BlockHeader,
    /// Offset of the ring data block (`[u64; cap]`), or `0` when unallocated.
    pub data: u64,
    /// Number of slots in the ring.
    pub cap: u64,
    /// Index (into the ring) of the front element.
    pub head: u64,
    /// Number of elements currently held.
    pub len: u64,
}

// Field offsets within the handle block.
const DATA_OFF: u64 = HEADER_SIZE; // 16
const CAP_OFF: u64 = HEADER_SIZE + 8; // 24
const HEAD_OFF: u64 = HEADER_SIZE + 16; // 32
const LEN_OFF: u64 = HEADER_SIZE + 24; // 40

const DEQUE_SIZE: u64 = size_of::<DequeOnDisk>() as u64;
/// The capacity a freshly grown empty ring starts at.
const MIN_CAP: u64 = 4;

/// An owned double-ended queue of `T` blocks.
///
/// A typed handle (a newtype over a [`BStackRange`]); [`new`](Self::new) returns
/// a bare [`BStackOwned<BStackDeque<T>>`] that frees nothing on scope exit — free
/// it with [`bstack_drop`](BStackDrop::bstack_drop) or wrap it
/// (`AutoDrop` / [`crate::BStackCow`]).
///
/// The deque owns its elements' blocks: pushing takes a [`BStackOwned<T>`],
/// popping hands one back, and teardown recursively frees every element and the
/// ring.
pub struct BStackDeque<T: BStackBlock> {
    range: BStackRange,
    _marker: PhantomData<fn() -> T>,
}

impl<T: BStackBlock> BStackDeque<T> {
    fn value_size() -> u64 {
        size_of::<<T as BStackBlock>::OnDisk>() as u64
    }

    /// A `T`-value handle over the block at `off` (fixed-size-block model).
    fn value_at(off: u64) -> T {
        unsafe { <T as BStackBlock>::from_range(BStackRange::new(off, Self::value_size())) }
    }

    /// Read the four `(head, len, cap, data)` metadata fields of the handle at
    /// `handle` in a single I/O (the on-disk order is `data, cap, head, len`).
    fn read_meta(stack: &BStack, handle: u64) -> io::Result<(u64, u64, u64, u64)> {
        let [data, cap, head, len] = read_fields::<4>(stack, handle + DATA_OFF)?;
        Ok((head, len, cap, data))
    }

    /// The ring index for logical position `head + x` (mod `cap`), rejecting
    /// `cap == 0` — a corrupted on-disk capacity, otherwise a `%` panic
    /// unconditionally reachable from a pure read (`front`/`back`/`to_vec`/
    /// iteration) regardless of build mode. `head + x` wrapping before the
    /// reduction is intentional: only the reduced, `cap`-bounded index is ever
    /// used for an address.
    fn ring_index(head: u64, x: u64, cap: u64) -> io::Result<u64> {
        if cap == 0 {
            return Err(io_error!(
                "corrupt deque: zero capacity with a nonzero length"
            ));
        }
        Ok(head.wrapping_add(x) % cap)
    }

    /// The absolute ring-slot address for a (already `cap`-bounded) index,
    /// rejecting overflow — `data` can originate from a corrupted on-disk
    /// pointer.
    fn slot_addr(data: u64, idx: u64) -> io::Result<u64> {
        checked_off_mul(data, idx, 8)
    }

    /// Allocate an empty deque (no ring is allocated until the first push).
    pub fn new<A: BStackRaiiAllocator>(allocator: &A) -> io::Result<BStackOwned<Self>> {
        Self::with_image(allocator, 0, 0)
    }

    /// Allocate an empty deque with room for `cap` elements pre-reserved (so the
    /// first `cap` pushes never grow). `cap == 0` behaves like [`new`](Self::new).
    pub fn with_capacity<A: BStackRaiiAllocator>(
        allocator: &A,
        cap: u64,
    ) -> io::Result<BStackOwned<Self>> {
        if cap == 0 {
            return Self::new(allocator);
        }
        // Allocate the ring first (an orphan); its slots are empty (len == 0), so
        // their contents are never read before being written.
        let size = cap.saturating_mul(8);
        let ring = allocator.alloc(size)?.as_range().start();
        match Self::with_image(allocator, ring, cap) {
            Ok(owned) => Ok(owned),
            Err(e) => {
                // SAFETY: the ring was just allocated and linked to nothing.
                let _ = unsafe { dealloc_range(allocator, BStackRange::new(ring, size)) };
                Err(e)
            }
        }
    }

    fn with_image<A: BStackRaiiAllocator>(
        allocator: &A,
        data: u64,
        cap: u64,
    ) -> io::Result<BStackOwned<Self>> {
        let od = DequeOnDisk {
            header: BlockHeader {
                size: DEQUE_SIZE,
                tag: Self::eightcc(),
            },
            data,
            cap,
            head: 0,
            len: 0,
        };
        let range = alloc_image(allocator, bytemuck::bytes_of(&od))?;
        // SAFETY: a freshly allocated block owned by no other handle.
        Ok(unsafe { BStackOwned::from_raw(Self::from_range(range)) })
    }

    /// Number of elements.
    pub fn len(&self, stack: &BStack) -> io::Result<u64> {
        read_u64(stack, self.range.start() + LEN_OFF)
    }

    /// Whether the deque has no elements.
    pub fn is_empty(&self, stack: &BStack) -> io::Result<bool> {
        Ok(self.len(stack)? == 0)
    }

    /// Current ring capacity (elements storable before the next growth).
    pub fn capacity(&self, stack: &BStack) -> io::Result<u64> {
        read_u64(stack, self.range.start() + CAP_OFF)
    }

    /// Append a value to the back, taking ownership of its block. Grows the ring
    /// (once) if it is full, then commits the slot write + length bump atomically.
    pub fn push_back<A: BStackRaiiAllocator>(
        &self,
        allocator: &A,
        value: BStackOwned<T>,
    ) -> Result<(), ReplaceError<BStackOwned<T>>> {
        let handle = self.range.start();
        // Guard the value block so a stray return can't orphan it; on a failed
        // push [`finish_handback`] returns it to the caller rather than freeing it
        //, and defuses the guard on success once it is linked.
        let value = value.auto(allocator);
        let val_off = value.range().start();
        let outcome: io::Result<()> = (|| {
            loop {
                let full = Cell::new(false);
                let mut w: WriteBuf<2> = WriteBuf::new();
                atomic_update(
                    allocator,
                    &[
                        handle + HEAD_OFF,
                        handle + LEN_OFF,
                        handle + CAP_OFF,
                        handle + DATA_OFF,
                    ],
                    |_v1| Ok(Vec::new()),
                    |v1, _v2| {
                        let (head, len, cap, data) = (v1[0], v1[1], v1[2], v1[3]);
                        if len < cap {
                            // `len < cap` (unsigned) already implies `cap > 0`.
                            let idx = head.wrapping_add(len) % cap;
                            let slot = Self::slot_addr(data, idx)?;
                            w.push(w8(slot, val_off));
                            w.push(w8(handle + LEN_OFF, len + 1));
                        } else {
                            full.set(true);
                        }
                        Ok(w.as_slice())
                    },
                )?;
                if !full.get() {
                    return Ok(());
                }
                self.grow(allocator)?;
            }
        })();
        value.finish_handback(outcome)
    }

    /// Prepend a value to the front, taking ownership of its block.
    pub fn push_front<A: BStackRaiiAllocator>(
        &self,
        allocator: &A,
        value: BStackOwned<T>,
    ) -> Result<(), ReplaceError<BStackOwned<T>>> {
        let handle = self.range.start();
        // Guard the value block so a stray return can't orphan it; on a failed
        // push [`finish_handback`] returns it to the caller rather than freeing it
        //, and defuses the guard on success once it is linked.
        let value = value.auto(allocator);
        let val_off = value.range().start();
        let outcome: io::Result<()> = (|| {
            loop {
                let full = Cell::new(false);
                let mut w: WriteBuf<3> = WriteBuf::new();
                atomic_update(
                    allocator,
                    &[
                        handle + HEAD_OFF,
                        handle + LEN_OFF,
                        handle + CAP_OFF,
                        handle + DATA_OFF,
                    ],
                    |_v1| Ok(Vec::new()),
                    |v1, _v2| {
                        let (head, len, cap, data) = (v1[0], v1[1], v1[2], v1[3]);
                        if len < cap {
                            // `len < cap` (unsigned) already implies `cap > 0`.
                            let idx = head.wrapping_add(cap - 1) % cap;
                            let slot = Self::slot_addr(data, idx)?;
                            w.push(w8(slot, val_off));
                            w.push(w8(handle + HEAD_OFF, idx));
                            w.push(w8(handle + LEN_OFF, len + 1));
                        } else {
                            full.set(true);
                        }
                        Ok(w.as_slice())
                    },
                )?;
                if !full.get() {
                    return Ok(());
                }
                self.grow(allocator)?;
            }
        })();
        value.finish_handback(outcome)
    }

    /// Remove and return the last element (as an owned value block), or `None` if
    /// empty. Atomic: the slot is read and the length decremented in one commit;
    /// the value block's ownership transfers to the caller (its ring slot is left
    /// stale and reused by a later push).
    pub fn pop_back<A: BStackRaiiAllocator>(
        &self,
        allocator: &A,
    ) -> io::Result<Option<BStackOwned<T>>> {
        let handle = self.range.start();
        let got = Cell::new(false);
        let val = Cell::new(0u64);
        let mut w: WriteBuf<1> = WriteBuf::new();
        atomic_update(
            allocator,
            &[
                handle + HEAD_OFF,
                handle + LEN_OFF,
                handle + CAP_OFF,
                handle + DATA_OFF,
            ],
            |v1| {
                let (head, len, cap, data) = (v1[0], v1[1], v1[2], v1[3]);
                if len == 0 {
                    Ok(Vec::new())
                } else {
                    Ok(vec![Self::slot_addr(
                        data,
                        Self::ring_index(head, len - 1, cap)?,
                    )?])
                }
            },
            |v1, v2| {
                let len = v1[1];
                if len != 0 {
                    got.set(true);
                    val.set(v2[0]);
                    w.push(w8(handle + LEN_OFF, len - 1));
                }
                Ok(w.as_slice())
            },
        )?;
        if !got.get() {
            return Ok(None);
        }
        // SAFETY: the value block's ownership transfers to the caller.
        Ok(Some(unsafe {
            BStackOwned::from_raw(Self::value_at(val.get()))
        }))
    }

    /// Remove and return the first element (as an owned value block), or `None`
    /// if empty.
    pub fn pop_front<A: BStackRaiiAllocator>(
        &self,
        allocator: &A,
    ) -> io::Result<Option<BStackOwned<T>>> {
        let handle = self.range.start();
        let got = Cell::new(false);
        let val = Cell::new(0u64);
        let mut w: WriteBuf<2> = WriteBuf::new();
        atomic_update(
            allocator,
            &[
                handle + HEAD_OFF,
                handle + LEN_OFF,
                handle + CAP_OFF,
                handle + DATA_OFF,
            ],
            |v1| {
                let (head, len, cap, data) = (v1[0], v1[1], v1[2], v1[3]);
                if len == 0 {
                    Ok(Vec::new())
                } else {
                    Ok(vec![Self::slot_addr(
                        data,
                        Self::ring_index(head, 0, cap)?,
                    )?])
                }
            },
            |v1, v2| {
                let (head, len, cap) = (v1[0], v1[1], v1[2]);
                if len != 0 {
                    got.set(true);
                    val.set(v2[0]);
                    w.push(w8(handle + HEAD_OFF, Self::ring_index(head, 1, cap)?));
                    w.push(w8(handle + LEN_OFF, len - 1));
                }
                Ok(w.as_slice())
            },
        )?;
        if !got.get() {
            return Ok(None);
        }
        // SAFETY: the value block's ownership transfers to the caller.
        Ok(Some(unsafe {
            BStackOwned::from_raw(Self::value_at(val.get()))
        }))
    }

    /// Grow the ring to at least double its capacity, atomically snapshotting and
    /// re-basing the live elements. A no-op (beyond a wasted allocation, freed
    /// again) if another thread already made room.
    fn grow<A: BStackRaiiAllocator>(&self, allocator: &A) -> io::Result<()> {
        let handle = self.range.start();
        let cap0 = read_u64(allocator.stack(), handle + CAP_OFF)?;
        let newcap = if cap0 == 0 {
            MIN_CAP
        } else {
            cap0.saturating_mul(2)
        };
        // Allocate the new ring up front (an orphan until the commit swaps to it).
        let new_size = newcap
            .checked_mul(8)
            .ok_or_else(|| io_error!("deque capacity overflow"))?;
        let newring = allocator.alloc(new_size)?.as_range().start();

        let grown = Cell::new(false);
        let old_ring = Cell::new(0u64);
        let old_cap = Cell::new(0u64);

        // Abort if, at commit time, the ring already has room or is already at
        // least this big (another thread grew it) — then our `newring` is wasted.
        let abort = |_head: u64, len: u64, cap: u64| (cap != 0 && len < cap) || newcap <= cap;
        let mut w: Vec<(u64, SmallBuf)> = Vec::new();

        atomic_update(
            allocator,
            &[
                handle + HEAD_OFF,
                handle + LEN_OFF,
                handle + CAP_OFF,
                handle + DATA_OFF,
            ],
            |v1| {
                let (head, len, cap, data) = (v1[0], v1[1], v1[2], v1[3]);
                if abort(head, len, cap) {
                    Ok(Vec::new())
                } else {
                    // The live elements, in logical order.
                    (0..len)
                        .map(|i| Self::slot_addr(data, Self::ring_index(head, i, cap)?))
                        .collect::<io::Result<Vec<u64>>>()
                }
            },
            |v1, v2| {
                let (head, len, cap, data) = (v1[0], v1[1], v1[2], v1[3]);
                if !abort(head, len, cap) {
                    grown.set(true);
                    old_ring.set(data);
                    old_cap.set(cap);
                    w.reserve(v2.len() + 3);
                    // Copy every live element to the front of the new ring.
                    for (i, &r) in v2.iter().enumerate() {
                        let off = newring
                            .checked_add(
                                (i as u64)
                                    .checked_mul(8)
                                    .ok_or_else(|| io_error!("deque offset overflow"))?,
                            )
                            .ok_or_else(|| io_error!("deque offset overflow"))?;
                        w.push(w8(off, r));
                    }
                    // Swap the descriptor to the new ring, re-based at head 0.
                    w.push(w8(handle + DATA_OFF, newring));
                    w.push(w8(handle + CAP_OFF, newcap));
                    w.push(w8(handle + HEAD_OFF, 0u64));
                }
                Ok(w.as_slice())
            },
        )?;

        if grown.get() {
            // The old ring is now unreferenced; free it (leak-only on crash).
            if old_cap.get() > 0 {
                // SAFETY: the descriptor no longer points at the old ring.
                let _ = unsafe {
                    dealloc_range(
                        allocator,
                        BStackRange::new(old_ring.get(), old_cap.get().saturating_mul(8)),
                    )
                };
            }
        } else {
            // Growth was unnecessary; reclaim the unused new ring.
            // SAFETY: `newring` was never linked into the descriptor.
            let _ = unsafe { dealloc_range(allocator, BStackRange::new(newring, new_size)) };
        }
        Ok(())
    }

    /// A **borrowed** handle to the front value (no ownership), or `None` if empty.
    pub fn front(&self, stack: &BStack) -> io::Result<Option<T>> {
        let (head, len, cap, data) = Self::read_meta(stack, self.range.start())?;
        if len == 0 {
            return Ok(None);
        }
        let off = Self::slot_addr(data, Self::ring_index(head, 0, cap)?)?;
        Ok(Some(Self::value_at(read_u64(stack, off)?)))
    }

    /// A **borrowed** handle to the back value (no ownership), or `None` if empty.
    pub fn back(&self, stack: &BStack) -> io::Result<Option<T>> {
        let (head, len, cap, data) = Self::read_meta(stack, self.range.start())?;
        if len == 0 {
            return Ok(None);
        }
        let off = Self::slot_addr(data, Self::ring_index(head, len - 1, cap)?)?;
        Ok(Some(Self::value_at(read_u64(stack, off)?)))
    }

    /// Collect **borrowed** handles to every value, front to back. The handles
    /// alias the deque's blocks — do not free them; they stay valid only while the
    /// deque does.
    pub fn to_vec(&self, stack: &BStack) -> io::Result<Vec<T>> {
        let (head, len, cap, data) = Self::read_meta(stack, self.range.start())?;
        // Untrusted `len`: each element occupies 8 ring bytes, so bound it by the
        // stack before sizing an allocation with it (as `string.rs` does).
        let ring_bytes = len
            .checked_mul(8)
            .ok_or_else(|| io_error!("deque length overflow"))?;
        if ring_bytes > stack.len()? {
            return Err(io_error!("deque length larger than the stack"));
        }
        let mut out = Vec::with_capacity(len as usize);
        for i in 0..len {
            let off = Self::slot_addr(data, Self::ring_index(head, i, cap)?)?;
            out.push(Self::value_at(read_u64(stack, off)?));
        }
        Ok(out)
    }

    /// A lazy iterator over the elements, front to back, yielding `io::Result`
    /// value handles. A read snapshot: do not mutate the deque while iterating.
    pub fn iter<'a>(&self, stack: &'a BStack) -> io::Result<DequeIter<'a, T>> {
        let (head, len, cap, data) = Self::read_meta(stack, self.range.start())?;
        Ok(DequeIter {
            stack,
            block_off: self.range.start(),
            data,
            cap,
            head,
            len,
            pos: 0,
            _marker: PhantomData,
        })
    }
}

impl<T: BStackBlock> BStackCast for BStackDeque<T> {
    /// A `"Deq"` prefix over hash bytes perturbed by `T`'s tag, so deques of
    /// different element types never share a discriminant despite the identical
    /// on-disk layout.
    fn eightcc() -> EightCC {
        const BASE: EightCC = EightCC::new([b'D', b'e', b'q', 0x80, 0x81, 0x82, 0x83, 0x84]);
        BASE.mix(<T as BStackCast>::eightcc())
    }
}

// Self-contained (no separate control block): may be `#[embed]`ded.
impl<T: BStackBlock> crate::types::traits::BStackEmbeddable for BStackDeque<T> {}

impl<T: BStackBlock> BStackBlock for BStackDeque<T> {
    type OnDisk = DequeOnDisk;

    unsafe fn from_range(range: BStackRange) -> Self {
        BStackDeque {
            range,
            _marker: PhantomData,
        }
    }

    fn range(&self) -> BStackRange {
        self.range
    }

    /// Recursively free every element block and the ring, **without** freeing the
    /// handle block itself.
    fn __bstack_drop_children<A: BStackRaiiAllocator>(
        allocator: &A,
        range: BStackRange,
    ) -> io::Result<()> {
        let (head, len, cap, data) = Self::read_meta(allocator.stack(), range.start())?;
        for i in 0..len {
            let off = Self::slot_addr(data, Self::ring_index(head, i, cap)?)?;
            let r = read_u64(allocator.stack(), off)?;
            if r != 0 {
                // SAFETY: the deque solely owns each element block.
                let owned = unsafe { BStackOwned::from_raw(Self::value_at(r)) };
                owned.bstack_drop(allocator)?;
            }
        }
        if data != 0 {
            let size = cap
                .checked_mul(8)
                .ok_or_else(|| io_error!("deque capacity overflow"))?;
            // SAFETY: the deque solely owns its ring block.
            unsafe { dealloc_range(allocator, BStackRange::new(data, size))? };
        }
        Ok(())
    }

    /// Deep-clone the deque into `plan`: every element is deep-cloned (via `T`'s
    /// own clone hook) and packed into a fresh, compacted ring (`head = 0`,
    /// `cap = len`); the handle block is staged — all in the parent plan's single
    /// atomic commit.
    fn __bstack_clone_children_inplace<A: BStackRaiiAllocator>(
        &self,
        allocator: &A,
        plan: &mut ClonePlan,
    ) -> io::Result<Self::OnDisk> {
        let (head, len, cap, data) = Self::read_meta(allocator.stack(), self.range.start())?;

        // Untrusted `len`: checked math + stack bound before sizing allocations
        // (see `to_vec`); `ring_bytes` is also the fresh ring's size below.
        let ring_bytes = len
            .checked_mul(8)
            .ok_or_else(|| io_error!("deque length overflow"))?;
        if ring_bytes > allocator.len()? {
            return Err(io_error!("deque length larger than the stack"));
        }
        // Deep-clone each element (in logical order) into the plan.
        let mut dsts = Vec::with_capacity(len as usize);
        for i in 0..len {
            let off = Self::slot_addr(data, Self::ring_index(head, i, cap)?)?;
            let r = read_u64(allocator.stack(), off)?;
            let dst = if r != 0 {
                Self::value_at(r)
                    .__bstack_clone_into(allocator, plan)?
                    .start()
            } else {
                0
            };
            dsts.push(dst);
        }

        // Pack the cloned refs into a fresh, exactly-sized ring.
        let (new_data, new_cap) = if len > 0 {
            let ring = plan.alloc_raw(allocator, ring_bytes)?;
            let mut bytes = Vec::with_capacity(dsts.len() * 8);
            for d in &dsts {
                bytes.extend_from_slice(&d.to_le_bytes());
            }
            plan.write(ring.start(), bytes);
            (ring.start(), len)
        } else {
            (0, 0)
        };

        // Return the descriptor pointing at the freshly-cloned ring; the caller
        // places it — the default `__bstack_clone_into` in its own fresh block, or an
        // `#[embed]`ding parent inline in its payload. (Returning the descriptor
        // *verbatim* — the trait default — would alias the source's ring, so an
        // embedded collection MUST override this.)
        Ok(DequeOnDisk {
            header: BlockHeader {
                size: DEQUE_SIZE,
                tag: Self::eightcc(),
            },
            data: new_data,
            cap: new_cap,
            head: 0,
            len,
        })
    }
}

impl<T: BStackBlock> TryCloneIn for BStackDeque<T> {}

/// A front-to-back iterator over a [`BStackDeque`], yielding `io::Result<T>`
/// value handles. Created by [`BStackDeque::iter`].
pub struct DequeIter<'a, T: BStackBlock> {
    stack: &'a BStack,
    /// The deque handle block, re-read each step to detect mutation.
    block_off: u64,
    data: u64,
    cap: u64,
    head: u64,
    len: u64,
    pos: u64,
    _marker: PhantomData<fn() -> T>,
}

impl<'a, T: BStackBlock> Iterator for DequeIter<'a, T> {
    type Item = io::Result<T>;

    fn next(&mut self) -> Option<Self::Item> {
        if self.pos >= self.len {
            return None;
        }
        // Fail fast if the deque was mutated during iteration. A `grow` frees
        // the old ring and repoints `data`, but a `push`/`pop` mutates *in place*:
        // it advances `head` / changes `len` and hands out (or frees) an element
        // block **without moving the ring or clearing the vacated slot**, so
        // checking `data`/`cap` alone would miss it and later read a stale offset a
        // `T` handle could then free (use-after-free). Compare all four snapshot
        // fields — pure iteration never touches them, so any change is a mutation —
        // and turn it into a clean error.
        match BStackDeque::<T>::read_meta(self.stack, self.block_off) {
            Ok((head, len, cap, data))
                if data == self.data && cap == self.cap && head == self.head && len == self.len => {
            }
            Ok(_) => {
                self.pos = self.len;
                return Some(Err(io_error!(
                    "BStackDeque was mutated during iteration (its head/len/ring \
                     changed); the iterator is invalidated"
                )));
            }
            Err(e) => {
                self.pos = self.len;
                return Some(Err(e));
            }
        }
        let slot = match BStackDeque::<T>::ring_index(self.head, self.pos, self.cap)
            .and_then(|idx| BStackDeque::<T>::slot_addr(self.data, idx))
        {
            Ok(off) => off,
            Err(e) => {
                self.pos = self.len; // stop after an error
                return Some(Err(e));
            }
        };
        self.pos += 1;
        match read_u64(self.stack, slot) {
            Ok(vref) => Some(Ok(BStackDeque::<T>::value_at(vref))),
            Err(e) => {
                self.pos = self.len; // stop after an error
                Some(Err(e))
            }
        }
    }
}