use std::error::Error;
use std::fmt::Debug;
use std::fmt::Display;
use std::fmt::Formatter;
use std::fmt::Result as FmtResult;
#[must_use]
pub struct ContractTempFailure<T> {
error: Box<dyn Error + Send>,
resource: T,
}
impl<T> ContractTempFailure<T> {
pub(crate) fn new(error: impl Error + Send + 'static, resource: T) -> Self {
Self {
error: Box::new(error),
resource,
}
}
#[must_use = "inspect or retain the original error"]
#[inline]
pub fn error(&self) -> &(dyn Error + Send + 'static) {
self.error.as_ref()
}
#[must_use = "inspect the retained resource"]
#[inline]
pub const fn resource(&self) -> &T {
&self.resource
}
#[must_use = "use the retained resource to recover or clean it up"]
#[inline]
pub fn resource_mut(&mut self) -> &mut T {
&mut self.resource
}
#[must_use = "handle the transferred error and resource"]
pub fn into_parts(self) -> (Box<dyn Error + Send>, T) {
(self.error, self.resource)
}
}
impl<T> Debug for ContractTempFailure<T> {
fn fmt(&self, formatter: &mut Formatter<'_>) -> FmtResult {
formatter.write_str("ContractTempFailure { resource: retained, error: retained }")
}
}
impl<T> Display for ContractTempFailure<T> {
fn fmt(&self, formatter: &mut Formatter<'_>) -> FmtResult {
formatter.write_str("temporary lifecycle failed; original error and recovery resource retained")
}
}
impl<T> Error for ContractTempFailure<T> {
fn source(&self) -> Option<&(dyn Error + 'static)> {
Some(self.error.as_ref())
}
}