From 338babaad2189f7ff1ee088994c8c20a0646ff4d Mon Sep 17 00:00:00 2001 From: Malte Voos Date: Wed, 1 Oct 2025 00:20:10 +0200 Subject: init --- src/util/mod.rs | 3 +++ src/util/option_tracker.rs | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 src/util/mod.rs create mode 100644 src/util/option_tracker.rs (limited to 'src/util') diff --git a/src/util/mod.rs b/src/util/mod.rs new file mode 100644 index 0000000..5b0c6ac --- /dev/null +++ b/src/util/mod.rs @@ -0,0 +1,3 @@ +mod option_tracker; + +pub use option_tracker::OptionTracker; diff --git a/src/util/option_tracker.rs b/src/util/option_tracker.rs new file mode 100644 index 0000000..3c19ee5 --- /dev/null +++ b/src/util/option_tracker.rs @@ -0,0 +1,43 @@ +pub struct OptionTracker { + inner: Option, + dirty: bool, +} + +/// Tracks changes to an inner Option. Any change using `set` will cause the +/// tracker to be marked as dirty, unless both the current and new value are +/// `None`. This should be used when changes from `Some(something)` to +/// `Some(something_different)` are rare, or when comparing inner values is more +/// expensive than performing an update which will mark the tracker as clean. +impl OptionTracker { + pub fn new(inner: Option) -> Self { + Self { inner, dirty: true } + } + + pub fn get(&self) -> &Option { + &self.inner + } + + pub fn set(&mut self, value: Option) { + match (&self.inner, &value) { + (None, None) => {} + _ => self.dirty = true, + } + + self.inner = value; + } + + pub fn is_dirty(&self) -> bool { + self.dirty + } + + /// Marks the tracker as clean. + pub fn reset(&mut self) { + self.dirty = false; + } +} + +impl Default for OptionTracker { + fn default() -> Self { + Self::new(Option::default()) + } +} -- cgit 1.4.1