use core::mem;
use super::{super::Options, Ascend};
use crate::{
allocator::Sealed,
error::Error,
options::{CompressionPolicy, Freelist},
traits::Constructable,
types::{Height, KeySize},
Arena,
};
#[cfg(all(feature = "memmap", not(target_family = "wasm")))]
#[cfg_attr(docsrs, doc(cfg(all(feature = "memmap", not(target_family = "wasm")))))]
mod memmap;
#[derive(Debug)]
pub struct Builder<C = Ascend> {
options: Options,
cmp: C,
}
impl<C> Default for Builder<C>
where
C: Default,
{
#[inline]
fn default() -> Self {
Self {
options: Options::default(),
cmp: Default::default(),
}
}
}
impl<C: Default> From<Options> for Builder<C> {
#[inline]
fn from(options: Options) -> Self {
Self {
options,
cmp: Default::default(),
}
}
}
impl<C> From<Builder<C>> for Options {
#[inline]
fn from(builder: Builder<C>) -> Self {
builder.options
}
}
impl<C> Builder<C> {
#[inline]
pub const fn with(cmp: C) -> Self {
Self {
options: Options::new(),
cmp,
}
}
}
impl<C: Default> Builder<C> {
#[inline]
pub fn new() -> Self {
Self {
options: Options::new(),
cmp: Default::default(),
}
}
}
impl<C> Builder<C> {
#[inline]
pub const fn comparator(&self) -> &C {
&self.cmp
}
#[inline]
pub fn with_comparator<NC>(self, cmp: NC) -> Builder<NC> {
Builder {
options: self.options,
cmp,
}
}
#[inline]
pub const fn options(&self) -> &Options {
&self.options
}
#[inline]
pub const fn with_options(mut self, opts: Options) -> Self {
self.options = opts;
self
}
crate::__builder_opts!(generic::Builder);
}
impl<C> Builder<C> {
#[inline]
pub fn alloc<T>(self) -> Result<T, Error>
where
T: Arena,
T::Constructable: Constructable<Comparator = C>,
{
let node_align =
mem::align_of::<<<T::Constructable as Constructable>::Allocator as Sealed>::Node>();
let Builder { options, cmp, .. } = self;
options
.to_arena_options()
.with_maximum_alignment(node_align)
.alloc::<<<T::Constructable as Constructable>::Allocator as Sealed>::Allocator>()
.map_err(Into::into)
.and_then(|arena| T::construct(arena, options, false, cmp))
}
}