Move transparent wrapper errors to own module

This commit is contained in:
David Tolnay 2019-11-27 14:22:50 -08:00
parent 303e1a08e7
commit ba0b30790c
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82
3 changed files with 50 additions and 46 deletions

View File

@ -1,4 +1,5 @@
use crate::backtrace::Backtrace;
use crate::wrapper::{DisplayError, MessageError};
use crate::{Chain, Error};
use std::any::TypeId;
use std::error::Error as StdError;
@ -641,52 +642,6 @@ pub(crate) struct ContextError<C, E> {
pub error: E,
}
#[repr(transparent)]
struct MessageError<M>(M);
impl<M> Debug for MessageError<M>
where
M: Display + Debug,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Debug::fmt(&self.0, f)
}
}
impl<M> Display for MessageError<M>
where
M: Display + Debug,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Display::fmt(&self.0, f)
}
}
impl<M> StdError for MessageError<M> where M: Display + Debug + 'static {}
#[repr(transparent)]
struct DisplayError<M>(M);
impl<M> Debug for DisplayError<M>
where
M: Display,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Display::fmt(&self.0, f)
}
}
impl<M> Display for DisplayError<M>
where
M: Display,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Display::fmt(&self.0, f)
}
}
impl<M> StdError for DisplayError<M> where M: Display + 'static {}
impl<E> ErrorImpl<E> {
fn erase(&self) -> &ErrorImpl<()> {
// Erase the concrete type of E but preserve the vtable in self.vtable

View File

@ -175,6 +175,7 @@ mod error;
mod fmt;
mod kind;
mod macros;
mod wrapper;
#[cfg(not(feature = "std"))]
compile_error!("no_std support is not implemented yet");

48
src/wrapper.rs Normal file
View File

@ -0,0 +1,48 @@
use std::error::Error as StdError;
use std::fmt::{self, Debug, Display};
#[repr(transparent)]
pub struct MessageError<M>(pub M);
impl<M> Debug for MessageError<M>
where
M: Display + Debug,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Debug::fmt(&self.0, f)
}
}
impl<M> Display for MessageError<M>
where
M: Display + Debug,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Display::fmt(&self.0, f)
}
}
impl<M> StdError for MessageError<M> where M: Display + Debug + 'static {}
#[repr(transparent)]
pub struct DisplayError<M>(pub M);
impl<M> Debug for DisplayError<M>
where
M: Display,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Display::fmt(&self.0, f)
}
}
impl<M> Display for DisplayError<M>
where
M: Display,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Display::fmt(&self.0, f)
}
}
impl<M> StdError for DisplayError<M> where M: Display + 'static {}