Skip to content
This repository has been archived by the owner on Jul 2, 2024. It is now read-only.

Commit

Permalink
Optimize Error
Browse files Browse the repository at this point in the history
  • Loading branch information
SanmerDev committed Jun 15, 2024
1 parent 3c9336e commit 55992ee
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@ pub struct Error {
}

impl Error {
pub(crate) fn msg<T: fmt::Display>(msg: T) -> Self {
pub(crate) fn msg(msg: String) -> Self {
Error {
err: Box::new(ErrorImpl {
msg: Box::new(msg.to_string()),
msg: msg.into_boxed_str(),
}),
}
}
}

struct ErrorImpl {
msg: Box<dyn fmt::Display>,
msg: Box<str>,
}

impl fmt::Debug for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&self.err, f)
self.err.fmt(f)
}
}

Expand All @@ -37,7 +37,7 @@ impl fmt::Display for Error {

impl fmt::Debug for ErrorImpl {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(&self.msg.to_string())
self.msg.fmt(f)
}
}

Expand All @@ -51,12 +51,12 @@ impl serde::de::StdError for Error {}

impl serde::de::Error for Error {
fn custom<T: fmt::Display>(msg: T) -> Self {
Error::msg(msg)
Error::msg(msg.to_string())
}
}

impl serde::ser::Error for Error {
fn custom<T: fmt::Display>(msg: T) -> Self {
Error::msg(msg)
Error::msg(msg.to_string())
}
}

0 comments on commit 55992ee

Please sign in to comment.