https://github.com/ilammy/fluid-let
Dynamically scoped variables in Rust
https://github.com/ilammy/fluid-let
dynamic-variable library lisp rust
Last synced: 3 days ago
JSON representation
Dynamically scoped variables in Rust
- Host: GitHub
- URL: https://github.com/ilammy/fluid-let
- Owner: ilammy
- License: mit
- Created: 2019-03-10T23:27:18.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2021-10-12T13:07:43.000Z (over 3 years ago)
- Last Synced: 2025-04-27T18:41:33.401Z (5 days ago)
- Topics: dynamic-variable, library, lisp, rust
- Language: Rust
- Homepage:
- Size: 57.6 KB
- Stars: 11
- Watchers: 2
- Forks: 0
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
fluid-let
=========[](https://github.com/ilammy/fluid-let/actions)
[](https://docs.rs/fluid-let)
[](https://crates.io/crates/fluid-let)[**fluid-let**](https://crates.io/crates/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:
```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:```rust
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:```rust
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:
```rust
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](LICENSE)).