11 releases

0.2.2 Jul 20, 2024
0.2.1 Mar 20, 2023
0.2.0 Nov 30, 2022
0.1.7 Sep 27, 2022
0.1.0 Jul 27, 2021

#1721 in Data structures

Download history 439/week @ 2024-07-04 658/week @ 2024-07-11 931/week @ 2024-07-18 1008/week @ 2024-07-25 990/week @ 2024-08-01 932/week @ 2024-08-08 655/week @ 2024-08-15 746/week @ 2024-08-22 710/week @ 2024-08-29 798/week @ 2024-09-05 1189/week @ 2024-09-12 2823/week @ 2024-09-19 1939/week @ 2024-09-26 1285/week @ 2024-10-03 1323/week @ 2024-10-10 1318/week @ 2024-10-17

6,305 downloads per month
Used in 18 crates (via tracker)

Apache-2.0 OR MIT

15KB
238 lines

Tracker - track changes to structs efficiently

Rust Tracker on crates.io Tracker on docs.rs

Tracker is a small crate that allows you to track changes to struct fields.

It implements the following methods for your struct fields:

  • get_#field_name()
    Get an immutable reference to field_name

  • get_mut_#field_name()
    Get a mutable reference to field_name. Assumes the field will be modified and marks it as changed.

  • set_#field_name(value)
    Set a value of field_name. Marks the field as changed only if the new value isn't equal with the previous value.

  • update_#field_name(fn)
    Update your field_name with a function or closure. Assumes the field will be modified and marks it as changed.

  • changed_#field_name()
    Check if value of field_name has changed.

To check for changes explicitly you can call var_name.changed(StructName::field_name()) and it will return a bool. Multiple fields can be checked with var_name.changed(StructName::field_name_1() | StructName::field_name_2()). Finally, it is possible to check for any changes at all with var_name.changed(StructName::track_all()) or its shortcut var_name.changed_any().

To reset all previous changes you can call var_name.reset().

How it works

Let's have a look at a small example.

#[tracker::track]
struct Test {
    x: u8,
    y: u64,
}

fn main() {
    let mut t = Test {
        x: 0,
        y: 0,
        // the macro generates a new variable called
        // "tracker" that stores the changes
        tracker: 0,
    };

    t.set_x(42);
    // let's check whether the change was detected
    assert!(t.changed(Test::x()));

    // reset t so we don't track old changes
    t.reset();

    t.set_x(42);
    // same value so no change
    assert!(!t.changed(Test::x()));
}

What happens behind the scenes when you call set_x() is that a bitflag is set in the tracker field of your struct:

                                        y   x
tracker: u8 = | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
set_x(42)  -> | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 |
reset()    -> | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |

As you can see this works pretty efficient. The macro expansion looks like this:

impl Test {
    pub fn get_x(&self) -> &u8 {
        &self.x
    }
    pub fn get_mut_x(&mut self) -> &mut u8 {
        self.tracker |= Self::x();
        &mut self.x
    }
    pub fn update_x<F: Fn(&mut u8)>(&mut self, f: F) {
        self.tracker |= Self::x();
        f(&mut self.x);
    }
    pub const fn x() -> u8 {
        1 << 0usize
    }
    pub fn set_x(&mut self, value: u8) {
        if self.x != value {
        self.tracker |= Self::x();
        }
        self.x = value;
    }
}

Further attributes

#[tracker::track]
struct Test {
    #[tracker::do_not_track]
    a: u8,
    #[do_not_track]
    b: u8,
    #[tracker::no_eq]
    c: u8,
    #[no_eq]
    d: u8,
}

You can mark fields as

  • do_not_track if you don't want tracker to implement anything for this field
  • no_eq if the type of the field doesn't implement PartialEq or tracker should not check for equality when calling set_#field_name(value) so that even overwriting with the same value marks the field as changed.

Dependencies

~285–740KB
~18K SLoC