use core::error::Error;
use core::fmt;
use core::marker::PhantomData;
use crate::alloc::{self, Allocator, String};
#[cfg(feature = "alloc")]
use crate::alloc::{System, SYSTEM};
#[cfg(test)]
use crate::mode::Binary;
use crate::Context;
use super::ContextError;
#[cfg(test)]
use super::ErrorMarker;
pub struct Same<M, E, A>
where
E: ContextError,
{
alloc: A,
_marker: PhantomData<(M, E)>,
}
#[cfg(feature = "alloc")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "alloc")))]
impl<M, E> Same<M, E, &'static System>
where
E: ContextError,
{
pub fn new() -> Self {
Self::with_alloc(&SYSTEM)
}
}
impl<M, E, A> Same<M, E, A>
where
E: ContextError,
{
pub fn with_alloc(alloc: A) -> Self {
Self {
alloc,
_marker: PhantomData,
}
}
}
#[cfg(test)]
impl<A> Same<Binary, ErrorMarker, A> {
pub(crate) fn with_marker(alloc: A) -> Self {
Self::with_alloc(alloc)
}
}
impl<M, E, A> Context for Same<M, E, A>
where
A: Allocator,
M: 'static,
E: ContextError,
{
type Mode = M;
type Error = E;
type Mark = ();
type Allocator = A;
type String<'this> = String<'this, A> where Self: 'this;
#[inline]
fn clear(&self) {}
#[inline]
fn alloc(&self) -> &Self::Allocator {
&self.alloc
}
#[inline]
fn collect_string<T>(&self, value: &T) -> Result<Self::String<'_>, Self::Error>
where
T: ?Sized + fmt::Display,
{
alloc::collect_string(self, value)
}
#[inline]
fn custom<T>(&self, message: T) -> Self::Error
where
T: 'static + Send + Sync + Error,
{
E::custom(message)
}
#[inline]
fn message<T>(&self, message: T) -> Self::Error
where
T: fmt::Display,
{
E::message(message)
}
}
#[cfg(feature = "alloc")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "alloc")))]
impl<M, E> Default for Same<M, E, &'static System>
where
E: ContextError,
{
#[inline]
fn default() -> Self {
Self::new()
}
}