nlib 0.1.3

Nate's library. Various things or macro patterns I use to aid in more succint Rust programming.
Documentation
// Copyright 2025 Nathan Sizemore <nathanrsizemore@gmail.com>
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, you can obtain one at http://mozilla.org/MPL/2.0/.

/// RAII guard that runs a closure when dropped.
/// - Use for "finally" behavior (commit/rollback, unlock, temp file cleanup).
/// - Runs at most once; moving is fine, but only the last drop executes it.
pub struct ExecOnDrop<F: FnOnce()> {
    inner: Option<F>, // `Option` lets us take the closure so it runs only once.
}

impl<F: FnOnce()> ExecOnDrop<F> {
    /// Create a new drop guard that will execute `exec` on drop.
    pub fn new(exec: F) -> Self {
        Self { inner: Some(exec) }
    }
}

impl<F: FnOnce()> Drop for ExecOnDrop<F> {
    fn drop(&mut self) {
        // If present, take and run the closure. Subsequent drops do nothing.
        if let Some(f) = self.inner.take() {
            f();
        }
    }
}