Skip to content

Commit

Permalink
fix: snapshot + bomb behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
indietyp committed Sep 20, 2024
1 parent a7d808e commit 562cd3f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 13 deletions.
2 changes: 1 addition & 1 deletion libs/error-stack/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ thiserror = { workspace = true }
rustc_version = { workspace = true }

[features]
default = ["std", "backtrace", "unstable", "futures"]
default = ["std", "backtrace"]

std = ["anyhow?/std"] # Enables support for `Error`
backtrace = ["std"] # Enables automatic capturing of `Backtrace`s (requires Rust 1.65+)
Expand Down
34 changes: 23 additions & 11 deletions libs/error-stack/src/sink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,27 @@ use crate::Report;
/// This runtime check complements the compile-time `#[must_use]` attribute,
/// providing a more robust mechanism to prevent `ReportSink` not being consumed.
#[derive(Debug, Default)]
enum Bomb {
enum BombState {
Panic,
#[default]
Warn,
Defused,
}

#[derive(Debug, Default)]
struct Bomb(BombState);

impl Bomb {
const fn panic() -> Self {
Self(BombState::Panic)
}

const fn warn() -> Self {
Self(BombState::Warn)
}

fn defuse(&mut self) {
*self = Self::Defused;
self.0 = BombState::Defused;
}
}

Expand All @@ -39,13 +50,13 @@ impl Drop for Bomb {
return;
}

match self {
Self::Panic => panic!("ReportSink was dropped without being consumed"),
match self.0 {
BombState::Panic => panic!("ReportSink was dropped without being consumed"),
#[allow(clippy::print_stderr)]
Self::Warn => {
BombState::Warn => {
eprintln!("ReportSink was dropped without being consumed");
}
Self::Defused => {}
BombState::Defused => {}
}
}
}
Expand Down Expand Up @@ -113,7 +124,7 @@ impl<C> ReportSink<C> {
pub const fn new() -> Self {
Self {
report: None,
bomb: Bomb::Warn,
bomb: Bomb::warn(),
}
}

Expand All @@ -123,7 +134,7 @@ impl<C> ReportSink<C> {
pub const fn new_armed() -> Self {
Self {
report: None,
bomb: Bomb::Panic,
bomb: Bomb::panic(),
}
}

Expand Down Expand Up @@ -301,7 +312,8 @@ impl<C> Try for ReportSink<C> {
}
}

fn branch(self) -> core::ops::ControlFlow<Self::Residual, Self::Output> {
fn branch(mut self) -> core::ops::ControlFlow<Self::Residual, Self::Output> {
self.bomb.defuse();
self.report.map_or(
core::ops::ControlFlow::Continue(()), //
|report| core::ops::ControlFlow::Break(Err(report)),
Expand Down Expand Up @@ -459,12 +471,12 @@ mod test {
let report = sink().expect_err("should have failed");

let contexts: BTreeSet<_> = report.current_contexts().collect();
assert_eq!(contexts.len(), 2);
assert_eq!(contexts.len(), 1);
assert!(contexts.contains(&TestError(0)));
}

#[test]
#[should_panic(expected = "must be consumed")]
#[should_panic(expected = "without being consumed")]
fn panic_on_unused() {
#[allow(clippy::unnecessary_wraps)]
fn sink() -> Result<(), Report<[TestError]>> {
Expand Down
2 changes: 1 addition & 1 deletion libs/error-stack/tests/ui/macro_invalid_args.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ error: unexpected end of macro invocation
note: while trying to match meta-variable `$err:expr`
--> src/macros.rs
|
| ($err:expr $(,)?) => {{
| ($err:expr) => {{
| ^^^^^^^^^

error: unexpected end of macro invocation
Expand Down

0 comments on commit 562cd3f

Please sign in to comment.