abin 0.1.6

A library for working with binaries and strings. The library tries to avoid heap-allocations / memory-copy whenever possible by automatically choosing a reasonable strategy: stack for small binaries; static-lifetime-binary or reference-counting.
Documentation
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()
    }
}