btree_vec/
lib.rs

1/*
2 * Copyright (C) 2021-2023 taylor.fish <contact@taylor.fish>
3 *
4 * This file is part of btree-vec.
5 *
6 * btree-vec is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * btree-vec is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with btree-vec. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20#![cfg_attr(not(all(test, btree_vec_debug)), no_std)]
21#![cfg_attr(
22    any(feature = "allocator_api", has_allocator_api),
23    feature(allocator_api)
24)]
25#![cfg_attr(feature = "dropck_eyepatch", feature(dropck_eyepatch))]
26#![deny(unsafe_op_in_unsafe_fn)]
27
28//! This crate provides a growable array (vector) implemented using a B-tree
29//! (more specifically, a B+ tree). It provides non-amortized O(log *n*) random
30//! accesses, insertions, and removals, as well as O(*n*) iteration. The
31//! branching factor is also customizable.
32//!
33//! The design is similar to [unsorted counted B-trees][cb] as described by
34//! Simon Tatham.
35//!
36//! [cb]: https://www.chiark.greenend.org.uk/~sgtatham/algorithms/cbtree.html
37//!
38//! For now, the vector supports insertions and removals only of single
39//! elements, but bulk operations, including implementations of [`Extend`]
40//! and [`FromIterator`], may be added in the future.
41//!
42//! Example
43//! -------
44//!
45//! ```rust
46//! # use btree_vec::BTreeVec;
47//! let mut vec = BTreeVec::new();
48//! for i in 0..20 {
49//!     vec.push(i);
50//! }
51//! for i in 0..10 {
52//!     assert!(vec.remove(i) == i * 2);
53//! }
54//! for i in 0..10 {
55//!     assert!(vec[i] == i * 2 + 1);
56//! }
57//! for i in 0..10 {
58//!     vec.insert(i * 2, i * 2);
59//! }
60//! assert!(vec.len() == 20);
61//! for (i, n) in vec.iter().copied().enumerate() {
62//!     assert!(i == n);
63//! }
64//! ```
65//!
66//! Crate features
67//! --------------
68//!
69//! If the crate feature `dropck_eyepatch` is enabled, items in a [`BTreeVec`]
70//! can contain references with the same life as the vector itself. This
71//! requires Rust nightly, as the unstable language feature [`dropck_eyepatch`]
72//! must be used.
73//!
74//! If the crate feature `allocator_api` is enabled, you can configure
75//! [`BTreeVec`] with the unstable [`Allocator`] trait. Alternatively, if the
76//! feature `allocator-fallback` is enabled, this crate will use the allocator
77//! API provided by [allocator-fallback] instead of the standard library’s.
78//!
79//! [`dropck_eyepatch`]: https://github.com/rust-lang/rust/issues/34761
80//! [allocator-fallback]: https://docs.rs/allocator-fallback
81//!
82//! [`Extend`]: core::iter::Extend
83//! [`FromIterator`]: core::iter::FromIterator
84//! [`Allocator`]: alloc::alloc::Allocator
85
86extern crate alloc;
87
88#[cfg(feature = "allocator_api")]
89use alloc::alloc as allocator;
90
91#[cfg(not(feature = "allocator_api"))]
92#[cfg(feature = "allocator-fallback")]
93use allocator_fallback as allocator;
94
95#[cfg(not(any_allocator_api))]
96#[path = "alloc_fallback.rs"]
97mod allocator;
98
99use alloc::boxed::Box;
100use allocator::{Allocator, Global};
101use core::fmt::{self, Debug, Formatter};
102use core::iter::{ExactSizeIterator, FusedIterator};
103use core::marker::PhantomData;
104use core::ops::{Index, IndexMut};
105use core::ptr::NonNull;
106
107#[cfg(btree_vec_debug)]
108#[allow(dead_code)]
109pub mod debug;
110mod insert;
111mod node;
112mod remove;
113#[cfg(test)]
114mod tests;
115mod verified_alloc;
116
117use insert::{ItemInsertion, insert};
118use node::{LeafRef, Mutable, Node, NodeRef};
119use node::{PrefixCast, PrefixPtr, PrefixRef};
120use remove::remove;
121use verified_alloc::VerifiedAlloc;
122
123/// A growable array (vector) implemented as a B+ tree.
124///
125/// Provides non-amortized O(log n) random accesses, insertions, and removals,
126/// and O(n) iteration.
127///
128/// `B` is the branching factor. It must be at least 3. The standard library
129/// uses a value of 6 for its B-tree structures. Larger values are better when
130/// `T` is smaller.
131///
132/// # Mathematical variables
133///
134/// For the purposes of specifying the time complexity of various operations,
135/// *n* refers to the number of items in the vector.
136pub struct BTreeVec<T, const B: usize = 12, A: Allocator = Global> {
137    root: Option<PrefixPtr<T, B>>,
138    size: usize,
139    alloc: VerifiedAlloc<A>,
140    /// Lets dropck know that `T` may be dropped.
141    phantom: PhantomData<Box<T>>,
142}
143
144// SAFETY: `BTreeVec` owns its data, so it can be sent to another thread.
145unsafe impl<T, const B: usize, A> Send for BTreeVec<T, B, A>
146where
147    T: Send,
148    A: Allocator,
149{
150}
151
152// SAFETY: `BTreeVec` owns its data and provides access to it only through
153// standard borrows.
154unsafe impl<T, const B: usize, A> Sync for BTreeVec<T, B, A>
155where
156    T: Sync,
157    A: Allocator,
158{
159}
160
161fn leaf_for<T, const B: usize, R>(
162    mut root: PrefixRef<T, B, R>,
163    mut index: usize,
164) -> (LeafRef<T, B, R>, usize) {
165    loop {
166        let node = match root.cast() {
167            PrefixCast::Leaf(node) => return (node, index),
168            PrefixCast::Internal(node) => node,
169        };
170        let last = node.length() - 1;
171        let mut sizes = node.sizes.iter().copied().take(last);
172        let index = sizes
173            .position(|size| {
174                if let Some(n) = index.checked_sub(size) {
175                    index = n;
176                    false
177                } else {
178                    true
179                }
180            })
181            .unwrap_or(last);
182        root = node.into_child(index);
183    }
184}
185
186impl<T> BTreeVec<T> {
187    /// Creates a new [`BTreeVec`]. Note that this function is implemented
188    /// only for the default value of `B`; see [`Self::create`] for an
189    /// equivalent that works with all values of `B`.
190    pub fn new() -> Self {
191        Self::create()
192    }
193}
194
195impl<T, A: Allocator> BTreeVec<T, 12, A> {
196    #[cfg_attr(
197        not(any(feature = "allocator_api", feature = "allocator-fallback")),
198        doc(hidden)
199    )]
200    /// Creates a new [`BTreeVec`] with the given allocator. Note that this
201    /// function is implemented only for the default value of `B`; see
202    /// [`Self::create_in`] for an equivalent that works with all values of
203    /// `B`.
204    pub fn new_in(alloc: A) -> Self {
205        Self::create_in(alloc)
206    }
207}
208
209impl<T, const B: usize> BTreeVec<T, B> {
210    /// Creates a new [`BTreeVec`]. This function exists because
211    /// [`BTreeVec::new`] is implemented only for the default value of `B`.
212    pub fn create() -> Self {
213        Self::create_in(Global)
214    }
215}
216
217impl<T, const B: usize, A: Allocator> BTreeVec<T, B, A> {
218    #[cfg_attr(
219        not(any(feature = "allocator_api", feature = "allocator-fallback")),
220        doc(hidden)
221    )]
222    /// Creates a new [`BTreeVec`] with the given allocator. This function
223    /// exists because [`BTreeVec::new_in`] is implemented only for the default
224    /// value of `B`.
225    pub fn create_in(alloc: A) -> Self {
226        assert!(B >= 3);
227        // SAFETY:
228        //
229        // * All nodes are allocated by `alloc`, either via the calls to
230        //  `insert` and `LeafRef::alloc` in `Self::insert`. Nodes are
231        //  deallocated in two places: via the call to `remove` in
232        //  `Self::remove`, and via the call to `NodeRef::destroy` in
233        //  `Self::drop`. In both of these cases, `alloc` is provided as the
234        //  allocator with which to deallocate the nodes.
235        //
236        // * When `alloc` (`Self.alloc`) is dropped, `Self::drop` will have
237        //   run, which destroys all nodes. If `alloc`'s memory is reused
238        //   (e.g., via `mem::forget`), the only way this can happen is if the
239        //   operation that made its memory able to be reused applied to the
240        //   entire `BTreeVec`. Thus, all allocated nodes will become
241        //   inaccessible as they are not exposed via any public APIs,
242        //   guaranteeing that they will never be accessed.
243        let alloc = unsafe { VerifiedAlloc::new(alloc) };
244        Self {
245            root: None,
246            size: 0,
247            alloc,
248            phantom: PhantomData,
249        }
250    }
251
252    /// # Safety
253    ///
254    /// * There must not be any mutable references, including other
255    ///   [`NodeRef`]s where `R` is [`Mutable`], to any data accessible via the
256    ///   returned [`NodeRef`].
257    ///
258    /// [`Mutable`]: node::Mutable
259    unsafe fn leaf_for(&self, index: usize) -> (LeafRef<T, B>, usize) {
260        // SAFETY: Caller guarantees safety.
261        leaf_for(unsafe { NodeRef::new(self.root.unwrap()) }, index)
262    }
263
264    /// # Safety
265    ///
266    /// There must be no other references, including [`NodeRef`]s, to any data
267    /// accessible via the returned [`NodeRef`].
268    unsafe fn leaf_for_mut(
269        &mut self,
270        index: usize,
271    ) -> (LeafRef<T, B, Mutable>, usize) {
272        // SAFETY: Caller guarantees safety.
273        leaf_for(unsafe { NodeRef::new_mutable(self.root.unwrap()) }, index)
274    }
275
276    /// Gets the length of the vector.
277    ///
278    /// # Time complexity
279    ///
280    /// Constant.
281    pub fn len(&self) -> usize {
282        self.size
283    }
284
285    /// Checks whether the vector is empty.
286    ///
287    /// # Time complexity
288    ///
289    /// Constant.
290    pub fn is_empty(&self) -> bool {
291        self.size == 0
292    }
293
294    /// Gets the item at `index`, or [`None`] if no such item exists.
295    ///
296    /// # Time complexity
297    ///
298    /// Θ(log *n*).
299    pub fn get(&self, index: usize) -> Option<&T> {
300        (index < self.size).then(|| {
301            // SAFETY: `BTreeVec` uses `NodeRef`s in accordance with
302            // standard borrowing rules, so there are no existing mutable
303            // references.
304            let (leaf, index) = unsafe { self.leaf_for(index) };
305            leaf.into_child(index)
306        })
307    }
308
309    /// Gets a mutable reference to the item at `index`, or [`None`] if no such
310    /// item exists.
311    ///
312    /// # Time complexity
313    ///
314    /// Θ(log *n*).
315    pub fn get_mut(&mut self, index: usize) -> Option<&mut T> {
316        (index < self.size).then(|| {
317            // SAFETY: `BTreeVec` uses `NodeRef`s in accordance with
318            // standard borrowing rules, so there are no existing references.
319            let (leaf, index) = unsafe { self.leaf_for_mut(index) };
320            leaf.into_child_mut(index)
321        })
322    }
323
324    /// Gets the first item in the vector, or [`None`] if the vector is empty.
325    ///
326    /// # Time complexity
327    ///
328    /// Θ(log *n*).
329    pub fn first(&self) -> Option<&T> {
330        self.get(0)
331    }
332
333    /// Gets a mutable reference to the first item in the vector, or [`None`]
334    /// if the vector is empty.
335    ///
336    /// # Time complexity
337    ///
338    /// Θ(log *n*).
339    pub fn first_mut(&mut self) -> Option<&mut T> {
340        self.get_mut(0)
341    }
342
343    /// Gets the last item in the vector, or [`None`] if the vector is empty.
344    ///
345    /// # Time complexity
346    ///
347    /// Θ(log *n*).
348    pub fn last(&self) -> Option<&T> {
349        self.size.checked_sub(1).and_then(|s| self.get(s))
350    }
351
352    /// Gets a mutable reference to the last item in the vector, or [`None`] if
353    /// the vector is empty.
354    ///
355    /// # Time complexity
356    ///
357    /// Θ(log *n*).
358    pub fn last_mut(&mut self) -> Option<&mut T> {
359        self.size.checked_sub(1).and_then(move |s| self.get_mut(s))
360    }
361
362    /// Inserts `item` at `index`.
363    ///
364    /// # Panics
365    ///
366    /// Panics if `index` is greater than [`self.len()`](Self::len).
367    ///
368    /// # Time complexity
369    ///
370    /// Θ(log *n*).
371    pub fn insert(&mut self, index: usize, item: T) {
372        assert!(index <= self.size);
373        self.root.get_or_insert_with(|| {
374            LeafRef::alloc(&self.alloc).into_prefix().as_ptr()
375        });
376        // SAFETY: `BTreeVec` uses `NodeRef`s in accordance with standard
377        // borrowing rules, so there are no existing references.
378        let (leaf, index) = unsafe { self.leaf_for_mut(index) };
379        let root = insert(
380            ItemInsertion {
381                node: leaf,
382                index,
383                item,
384                root_size: self.size,
385            },
386            &self.alloc,
387        );
388        self.root = Some(root.as_ptr());
389        self.size += 1;
390    }
391
392    /// Inserts `item` at the end of the vector.
393    ///
394    /// # Time complexity
395    ///
396    /// Θ(log *n*).
397    pub fn push(&mut self, item: T) {
398        self.insert(self.size, item);
399    }
400
401    /// Removes and returns the item at `index`.
402    ///
403    /// # Panics
404    ///
405    /// Panics if `index` is not less than [`self.len()`](Self::len).
406    ///
407    /// # Time complexity
408    ///
409    /// Θ(log *n*).
410    pub fn remove(&mut self, index: usize) -> T {
411        assert!(index < self.size);
412        // SAFETY: `BTreeVec` uses `NodeRef`s in accordance with
413        // standard borrowing rules, so there are no existing references.
414        let (leaf, index) = unsafe { self.leaf_for_mut(index) };
415        let (root, item) = remove(leaf, index, &self.alloc);
416        self.root = Some(root.as_ptr());
417        self.size -= 1;
418        item
419    }
420
421    /// Removes and returns the last item in the vector, or [`None`] if the
422    /// vector is empty.
423    ///
424    /// # Time complexity
425    ///
426    /// Θ(log *n*).
427    pub fn pop(&mut self) -> Option<T> {
428        self.size.checked_sub(1).map(|s| self.remove(s))
429    }
430
431    /// Gets an iterator that returns references to each item in the vector.
432    ///
433    /// # Time complexity
434    ///
435    /// Iteration over the entire vector is Θ(*n*).
436    pub fn iter(&self) -> Iter<'_, T, B> {
437        // SAFETY: `BTreeVec` uses `NodeRef`s in accordance with standard
438        // borrowing rules, so there are no existing mutable references.
439        Iter {
440            leaf: self.root.map(|_| unsafe { self.leaf_for(0) }.0),
441            index: 0,
442            remaining: self.len(),
443            phantom: PhantomData,
444        }
445    }
446
447    /// Gets an iterator that returns mutable references to each item in the
448    /// vector.
449    ///
450    /// # Time complexity
451    ///
452    /// Iteration over the entire vector is Θ(*n*).
453    pub fn iter_mut(&mut self) -> IterMut<'_, T, B> {
454        // SAFETY: `BTreeVec` uses `NodeRef`s in accordance with standard
455        // borrowing rules, so there are no existing references.
456        IterMut {
457            leaf: self.root.map(|_| unsafe { self.leaf_for_mut(0) }.0),
458            index: 0,
459            remaining: self.len(),
460            phantom: PhantomData,
461        }
462    }
463}
464
465impl<T, const B: usize, A> Default for BTreeVec<T, B, A>
466where
467    A: Allocator + Default,
468{
469    fn default() -> Self {
470        Self::create_in(A::default())
471    }
472}
473
474impl<T, const B: usize, A: Allocator> Index<usize> for BTreeVec<T, B, A> {
475    type Output = T;
476
477    fn index(&self, index: usize) -> &T {
478        self.get(index).unwrap()
479    }
480}
481
482impl<T, const B: usize, A: Allocator> IndexMut<usize> for BTreeVec<T, B, A> {
483    fn index_mut(&mut self, index: usize) -> &mut T {
484        self.get_mut(index).unwrap()
485    }
486}
487
488impl<T: Debug, const B: usize, A: Allocator> Debug for BTreeVec<T, B, A> {
489    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
490        f.debug_list().entries(self.iter()).finish()
491    }
492}
493
494// SAFETY: This `Drop` impl does not directly or indirectly access any data in
495// any `T`, except for calling its destructor (see [1]), and `Self` contains a
496// `PhantomData<Box<T>>` so dropck knows that `T` may be dropped (see [2]).
497//
498// [1]: https://doc.rust-lang.org/nomicon/dropck.html
499// [2]: https://forge.rust-lang.org/libs/maintaining-std.html
500//      #is-there-a-manual-drop-implementation
501#[cfg_attr(feature = "dropck_eyepatch", add_syntax::prepend(unsafe))]
502impl<#[cfg_attr(feature = "dropck_eyepatch", may_dangle)] T, const B: usize, A>
503    Drop for BTreeVec<T, B, A>
504where
505    A: Allocator,
506{
507    fn drop(&mut self) {
508        if let Some(root) = self.root {
509            // SAFETY: `BTreeVec` uses `NodeRef`s in accordance with
510            // standard borrowing rules, so there are no existing
511            // references.
512            unsafe { NodeRef::new_mutable(root) }.destroy(&self.alloc);
513        }
514    }
515}
516
517fn nth<T, const B: usize, R>(
518    leaf: LeafRef<T, B, R>,
519    index: usize,
520    mut n: usize,
521) -> Option<(LeafRef<T, B, R>, usize)> {
522    if let Some(new) = n.checked_sub(leaf.length() - index) {
523        n = new;
524    } else {
525        return Some((leaf, index + n));
526    };
527    let mut child_index = leaf.index();
528    let mut parent = leaf.into_parent().ok()?;
529    loop {
530        let sizes = parent.sizes[..parent.length()].iter().copied();
531        for (i, size) in sizes.enumerate().skip(child_index + 1) {
532            if let Some(new) = n.checked_sub(size) {
533                n = new;
534            } else {
535                return Some(leaf_for(parent.into_child(i), n));
536            }
537        }
538        child_index = parent.index();
539        parent = parent.into_parent().ok()?;
540    }
541}
542
543/// An iterator over the items in a [`BTreeVec`].
544pub struct Iter<'a, T, const B: usize> {
545    leaf: Option<LeafRef<T, B>>,
546    index: usize,
547    remaining: usize,
548    phantom: PhantomData<&'a T>,
549}
550
551impl<'a, T, const B: usize> Iterator for Iter<'a, T, B> {
552    type Item = &'a T;
553
554    fn next(&mut self) -> Option<Self::Item> {
555        let mut leaf = self.leaf?;
556        if self.index == leaf.length() {
557            self.leaf = self.leaf.take().unwrap().into_next().ok();
558            leaf = self.leaf?;
559            self.index = 0;
560        }
561        let index = self.index;
562        self.index += 1;
563        Some(leaf.into_child(index))
564    }
565
566    fn nth(&mut self, n: usize) -> Option<Self::Item> {
567        let (leaf, i) = nth(self.leaf.take()?, self.index, n)?;
568        self.index = i + 1;
569        Some(self.leaf.insert(leaf).into_child(i))
570    }
571
572    fn size_hint(&self) -> (usize, Option<usize>) {
573        (self.remaining, Some(self.remaining))
574    }
575}
576
577impl<T, const B: usize> FusedIterator for Iter<'_, T, B> {}
578
579impl<T, const B: usize> ExactSizeIterator for Iter<'_, T, B> {
580    fn len(&self) -> usize {
581        let (lower, upper) = self.size_hint();
582        debug_assert_eq!(Some(lower), upper);
583        lower
584    }
585}
586
587impl<T, const B: usize> Clone for Iter<'_, T, B> {
588    fn clone(&self) -> Self {
589        Self {
590            leaf: self.leaf,
591            index: self.index,
592            remaining: self.remaining,
593            phantom: self.phantom,
594        }
595    }
596}
597
598// SAFETY: This type yields immutable references to items in the vector, so it
599// can be `Send` as long as `T` is `Sync` (which means `&T` is `Send`).
600unsafe impl<T: Sync, const B: usize> Send for Iter<'_, T, B> {}
601
602// SAFETY: This type has no `&self` methods that access shared data or fields
603// with non-`Sync` interior mutability, but `T` must be `Sync` to match the
604// `Send` impl, since this type implements `Clone`, effectively allowing it to
605// be sent.
606unsafe impl<T: Sync, const B: usize> Sync for Iter<'_, T, B> {}
607
608impl<'a, T, const B: usize, A> IntoIterator for &'a BTreeVec<T, B, A>
609where
610    A: Allocator,
611{
612    type Item = &'a T;
613    type IntoIter = Iter<'a, T, B>;
614
615    fn into_iter(self) -> Self::IntoIter {
616        self.iter()
617    }
618}
619
620/// A mutable iterator over the items in a [`BTreeVec`].
621pub struct IterMut<'a, T, const B: usize> {
622    leaf: Option<LeafRef<T, B, Mutable>>,
623    index: usize,
624    remaining: usize,
625    phantom: PhantomData<&'a mut T>,
626}
627
628impl<'a, T, const B: usize> Iterator for IterMut<'a, T, B> {
629    type Item = &'a mut T;
630
631    fn next(&mut self) -> Option<Self::Item> {
632        let mut leaf = self.leaf.as_mut()?;
633        if self.index == leaf.length() {
634            self.leaf = self.leaf.take().unwrap().into_next().ok();
635            leaf = self.leaf.as_mut()?;
636            self.index = 0;
637        }
638        let index = self.index;
639        self.index += 1;
640        // SAFETY: Extending the lifetime to `'a` is okay because `'a` doesn't
641        // outlive the `BTreeVec` and we won't access this index again for the
642        // life of the iterator.
643        Some(unsafe { NonNull::from(leaf.child_mut(index)).as_mut() })
644    }
645
646    fn nth(&mut self, n: usize) -> Option<Self::Item> {
647        let (leaf, i) = nth(self.leaf.take()?, self.index, n)?;
648        self.index = i + 1;
649        // SAFETY: Extending the lifetime to `'a` is okay because `'a` doesn't
650        // outlive the `BTreeVec` and we won't access this index again for the
651        // life of the iterator.
652        Some(unsafe {
653            NonNull::from(self.leaf.insert(leaf).child_mut(i)).as_mut()
654        })
655    }
656
657    fn size_hint(&self) -> (usize, Option<usize>) {
658        (self.remaining, Some(self.remaining))
659    }
660}
661
662impl<T, const B: usize> FusedIterator for IterMut<'_, T, B> {}
663
664impl<T, const B: usize> ExactSizeIterator for IterMut<'_, T, B> {
665    fn len(&self) -> usize {
666        let (lower, upper) = self.size_hint();
667        debug_assert_eq!(Some(lower), upper);
668        lower
669    }
670}
671
672// SAFETY: This type yields mutable references to items in the vector, so it
673// can be `Send` as long as `T` is `Send`. `T` doesn't need to be `Sync`
674// because no other iterator that yields items from the vector can exist at the
675// same time as this iterator.
676unsafe impl<T: Send, const B: usize> Send for IterMut<'_, T, B> {}
677
678// SAFETY: This type has no `&self` methods that access any fields.
679unsafe impl<T, const B: usize> Sync for IterMut<'_, T, B> {}
680
681impl<'a, T, const B: usize, A> IntoIterator for &'a mut BTreeVec<T, B, A>
682where
683    A: Allocator,
684{
685    type Item = &'a mut T;
686    type IntoIter = IterMut<'a, T, B>;
687
688    fn into_iter(self) -> Self::IntoIter {
689        self.iter_mut()
690    }
691}
692
693/// An owning iterator over the items in a [`BTreeVec`].
694pub struct IntoIter<T, const B: usize, A: Allocator = Global> {
695    leaf: Option<LeafRef<T, B, Mutable>>,
696    length: usize,
697    index: usize,
698    remaining: usize,
699    _tree: BTreeVec<T, B, A>,
700}
701
702impl<T, const B: usize, A: Allocator> Iterator for IntoIter<T, B, A> {
703    type Item = T;
704
705    fn next(&mut self) -> Option<Self::Item> {
706        let mut leaf = self.leaf.as_mut()?;
707        if self.index == self.length {
708            self.leaf = self.leaf.take().unwrap().into_next().ok();
709            leaf = self.leaf.as_mut()?;
710            self.index = 0;
711            self.length = leaf.length();
712            leaf.set_zero_length();
713        }
714        let index = self.index;
715        self.index += 1;
716        self.remaining -= 1;
717        // SAFETY: We haven't taken the item at `index` yet.
718        Some(unsafe { leaf.take_raw_child(index).assume_init() })
719    }
720
721    fn size_hint(&self) -> (usize, Option<usize>) {
722        (self.remaining, Some(self.remaining))
723    }
724}
725
726impl<T, const B: usize, A: Allocator> FusedIterator for IntoIter<T, B, A> {}
727
728impl<T, const B: usize> ExactSizeIterator for IntoIter<T, B> {
729    fn len(&self) -> usize {
730        let (lower, upper) = self.size_hint();
731        debug_assert_eq!(Some(lower), upper);
732        lower
733    }
734}
735
736// SAFETY: This type owns the items in the vector, so it can be `Send` as long
737// as `T` is `Send`.
738unsafe impl<T, const B: usize, A> Send for IntoIter<T, B, A>
739where
740    T: Send,
741    A: Allocator,
742{
743}
744
745// SAFETY: This type has no `&self` methods that access any fields.
746unsafe impl<T, const B: usize, A: Allocator> Sync for IntoIter<T, B, A> {}
747
748impl<T, const B: usize, A: Allocator> Drop for IntoIter<T, B, A> {
749    fn drop(&mut self) {
750        let mut leaf = if let Some(leaf) = self.leaf.take() {
751            leaf
752        } else {
753            return;
754        };
755        for i in self.index..self.length {
756            // SAFETY: We haven't taken the item at `index` yet.
757            unsafe {
758                leaf.take_raw_child(i).assume_init();
759            }
760        }
761    }
762}
763
764impl<T, const B: usize, A: Allocator> IntoIterator for BTreeVec<T, B, A> {
765    type Item = T;
766    type IntoIter = IntoIter<T, B, A>;
767
768    fn into_iter(mut self) -> Self::IntoIter {
769        // SAFETY: `BTreeVec` uses `NodeRef`s in accordance with standard
770        // borrowing rules, so because we own the `BTreeVec`, there are no
771        // existing references.
772        let leaf = self.root.map(|_| unsafe { self.leaf_for_mut(0) }.0);
773        IntoIter {
774            index: 0,
775            length: leaf.as_ref().map_or(0, |leaf| leaf.length()),
776            leaf,
777            remaining: self.len(),
778            _tree: self,
779        }
780    }
781}