use std::{fmt, io};
use bstack::BStackRange;
use super::impl_source_error;
use crate::BStackRaiiAllocator;
use crate::types::traits::BStackDrop;
pub struct ReplaceError<V> {
pub source: io::Error,
pub value: Option<V>,
pub raw_old: Vec<BStackRange>,
}
impl<V> ReplaceError<V> {
#[inline]
pub fn recovered(source: io::Error, value: V) -> Self {
Self {
source,
value: Some(value),
raw_old: Vec::new(),
}
}
#[inline]
pub fn lost(source: io::Error) -> Self {
Self {
source,
value: None,
raw_old: Vec::new(),
}
}
#[inline]
pub fn lost_raw(source: io::Error, raw_old: Vec<BStackRange>) -> Self {
Self {
source,
value: None,
raw_old,
}
}
#[inline]
pub fn into_source(self) -> io::Error {
self.source
}
}
impl<V: BStackDrop> ReplaceError<V> {
#[inline]
pub fn discard_freeing<A: BStackRaiiAllocator>(self, allocator: &A) -> io::Error {
if let Some(value) = self.value {
let _ = value.bstack_drop(allocator);
}
self.source
}
}
impl<V> fmt::Debug for ReplaceError<V> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ReplaceError")
.field("source", &self.source)
.field("value", &self.value.as_ref().map(|_| "..."))
.field("raw_old", &self.raw_old)
.finish()
}
}
impl_source_error!(ReplaceError<V>);