Skip to content

Commit

Permalink
impl Error for CastFailure
Browse files Browse the repository at this point in the history
  • Loading branch information
francesca64 committed Apr 8, 2021
1 parent 6cabb77 commit 43b4488
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Unreleased

# 0.1.3 (2021-04-08)

- Implement `std::error::Error` for `CastFailure`.

# 0.1.2 (2021-04-08)

- Added `try_cast` function for those who wish to live less dangerously.
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "en"
version = "0.1.2"
version = "0.1.3"
authors = ["Brainium Studios LLC"]
edition = "2018"
description = "The easiest numeric traits!"
Expand Down
15 changes: 12 additions & 3 deletions src/cast.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
use std::fmt::{Debug, Display};
use std::fmt::Debug;

#[derive(Debug)]
pub struct CastFailure<T, U: Debug> {
value: U,
_marker: std::marker::PhantomData<T>,
}

impl<T, U: Debug> Debug for CastFailure<T, U> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("CastFailure")
.field("value", &self.value)
.finish()
}
}

impl<T, U: Debug> From<U> for CastFailure<T, U> {
fn from(value: U) -> Self {
Self {
Expand All @@ -15,7 +22,7 @@ impl<T, U: Debug> From<U> for CastFailure<T, U> {
}
}

impl<T, U: Debug> Display for CastFailure<T, U> {
impl<T, U: Debug> std::fmt::Display for CastFailure<T, U> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
Expand All @@ -27,6 +34,8 @@ impl<T, U: Debug> Display for CastFailure<T, U> {
}
}

impl<T, U: Debug> std::error::Error for CastFailure<T, U> {}

impl<T, U: Debug> CastFailure<T, U> {
#[cold]
fn panic(self) -> ! {
Expand Down

0 comments on commit 43b4488

Please sign in to comment.