setting_tracker 0.1.3

Utility type for tracking setting changes
Documentation
  • Coverage
  • 30%
    3 out of 10 items documented0 out of 4 items with examples
  • Size
  • Source code size: 12.84 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.17 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 12s Average build duration of successful builds.
  • all releases: 12s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • osteri/setting_tracker
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • Osteri

setting_tracker

Setting tracker is a utility type for tracking changes in your settings struct. Other parts of the program are usually only interested when a specific value changes inside settings. This library tries to help achieving that by providing utility type Setting<T>.

You can wrap any type inside Setting<T> where T implements Clone and Default traits (Debug is highly recommended but not enforced).

As a made up example, first define our Settings struct (which is not part of this library):

use setting_tracker::Setting;

#[derive(Default, Debug)]
struct Settings {
    port: Setting<u16>,
    domain: Setting<String>,
}

User can now register callbacks when a specific setting changes:

let mut settings = Settings::default(); // equivalent of `Settings { port: 0, domain: "".to_string() }`
settings.port.cb(|old, new| println!("port: {:?} -> {:?}", old, new));

Setting can now be changed through the set member function:

settings.port.set(1);
settings.port.set(10);

Get the value by cloning:

let _ = settings.domain.get();

Get the value as reference, when cloning is considered too expensive:

let _ = settings.domain.as_ref();

Outputs:

port: 0 -> 1
port: 1 -> 10

Run the README.md example and see it yourself:

cargo run --example readme

Why?

This is useful pattern when individual setting change needs to be tracked. The simple example above just prints setting changes, but more useful pattern would be to use pub/sub signaling for example to other threads that a setting has been changed.

See more complex examples in example directory.