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

use serde::de;
use serde::de::Visitor;

use crate::{AnyBin, AnyStr};

pub struct ReIntegrationStrVisitor<TReIntegrator> {
    _phantom: PhantomData<TReIntegrator>,
}

impl<TReIntegrator> ReIntegrationStrVisitor<TReIntegrator> {
    pub fn new() -> Self {
        Self {
            _phantom: PhantomData::default(),
        }
    }
}

pub trait StrReIntegrator {
    type TBin: AnyBin;
    fn re_integrate_str(str: &str) -> AnyStr<Self::TBin>;
    fn re_integrate_string(string: String) -> AnyStr<Self::TBin>;
}

impl<'de, TReIntegrator> Visitor<'de> for ReIntegrationStrVisitor<TReIntegrator>
where
    TReIntegrator: StrReIntegrator,
{
    type Value = AnyStr<TReIntegrator::TBin>;

    fn expecting(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
        formatter.write_str("expecting a string")
    }

    #[inline]
    fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
    where
        E: de::Error,
    {
        Ok(TReIntegrator::re_integrate_str(v))
    }

    #[inline]
    fn visit_string<E>(self, v: String) -> Result<Self::Value, E>
    where
        E: de::Error,
    {
        Ok(TReIntegrator::re_integrate_string(v))
    }
}