2 releases (1 stable)

1.0.0 Oct 12, 2021
0.1.0 Mar 12, 2019

#143 in Configuration

Download history 3731/week @ 2024-08-03 4046/week @ 2024-08-10 3362/week @ 2024-08-17 4163/week @ 2024-08-24 4286/week @ 2024-08-31 4380/week @ 2024-09-07 3861/week @ 2024-09-14 3940/week @ 2024-09-21 6314/week @ 2024-09-28 4435/week @ 2024-10-05 4101/week @ 2024-10-12 4536/week @ 2024-10-19 5205/week @ 2024-10-26 5020/week @ 2024-11-02 3272/week @ 2024-11-09 4105/week @ 2024-11-16

18,116 downloads per month
Used in 71 crates (10 directly)

MIT license

27KB
296 lines

fluid-let

Build Status Rust Documentation Latest Version

fluid-let implements dynamically scoped variables.

Dynamic or fluid variables are a handy way to define global configuration values. They come from the Lisp family of languages where they are relatively popular for this use case.

Usage

Add this to your Cargo.toml:

[dependencies]
fluid-let = "1"

You can declare global dynamic variables using fluid_let! macro. Suppose you want to have a configurable Debug implementation for your hashes, controlling whether to print out the whole hash or a truncated version:

use fluid_let::fluid_let;

fluid_let!(pub static DEBUG_FULL_HASH: bool);

Enable full print out using the fluid_set! macro. Assignments to dynamic variables are effective for a certain dynamic scope. In this case, while the function is being executed:

use fluid_let::fluid_set;

fn some_important_function() {
    fluid_set!(DEBUG_FULL_HASH, &true);

    // Hashes will be printed out with full precision in this function
    // as well as in all functions that it calls.
}

And here is how you can implement Debug that uses dynamic configuration:

impl fmt::Debug for Hash {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let full = DEBUG_FULL_HASH.copied().unwrap_or(false);

        write!(f, "Hash(")?;
        if full {
            for byte in &self.value {
                write!(f, "{:02X}", byte)?;
            }
        } else {
            for byte in &self.value[..4] {
                write!(f, "{:02X}", byte)?;
            }
            write!(f, "...")?;
        }
        write!(f, ")")
    }
}

Here we print either the full value of the hash, or a truncated version, based on whether debugging mode has been enabled by the caller or not.

License

The code is licensed under MIT license (see LICENSE).

No runtime deps

Features