btree_vec/lib.rs
1/*
2 * Copyright (C) 2021-2023, 2025 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#![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::cmp::Ordering;
102use core::fmt::{self, Debug, Formatter};
103use core::iter::{ExactSizeIterator, FusedIterator};
104use core::marker::PhantomData;
105use core::ops::{Index, IndexMut};
106use core::ptr::NonNull;
107
108#[cfg(btree_vec_debug)]
109pub mod debug;
110mod insert;
111mod node;
112mod remove;
113mod verified_alloc;
114
115use insert::{ItemInsertion, insert};
116use node::{LeafRef, Mutable, Node, NodeRef};
117use node::{PrefixCast, PrefixPtr, PrefixRef};
118use remove::remove;
119use verified_alloc::VerifiedAlloc;
120
121/// A growable array (vector) implemented as a B+ tree.
122///
123/// Provides non-amortized O(log n) random accesses, insertions, and removals,
124/// and O(n) iteration.
125///
126/// `B` is the branching factor. It must be at least 3. The standard library
127/// uses a value of 6 for its B-tree structures. Larger values are better when
128/// `T` is smaller.
129///
130/// # Mathematical variables
131///
132/// For the purposes of specifying the time complexity of various operations,
133/// *n* refers to the number of items in the vector.
134pub struct BTreeVec<T, const B: usize = 12, A: Allocator = Global> {
135 root: Option<PrefixPtr<T, B>>,
136 size: usize,
137 alloc: VerifiedAlloc<A>,
138 /// Lets dropck know that `T` may be dropped.
139 phantom: PhantomData<Box<T>>,
140}
141
142// SAFETY: `BTreeVec` owns its data, so it can be sent to another thread.
143unsafe impl<T, const B: usize, A> Send for BTreeVec<T, B, A>
144where
145 T: Send,
146 A: Allocator,
147{
148}
149
150// SAFETY: `BTreeVec` owns its data and provides access to it only through
151// standard borrows.
152unsafe impl<T, const B: usize, A> Sync for BTreeVec<T, B, A>
153where
154 T: Sync,
155 A: Allocator,
156{
157}
158
159fn leaf_for<T, const B: usize, R>(
160 mut root: PrefixRef<T, B, R>,
161 mut index: usize,
162) -> (LeafRef<T, B, R>, usize) {
163 loop {
164 let node = match root.cast() {
165 PrefixCast::Leaf(node) => return (node, index),
166 PrefixCast::Internal(node) => node,
167 };
168 let last = node.length() - 1;
169 let mut sizes = node.sizes.iter().copied().take(last);
170 let index = sizes
171 .position(|size| {
172 if let Some(n) = index.checked_sub(size) {
173 index = n;
174 false
175 } else {
176 true
177 }
178 })
179 .unwrap_or(last);
180 root = node.into_child(index);
181 }
182}
183
184impl<T> BTreeVec<T> {
185 /// Creates a new [`BTreeVec`]. Note that this function is implemented
186 /// only for the default value of `B`; see [`Self::create`] for an
187 /// equivalent that works with all values of `B`.
188 pub fn new() -> Self {
189 Self::create()
190 }
191}
192
193impl<T, A: Allocator> BTreeVec<T, 12, A> {
194 #[cfg_attr(
195 not(any(feature = "allocator_api", feature = "allocator-fallback")),
196 doc(hidden)
197 )]
198 /// Creates a new [`BTreeVec`] with the given allocator. Note that this
199 /// function is implemented only for the default value of `B`; see
200 /// [`Self::create_in`] for an equivalent that works with all values of
201 /// `B`.
202 pub fn new_in(alloc: A) -> Self {
203 Self::create_in(alloc)
204 }
205}
206
207impl<T, const B: usize> BTreeVec<T, B> {
208 /// Creates a new [`BTreeVec`]. This function exists because
209 /// [`BTreeVec::new`] is implemented only for the default value of `B`.
210 pub fn create() -> Self {
211 Self::create_in(Global)
212 }
213}
214
215impl<T, const B: usize, A: Allocator> BTreeVec<T, B, A> {
216 #[cfg_attr(
217 not(any(feature = "allocator_api", feature = "allocator-fallback")),
218 doc(hidden)
219 )]
220 /// Creates a new [`BTreeVec`] with the given allocator. This function
221 /// exists because [`BTreeVec::new_in`] is implemented only for the default
222 /// value of `B`.
223 pub fn create_in(alloc: A) -> Self {
224 assert!(B >= 3);
225 // SAFETY:
226 //
227 // * All nodes are allocated by `alloc`, either via the calls to
228 // `insert` and `LeafRef::alloc` in `Self::insert`. Nodes are
229 // deallocated in two places: via the call to `remove` in
230 // `Self::remove`, and via the call to `NodeRef::destroy` in
231 // `Self::drop`. In both of these cases, `alloc` is provided as the
232 // allocator with which to deallocate the nodes.
233 //
234 // * When `alloc` (`Self.alloc`) is dropped, `Self::drop` will have
235 // run, which destroys all nodes. If `alloc`'s memory is reused
236 // (e.g., via `mem::forget`), the only way this can happen is if the
237 // operation that made its memory able to be reused applied to the
238 // entire `BTreeVec`. Thus, all allocated nodes will become
239 // inaccessible as they are not exposed via any public APIs,
240 // guaranteeing that they will never be accessed.
241 let alloc = unsafe { VerifiedAlloc::new(alloc) };
242 Self {
243 root: None,
244 size: 0,
245 alloc,
246 phantom: PhantomData,
247 }
248 }
249
250 /// # Safety
251 ///
252 /// * There must not be any mutable references, including other
253 /// [`NodeRef`]s where `R` is [`Mutable`], to any data accessible via the
254 /// returned [`NodeRef`].
255 ///
256 /// [`Mutable`]: node::Mutable
257 unsafe fn leaf_for(&self, index: usize) -> (LeafRef<T, B>, usize) {
258 // SAFETY: Caller guarantees safety.
259 leaf_for(unsafe { NodeRef::new(self.root.unwrap()) }, index)
260 }
261
262 /// # Safety
263 ///
264 /// There must be no other references, including [`NodeRef`]s, to any data
265 /// accessible via the returned [`NodeRef`].
266 unsafe fn leaf_for_mut(
267 &mut self,
268 index: usize,
269 ) -> (LeafRef<T, B, Mutable>, usize) {
270 // SAFETY: Caller guarantees safety.
271 leaf_for(unsafe { NodeRef::new_mutable(self.root.unwrap()) }, index)
272 }
273
274 /// Gets the length of the vector.
275 ///
276 /// # Time complexity
277 ///
278 /// Constant.
279 pub fn len(&self) -> usize {
280 self.size
281 }
282
283 /// Checks whether the vector is empty.
284 ///
285 /// # Time complexity
286 ///
287 /// Constant.
288 pub fn is_empty(&self) -> bool {
289 self.size == 0
290 }
291
292 /// Gets the item at `index`, or [`None`] if no such item exists.
293 ///
294 /// # Time complexity
295 ///
296 /// Θ(log *n*).
297 pub fn get(&self, index: usize) -> Option<&T> {
298 (index < self.size).then(|| {
299 // SAFETY: `BTreeVec` uses `NodeRef`s in accordance with
300 // standard borrowing rules, so there are no existing mutable
301 // references.
302 let (leaf, index) = unsafe { self.leaf_for(index) };
303 &leaf.into_children()[index]
304 })
305 }
306
307 /// Gets a mutable reference to the item at `index`, or [`None`] if no such
308 /// item exists.
309 ///
310 /// # Time complexity
311 ///
312 /// Θ(log *n*).
313 pub fn get_mut(&mut self, index: usize) -> Option<&mut T> {
314 (index < self.size).then(|| {
315 // SAFETY: `BTreeVec` uses `NodeRef`s in accordance with
316 // standard borrowing rules, so there are no existing references.
317 let (leaf, index) = unsafe { self.leaf_for_mut(index) };
318 &mut leaf.into_children_mut()[index]
319 })
320 }
321
322 /// Gets the first item in the vector, or [`None`] if the vector is empty.
323 ///
324 /// # Time complexity
325 ///
326 /// Θ(log *n*).
327 pub fn first(&self) -> Option<&T> {
328 self.get(0)
329 }
330
331 /// Gets a mutable reference to the first item in the vector, or [`None`]
332 /// if the vector is empty.
333 ///
334 /// # Time complexity
335 ///
336 /// Θ(log *n*).
337 pub fn first_mut(&mut self) -> Option<&mut T> {
338 self.get_mut(0)
339 }
340
341 /// Gets the last item in the vector, or [`None`] if the vector is empty.
342 ///
343 /// # Time complexity
344 ///
345 /// Θ(log *n*).
346 pub fn last(&self) -> Option<&T> {
347 self.size.checked_sub(1).and_then(|s| self.get(s))
348 }
349
350 /// Gets a mutable reference to the last item in the vector, or [`None`] if
351 /// the vector is empty.
352 ///
353 /// # Time complexity
354 ///
355 /// Θ(log *n*).
356 pub fn last_mut(&mut self) -> Option<&mut T> {
357 self.size.checked_sub(1).and_then(move |s| self.get_mut(s))
358 }
359
360 /// Inserts `item` at `index`.
361 ///
362 /// # Panics
363 ///
364 /// Panics if `index` is greater than [`self.len()`](Self::len).
365 ///
366 /// # Time complexity
367 ///
368 /// Θ(log *n*).
369 pub fn insert(&mut self, index: usize, item: T) {
370 assert!(index <= self.size);
371 self.root.get_or_insert_with(|| {
372 LeafRef::alloc(&self.alloc).into_prefix().as_ptr()
373 });
374 // SAFETY: `BTreeVec` uses `NodeRef`s in accordance with standard
375 // borrowing rules, so there are no existing references.
376 let (leaf, index) = unsafe { self.leaf_for_mut(index) };
377 let root = insert(
378 ItemInsertion {
379 node: leaf,
380 index,
381 item,
382 root_size: self.size,
383 },
384 &self.alloc,
385 );
386 self.root = Some(root.as_ptr());
387 self.size += 1;
388 }
389
390 /// Inserts `item` at the end of the vector.
391 ///
392 /// # Time complexity
393 ///
394 /// Θ(log *n*).
395 pub fn push(&mut self, item: T) {
396 self.insert(self.size, item);
397 }
398
399 /// Removes and returns the item at `index`.
400 ///
401 /// # Panics
402 ///
403 /// Panics if `index` is not less than [`self.len()`](Self::len).
404 ///
405 /// # Time complexity
406 ///
407 /// Θ(log *n*).
408 pub fn remove(&mut self, index: usize) -> T {
409 assert!(index < self.size);
410 // SAFETY: `BTreeVec` uses `NodeRef`s in accordance with
411 // standard borrowing rules, so there are no existing references.
412 let (leaf, index) = unsafe { self.leaf_for_mut(index) };
413 let (root, item) = remove(leaf, index, &self.alloc);
414 self.root = Some(root.as_ptr());
415 self.size -= 1;
416 item
417 }
418
419 /// Removes and returns the last item in the vector, or [`None`] if the
420 /// vector is empty.
421 ///
422 /// # Time complexity
423 ///
424 /// Θ(log *n*).
425 pub fn pop(&mut self) -> Option<T> {
426 self.size.checked_sub(1).map(|s| self.remove(s))
427 }
428
429 /// Gets an iterator that returns references to each item in the vector.
430 ///
431 /// # Time complexity
432 ///
433 /// Iteration over the entire vector is Θ(*n*).
434 pub fn iter(&self) -> Iter<'_, T, B> {
435 // SAFETY: `BTreeVec` uses `NodeRef`s in accordance with standard
436 // borrowing rules, so there are no existing mutable references.
437 Iter {
438 leaf: self.root.map(|_| unsafe { self.leaf_for(0) }.0),
439 index: 0,
440 remaining: self.len(),
441 phantom: PhantomData,
442 }
443 }
444
445 /// Gets an iterator that returns mutable references to each item in the
446 /// vector.
447 ///
448 /// # Time complexity
449 ///
450 /// Iteration over the entire vector is Θ(*n*).
451 pub fn iter_mut(&mut self) -> IterMut<'_, T, B> {
452 // SAFETY: `BTreeVec` uses `NodeRef`s in accordance with standard
453 // borrowing rules, so there are no existing references.
454 IterMut {
455 leaf: self.root.map(|_| unsafe { self.leaf_for_mut(0) }.0),
456 index: 0,
457 remaining: self.len(),
458 phantom: PhantomData,
459 }
460 }
461}
462
463impl<T, const B: usize, A> Default for BTreeVec<T, B, A>
464where
465 A: Allocator + Default,
466{
467 fn default() -> Self {
468 Self::create_in(A::default())
469 }
470}
471
472impl<T, const B: usize, A: Allocator> Index<usize> for BTreeVec<T, B, A> {
473 type Output = T;
474
475 fn index(&self, index: usize) -> &T {
476 self.get(index).unwrap()
477 }
478}
479
480impl<T, const B: usize, A: Allocator> IndexMut<usize> for BTreeVec<T, B, A> {
481 fn index_mut(&mut self, index: usize) -> &mut T {
482 self.get_mut(index).unwrap()
483 }
484}
485
486impl<T: Debug, const B: usize, A: Allocator> Debug for BTreeVec<T, B, A> {
487 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
488 f.debug_list().entries(self.iter()).finish()
489 }
490}
491
492// SAFETY: This `Drop` impl does not directly or indirectly access any data in
493// any `T`, except for calling its destructor (see [1]), and `Self` contains a
494// `PhantomData<Box<T>>` so dropck knows that `T` may be dropped (see [2]).
495//
496// [1]: https://doc.rust-lang.org/nomicon/dropck.html
497// [2]: https://forge.rust-lang.org/libs/maintaining-std.html
498// #is-there-a-manual-drop-implementation
499#[cfg_attr(feature = "dropck_eyepatch", add_syntax::prepend(unsafe))]
500impl<#[cfg_attr(feature = "dropck_eyepatch", may_dangle)] T, const B: usize, A>
501 Drop for BTreeVec<T, B, A>
502where
503 A: Allocator,
504{
505 fn drop(&mut self) {
506 if let Some(root) = self.root {
507 // SAFETY: `BTreeVec` uses `NodeRef`s in accordance with
508 // standard borrowing rules, so there are no existing
509 // references.
510 unsafe { NodeRef::new_mutable(root) }.destroy(&self.alloc);
511 }
512 }
513}
514
515impl<T, const B: usize, A> Clone for BTreeVec<T, B, A>
516where
517 T: Clone,
518 A: Clone + Allocator,
519{
520 fn clone(&self) -> Self {
521 let root = self.root.map(|root| {
522 // SAFETY: `BTreeVec` uses `NodeRef`s in accordance with standard
523 // borrowing rules, so there are no existing references.
524 unsafe { NodeRef::new(root) }
525 .clone_node(None, &self.alloc)
526 .0
527 .as_ptr()
528 });
529 Self {
530 root,
531 size: self.size,
532 alloc: self.alloc.clone(),
533 phantom: self.phantom,
534 }
535 }
536}
537
538impl<T, const B: usize, A1, A2> PartialEq<BTreeVec<T, B, A2>>
539 for BTreeVec<T, B, A1>
540where
541 T: PartialEq,
542 A1: Allocator,
543 A2: Allocator,
544{
545 fn eq(&self, other: &BTreeVec<T, B, A2>) -> bool {
546 self.iter().eq(other.iter())
547 }
548}
549
550impl<T: Eq, const B: usize, A: Allocator> Eq for BTreeVec<T, B, A> {}
551
552impl<T, const B: usize, A1, A2> PartialOrd<BTreeVec<T, B, A2>>
553 for BTreeVec<T, B, A1>
554where
555 T: PartialOrd,
556 A1: Allocator,
557 A2: Allocator,
558{
559 fn partial_cmp(&self, other: &BTreeVec<T, B, A2>) -> Option<Ordering> {
560 self.iter().partial_cmp(other.iter())
561 }
562}
563
564impl<T, const B: usize, A> Ord for BTreeVec<T, B, A>
565where
566 T: Ord,
567 A: Allocator,
568{
569 fn cmp(&self, other: &Self) -> Ordering {
570 self.iter().cmp(other.iter())
571 }
572}
573
574fn nth<T, const B: usize, R>(
575 leaf: LeafRef<T, B, R>,
576 index: usize,
577 mut n: usize,
578) -> Option<(LeafRef<T, B, R>, usize)> {
579 if let Some(new) = n.checked_sub(leaf.length() - index) {
580 n = new;
581 } else {
582 return Some((leaf, index + n));
583 };
584 let mut child_index = leaf.index();
585 let mut parent = leaf.into_parent().ok()?;
586 loop {
587 let sizes = parent.sizes[..parent.length()].iter().copied();
588 for (i, size) in sizes.enumerate().skip(child_index + 1) {
589 if let Some(new) = n.checked_sub(size) {
590 n = new;
591 } else {
592 return Some(leaf_for(parent.into_child(i), n));
593 }
594 }
595 child_index = parent.index();
596 parent = parent.into_parent().ok()?;
597 }
598}
599
600/// An iterator over the items in a [`BTreeVec`].
601pub struct Iter<'a, T, const B: usize> {
602 leaf: Option<LeafRef<T, B>>,
603 index: usize,
604 remaining: usize,
605 phantom: PhantomData<&'a T>,
606}
607
608impl<'a, T, const B: usize> Iterator for Iter<'a, T, B> {
609 type Item = &'a T;
610
611 /// # Time complexity
612 ///
613 /// Worst-case Θ(log *n*), but iteration over the entire vector by
614 /// repeatedly calling this method is only Θ(*n*).
615 fn next(&mut self) -> Option<Self::Item> {
616 let mut leaf = self.leaf?;
617 if self.index == leaf.length() {
618 self.leaf = self.leaf.take().unwrap().into_next().ok();
619 leaf = self.leaf?;
620 self.index = 0;
621 }
622 let index = self.index;
623 self.index += 1;
624 self.remaining -= 1;
625 Some(&leaf.into_children()[index])
626 }
627
628 /// # Time complexity
629 ///
630 /// Worst-case Θ(log *n*).
631 fn nth(&mut self, n: usize) -> Option<Self::Item> {
632 let remaining = core::mem::replace(&mut self.remaining, 0);
633 let (leaf, i) = nth(self.leaf.take()?, self.index, n)?;
634 self.index = i + 1;
635 self.remaining = remaining - n - 1;
636 Some(&self.leaf.insert(leaf).into_children()[i])
637 }
638
639 fn size_hint(&self) -> (usize, Option<usize>) {
640 (self.remaining, Some(self.remaining))
641 }
642}
643
644impl<T, const B: usize> FusedIterator for Iter<'_, T, B> {}
645
646impl<T, const B: usize> ExactSizeIterator for Iter<'_, T, B> {
647 fn len(&self) -> usize {
648 let (lower, upper) = self.size_hint();
649 debug_assert_eq!(Some(lower), upper);
650 lower
651 }
652}
653
654impl<T, const B: usize> Clone for Iter<'_, T, B> {
655 fn clone(&self) -> Self {
656 Self {
657 leaf: self.leaf,
658 index: self.index,
659 remaining: self.remaining,
660 phantom: self.phantom,
661 }
662 }
663}
664
665// SAFETY: This type yields immutable references to items in the vector, so it
666// can be `Send` as long as `T` is `Sync` (which means `&T` is `Send`).
667unsafe impl<T: Sync, const B: usize> Send for Iter<'_, T, B> {}
668
669// SAFETY: This type has no `&self` methods that access shared data or fields
670// with non-`Sync` interior mutability, but `T` must be `Sync` to match the
671// `Send` impl, since this type implements `Clone`, effectively allowing it to
672// be sent.
673unsafe impl<T: Sync, const B: usize> Sync for Iter<'_, T, B> {}
674
675impl<'a, T, const B: usize, A> IntoIterator for &'a BTreeVec<T, B, A>
676where
677 A: Allocator,
678{
679 type Item = &'a T;
680 type IntoIter = Iter<'a, T, B>;
681
682 fn into_iter(self) -> Self::IntoIter {
683 self.iter()
684 }
685}
686
687/// A mutable iterator over the items in a [`BTreeVec`].
688pub struct IterMut<'a, T, const B: usize> {
689 leaf: Option<LeafRef<T, B, Mutable>>,
690 index: usize,
691 remaining: usize,
692 phantom: PhantomData<&'a mut T>,
693}
694
695impl<'a, T, const B: usize> Iterator for IterMut<'a, T, B> {
696 type Item = &'a mut T;
697
698 /// # Time complexity
699 ///
700 /// Worst-case Θ(log *n*), but iteration over the entire vector by
701 /// repeatedly calling this method is only Θ(*n*).
702 fn next(&mut self) -> Option<Self::Item> {
703 let mut leaf = self.leaf.as_mut()?;
704 if self.index == leaf.length() {
705 self.leaf = self.leaf.take().unwrap().into_next().ok();
706 leaf = self.leaf.as_mut()?;
707 self.index = 0;
708 }
709 let index = self.index;
710 self.index += 1;
711 self.remaining -= 1;
712 let item = &mut leaf.children_mut()[index];
713 // SAFETY: Extending the lifetime to `'a` is okay because `'a` doesn't
714 // outlive the `BTreeVec` and we won't access this index again for the
715 // life of the iterator.
716 Some(unsafe { NonNull::from(item).as_mut() })
717 }
718
719 /// # Time complexity
720 ///
721 /// Worst-case Θ(log *n*).
722 fn nth(&mut self, n: usize) -> Option<Self::Item> {
723 let remaining = core::mem::replace(&mut self.remaining, 0);
724 let (leaf, i) = nth(self.leaf.take()?, self.index, n)?;
725 self.index = i + 1;
726 self.remaining = remaining - n - 1;
727 let item = &mut self.leaf.insert(leaf).children_mut()[i];
728 // SAFETY: Extending the lifetime to `'a` is okay because `'a` doesn't
729 // outlive the `BTreeVec` and we won't access this index again for the
730 // life of the iterator.
731 Some(unsafe { NonNull::from(item).as_mut() })
732 }
733
734 fn size_hint(&self) -> (usize, Option<usize>) {
735 (self.remaining, Some(self.remaining))
736 }
737}
738
739impl<T, const B: usize> FusedIterator for IterMut<'_, T, B> {}
740
741impl<T, const B: usize> ExactSizeIterator for IterMut<'_, T, B> {
742 fn len(&self) -> usize {
743 let (lower, upper) = self.size_hint();
744 debug_assert_eq!(Some(lower), upper);
745 lower
746 }
747}
748
749// SAFETY: This type yields mutable references to items in the vector, so it
750// can be `Send` as long as `T` is `Send`. `T` doesn't need to be `Sync`
751// because no other iterator that yields items from the vector can exist at the
752// same time as this iterator.
753unsafe impl<T: Send, const B: usize> Send for IterMut<'_, T, B> {}
754
755// SAFETY: This type has no `&self` methods that access any fields.
756unsafe impl<T, const B: usize> Sync for IterMut<'_, T, B> {}
757
758impl<'a, T, const B: usize, A> IntoIterator for &'a mut BTreeVec<T, B, A>
759where
760 A: Allocator,
761{
762 type Item = &'a mut T;
763 type IntoIter = IterMut<'a, T, B>;
764
765 fn into_iter(self) -> Self::IntoIter {
766 self.iter_mut()
767 }
768}
769
770/// An owning iterator over the items in a [`BTreeVec`].
771pub struct IntoIter<T, const B: usize, A: Allocator = Global> {
772 leaf: Option<LeafRef<T, B, Mutable>>,
773 length: usize,
774 index: usize,
775 remaining: usize,
776 _tree: BTreeVec<T, B, A>,
777}
778
779impl<T, const B: usize, A: Allocator> Iterator for IntoIter<T, B, A> {
780 type Item = T;
781
782 /// # Time complexity
783 ///
784 /// Worst-case Θ(log *n*), but iteration over the entire vector by
785 /// repeatedly calling this method is only Θ(*n*).
786 fn next(&mut self) -> Option<Self::Item> {
787 let mut leaf = self.leaf.as_mut()?;
788 if self.index == self.length {
789 self.leaf = self.leaf.take().unwrap().into_next().ok();
790 leaf = self.leaf.as_mut()?;
791 self.index = 0;
792 self.length = leaf.length();
793 leaf.set_zero_length();
794 }
795 let index = self.index;
796 self.index += 1;
797 self.remaining -= 1;
798 // SAFETY: We haven't taken the item at `index` yet.
799 Some(unsafe { leaf.take_raw_child(index).assume_init() })
800 }
801
802 fn size_hint(&self) -> (usize, Option<usize>) {
803 (self.remaining, Some(self.remaining))
804 }
805}
806
807impl<T, const B: usize, A: Allocator> FusedIterator for IntoIter<T, B, A> {}
808
809impl<T, const B: usize> ExactSizeIterator for IntoIter<T, B> {
810 fn len(&self) -> usize {
811 let (lower, upper) = self.size_hint();
812 debug_assert_eq!(Some(lower), upper);
813 lower
814 }
815}
816
817// SAFETY: This type owns the items in the vector, so it can be `Send` as long
818// as `T` is `Send`.
819unsafe impl<T, const B: usize, A> Send for IntoIter<T, B, A>
820where
821 T: Send,
822 A: Allocator,
823{
824}
825
826// SAFETY: This type has no `&self` methods that access any fields.
827unsafe impl<T, const B: usize, A: Allocator> Sync for IntoIter<T, B, A> {}
828
829impl<T, const B: usize, A: Allocator> Drop for IntoIter<T, B, A> {
830 fn drop(&mut self) {
831 let mut leaf = if let Some(leaf) = self.leaf.take() {
832 leaf
833 } else {
834 return;
835 };
836 for i in self.index..self.length {
837 // SAFETY: We haven't taken the item at `index` yet.
838 unsafe {
839 leaf.take_raw_child(i).assume_init();
840 }
841 }
842 }
843}
844
845impl<T, const B: usize, A: Allocator> IntoIterator for BTreeVec<T, B, A> {
846 type Item = T;
847 type IntoIter = IntoIter<T, B, A>;
848
849 fn into_iter(mut self) -> Self::IntoIter {
850 // SAFETY: `BTreeVec` uses `NodeRef`s in accordance with standard
851 // borrowing rules, so because we own the `BTreeVec`, there are no
852 // existing references.
853 let leaf = self.root.map(|_| unsafe { self.leaf_for_mut(0) }.0);
854 IntoIter {
855 index: 0,
856 length: leaf.as_ref().map_or(0, |leaf| leaf.length()),
857 leaf,
858 remaining: self.len(),
859 _tree: self,
860 }
861 }
862}