Skip to main content

osom_lib_btree/
defaults.rs

1//! Contains the default, recommended configuration for the B-tree.
2use osom_lib_alloc::traits::Allocator;
3use osom_lib_reprc::macros::reprc;
4use osom_lib_try_clone::TryClone;
5
6use crate::btree::{BTree, BTreeConfig};
7
8/// The default configuration for [`BTree`].
9#[reprc]
10#[repr(transparent)]
11#[derive(Debug, Default)]
12#[must_use]
13pub struct DefaultBTreeConfig<TAllocator: Allocator> {
14    allocator: TAllocator,
15}
16
17impl<TAllocator: Allocator> DefaultBTreeConfig<TAllocator> {
18    /// Creates a new [`DefaultBTreeConfig`] with the default allocator.
19    #[inline]
20    pub fn new() -> Self
21    where
22        TAllocator: Default,
23    {
24        Self::with_allocator(TAllocator::default())
25    }
26
27    /// Creates a new [`DefaultBTreeConfig`] with the specified allocator.
28    #[inline(always)]
29    pub const fn with_allocator(allocator: TAllocator) -> Self {
30        Self { allocator }
31    }
32}
33
34impl<TAllocator: Allocator> BTreeConfig for DefaultBTreeConfig<TAllocator> {
35    type ConcreteAllocator = TAllocator;
36
37    const CHILDREN_COUNT: usize = 16;
38
39    #[inline(always)]
40    fn allocator_mut(&mut self) -> &mut Self::ConcreteAllocator {
41        &mut self.allocator
42    }
43}
44
45/// An alias for [`BTree`] with [`DefaultBTreeConfig`].
46pub type DefaultBTree<TKey, TValue, TAllocator> = BTree<TKey, TValue, DefaultBTreeConfig<TAllocator>>;
47
48impl<TAllocator: Allocator + TryClone> TryClone for DefaultBTreeConfig<TAllocator> {
49    type Error = <TAllocator as TryClone>::Error;
50
51    fn try_clone(&self) -> Result<Self, Self::Error> {
52        Ok(Self {
53            allocator: self.allocator.try_clone()?,
54        })
55    }
56}