Skip to main content

Crate guarden

Crate guarden 

Source
Expand description

guarden provides scoped guard macros for deferred cleanup and manual triggers.

The public API centers on three macros:

  • guarded! for binding a guard to a local variable that runs on Drop.
  • guard! for creating a guard value that can be triggered manually.
  • defer! as a convenience alias for guarded!.

The macros support synchronous and asynchronous bodies, explicit capture lists, and export controls for captured values.

§⚠️ Critical Usage Note: Diverging Expressions

Do not use “naked” diverging expressions—such as panic!, todo!, or loop {}—as the sole content of a sync guard closure. This prevents the compiler from distinguishing between synchronous (ASYNC = false) and asynchronous (ASYNC = true) implementations, leading to a type inference error (E0277).

§Technical Context

The ! (Never Type) is a bottom type that can be coerced into any other type. Because it satisfies both the () return type requirement for sync guards and the Future trait requirement for async guards, the compiler encounters an inference deadlock.

§Workaround

For macros like guard! or guarded!, force the closure to resolve to () by explicitly setting the guard to sync:

let val = "critical failure".to_string();
guarded! {
    sync [val] {
        panic!("{}", val);
    }
}

Modules§

guard
task

Macros§

defer
Alias for guarded!.
guard
Creates a ContextGuard object, without binding it to a variable. The macro evaluates to an expression returning the guard.
guarded
Creates a ContextGuard object, binding it to a variable within the local scope.