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
use std::marker::PhantomData;

use crate::{
    AnyRc, Bin, BinBuilder, BinFactory, BooToOwned, BuilderCfg, DefaultBinBuilder, IntoUnSyncView,
    RcBin, SBin,
};

/// Default implementation used to create `Bin`. See `BinFactory` for documentation.
///
/// ```rust
/// use abin::{NewBin, BinFactory, Bin, AnyBin};
/// let bin : Bin = NewBin::from_static("Hello, I'm a binary!".as_bytes());
/// assert_eq!("Hello, I'm a binary!".as_bytes(), bin.as_slice());
/// ```
pub struct NewBin {
    _phantom: PhantomData<()>,
}

impl NewBin {
    /// Constructs a builder that can be used to create `Bin`.
    pub fn builder<'a>() -> impl BinBuilder<'a, T = Bin> {
        DefaultBinBuilder::<NewBin, BinBuilderCfg>::new()
    }
}

impl BooToOwned<[u8], Bin> for NewBin {
    fn convert_to_owned(borrowed: &[u8]) -> Bin {
        Self::copy_from_slice(borrowed)
    }
}

struct BinBuilderCfg;

impl BuilderCfg<Bin> for BinBuilderCfg {
    fn convert_from_sbin_to_t(sbin: SBin) -> Bin {
        sbin.un_sync()
    }

    fn vec_excess_capacity() -> usize {
        RcBin::overhead_bytes()
    }
}