-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8351cd4
commit 73c5acd
Showing
27 changed files
with
270 additions
and
141 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
[package] | ||
name = "b-x" | ||
version = "1.0.0" | ||
edition = "2021" | ||
license = "MIT OR Apache-2.0" | ||
repository = "https://github.com/bearcove/loona" | ||
documentation = "https://docs.rs/b-x" | ||
readme = "README.md" | ||
description = """ | ||
The stupidest boxed error ever | ||
""" | ||
rust-version = "1.75.0" | ||
|
||
[dependencies] | ||
|
||
[features] | ||
default = [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# b-x | ||
|
||
b-x provides the stupidest boxed error ever. | ||
|
||
When you don't want `eyre`, you don't want `thiserror`, you don't want `anyhow`, | ||
you want much, much less. Something that just implements `std::error::Error`. | ||
|
||
It's not even Send. You have to call `.bx()` on results and/or errors via extension | ||
traits. It's so stupid. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
use std::{error::Error as StdError, fmt}; | ||
|
||
/// The stupidest box error ever. It's not even Send. | ||
/// | ||
/// It has `From` implementations for some libstd error types, | ||
/// you can derive `From<E>` for your own error types | ||
/// with [impl_from!] | ||
pub struct BX(Box<dyn StdError>); | ||
|
||
/// A type alias where `E` defaults to `BX`. | ||
pub type Result<T, E = BX> = std::result::Result<T, E>; | ||
|
||
impl BX { | ||
/// Create a new `BX` from an `E`. | ||
pub fn from_err(e: impl StdError + 'static) -> Self { | ||
Self(e.into()) | ||
} | ||
|
||
/// Create a new `BX` from a boxed `E`. | ||
pub fn from_boxed(e: Box<dyn StdError + 'static>) -> Self { | ||
Self(e) | ||
} | ||
} | ||
|
||
pub fn box_error(e: impl StdError + 'static) -> BX { | ||
BX::from_err(e) | ||
} | ||
|
||
/// Adds `bx() -> BX` to error types | ||
pub trait BxForErrors { | ||
fn bx(self) -> BX; | ||
} | ||
|
||
impl<E: StdError + 'static> BxForErrors for E { | ||
fn bx(self) -> BX { | ||
BX::from_err(self) | ||
} | ||
} | ||
|
||
/// Adds `bx() -> Result<T, BX>` to result types | ||
pub trait BxForResults<T> { | ||
fn bx(self) -> Result<T, BX>; | ||
} | ||
|
||
impl<T, E: StdError + 'static> BxForResults<T> for Result<T, E> { | ||
fn bx(self) -> Result<T, BX> { | ||
self.map_err(BX::from_err) | ||
} | ||
} | ||
|
||
impl fmt::Debug for BX { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
self.0.fmt(f) | ||
} | ||
} | ||
|
||
impl fmt::Display for BX { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
self.0.fmt(f) | ||
} | ||
} | ||
|
||
impl StdError for BX { | ||
fn source(&self) -> Option<&(dyn StdError + 'static)> { | ||
self.0.source() | ||
} | ||
} | ||
|
||
/// Implements `From<E>` for `BX` for your own type. | ||
#[macro_export] | ||
macro_rules! make_bxable { | ||
($ty:ty) => { | ||
impl From<$ty> for $crate::BX { | ||
fn from(e: $ty) -> Self { | ||
$crate::BX::from_err(e) | ||
} | ||
} | ||
}; | ||
} | ||
|
||
make_bxable!(std::io::Error); | ||
make_bxable!(std::fmt::Error); | ||
make_bxable!(std::str::Utf8Error); | ||
make_bxable!(std::string::FromUtf8Error); | ||
make_bxable!(std::string::FromUtf16Error); | ||
make_bxable!(std::num::ParseIntError); | ||
make_bxable!(std::num::ParseFloatError); | ||
make_bxable!(std::num::TryFromIntError); | ||
make_bxable!(std::array::TryFromSliceError); | ||
make_bxable!(std::char::ParseCharError); | ||
make_bxable!(std::net::AddrParseError); | ||
make_bxable!(std::time::SystemTimeError); | ||
make_bxable!(std::env::VarError); | ||
make_bxable!(std::sync::mpsc::RecvError); | ||
make_bxable!(std::sync::mpsc::TryRecvError); | ||
make_bxable!(std::sync::mpsc::SendError<Box<dyn StdError + Send + Sync>>); | ||
make_bxable!(std::sync::PoisonError<Box<dyn StdError + Send + Sync>>); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.