Ferrous API Reference

All Types / Ferrous.Guard

struct ScopeGuard [MustUse]

public ref struct ScopeGuard : IDisposable

A stack-allocated scope guard that executes a callback when disposed (exiting a using scope), unless dismissed beforehand. Equivalent to Rust's Drop / defer pattern.

Properties

public readonly bool IsActive

Gets a value indicating whether this guard is still active and will run on disposal.

Methods

public static ScopeGuard Defer(Action onDispose)

Creates a new scope guard that executes onDispose when leaving scope.

Parameters
onDispose
The cleanup action to execute.

Returns: A stack-allocated ScopeGuard.

public static ScopeGuard<TState> Defer<TState>(TState state, Action<TState> onDispose)

Creates a zero-allocation scope guard passing explicit state to a static callback, avoiding closure heap allocations.

Type Parameters
TState
The type of state forwarded to the cleanup callback.
Parameters
state
The state value passed to onDispose.
onDispose
The cleanup callback to execute (prefer a static lambda).

Returns: A stack-allocated ScopeGuard<TState>.

public void Dismiss()

Disarms the guard so that disposing it becomes a no-op (useful for commit/rollback flows).

public void Dispose()

Executes the deferred callback if the guard has not been dismissed or already disposed.