#[cfg(feature = "alloc")]
use alloc::boxed::Box;
use core::error::Error;
use core::fmt::{Debug, Display, Formatter};
use core::ops::Deref;
use display_as_debug::fmt::DebugStructExt;
use display_as_debug::types::{Full, Short};
use nameof::name_of;
use tap::Pipe;
#[subdef::subdef]
pub struct TupleExtendError<E, P, I> {
#[cfg(doc)]
pub error: E,
#[cfg(doc)]
pub pending: Option<P>,
#[cfg(doc)]
pub remaining: I,
#[cfg(all(not(doc), feature = "alloc"))]
data: Box<TupleExtendErrorData<E, P, I>>,
#[cfg(all(not(doc), not(feature = "alloc")))]
data: TupleExtendErrorData<E, P, I>,
}
#[doc(hidden)]
pub struct TupleExtendErrorData<E, P, I> {
pub error: E,
pub pending: Option<P>,
pub remaining: I,
}
#[doc(hidden)]
impl<E, P, I> TupleExtendError<E, P, I> {
#[must_use]
#[cfg(feature = "alloc")]
pub fn new(error: E, pending: Option<P>, remaining: I) -> Self {
TupleExtendErrorData { error, pending, remaining }.pipe(Box::new).pipe(|data| Self { data })
}
#[must_use]
#[cfg(not(feature = "alloc"))]
pub fn new(error: E, pending: Option<P>, remaining: I) -> Self {
TupleExtendErrorData { error, pending, remaining }.pipe(|data| Self { data })
}
}
impl<E, P, I> TupleExtendError<E, P, I> {
#[must_use]
#[cfg(feature = "alloc")]
pub fn into_data(self) -> TupleExtendErrorData<E, P, I> {
*self.data
}
#[must_use]
#[cfg(not(feature = "alloc"))]
pub fn into_data(self) -> TupleExtendErrorData<E, P, I> {
self.data
}
}
#[doc(hidden)]
impl<E, P, I> Deref for TupleExtendError<E, P, I> {
type Target = TupleExtendErrorData<E, P, I>;
fn deref(&self) -> &Self::Target {
&self.data
}
}
impl<E: Debug, P, I> Debug for TupleExtendError<E, P, I> {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
f.debug_struct("TupleExtendError")
.field(name_of!(error in Self), &self.error)
.field_type::<P, Short>(name_of!(pending in Self))
.field_type::<I, Full>(name_of!(remaining in Self))
.finish()
}
}
#[doc(hidden)]
#[allow(clippy::missing_fields_in_debug, reason = "All fields actually covered")]
impl<E: Debug, P, I> Debug for TupleExtendErrorData<E, P, I> {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
f.debug_struct("TupleExtendErrorData")
.field(name_of!(error in Self), &self.error)
.field_type::<P, Short>(name_of!(pending in Self))
.field_type::<I, Full>(name_of!(remaining in Self))
.finish()
}
}
impl<E: Display, P, I> Display for TupleExtendError<E, P, I> {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
write!(f, "Error while extending collection: {}", self.error)
}
}
impl<E: Error + 'static, P, I> Error for TupleExtendError<E, P, I> {
fn source(&self) -> Option<&(dyn Error + 'static)> {
Some(&self.error)
}
}
#[doc(hidden)]
impl<E: Display, P, I> Display for TupleExtendErrorData<E, P, I> {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
write!(f, "Error while extending collection: {}", self.error)
}
}
#[doc(hidden)]
impl<E: Error + 'static, P, I> Error for TupleExtendErrorData<E, P, I> {
fn source(&self) -> Option<&(dyn Error + 'static)> {
Some(&self.error)
}
}