Skip to main content

abin/implementation/
factory_new.rs

1use std::marker::PhantomData;
2
3use crate::{
4    AnyRc, Bin, BinBuilder, BinFactory, BooToOwned, BuilderCfg, DefaultBinBuilder, IntoUnSyncView,
5    RcBin, SBin,
6};
7
8/// Default implementation used to create `Bin`. See `BinFactory` for documentation.
9///
10/// ```rust
11/// use abin::{NewBin, BinFactory, Bin, AnyBin};
12/// let bin : Bin = NewBin::from_static("Hello, I'm a binary!".as_bytes());
13/// assert_eq!("Hello, I'm a binary!".as_bytes(), bin.as_slice());
14/// ```
15pub struct NewBin {
16    _phantom: PhantomData<()>,
17}
18
19impl NewBin {
20    /// Constructs a builder that can be used to create `Bin`.
21    pub fn builder<'a>() -> impl BinBuilder<'a, T = Bin> {
22        DefaultBinBuilder::<NewBin, BinBuilderCfg>::new()
23    }
24}
25
26impl BooToOwned<[u8], Bin> for NewBin {
27    fn convert_to_owned(borrowed: &[u8]) -> Bin {
28        Self::copy_from_slice(borrowed)
29    }
30}
31
32struct BinBuilderCfg;
33
34impl BuilderCfg<Bin> for BinBuilderCfg {
35    fn convert_from_sbin_to_t(sbin: SBin) -> Bin {
36        sbin.un_sync()
37    }
38
39    fn vec_excess_capacity() -> usize {
40        RcBin::overhead_bytes()
41    }
42}