]> git.lizzy.rs Git - rust.git/commitdiff
Track which config items are accessed.
authorMichael Killough <michaeljkillough@gmail.com>
Tue, 16 May 2017 11:05:40 +0000 (18:05 +0700)
committerMichael Killough <michaeljkillough@gmail.com>
Tue, 16 May 2017 11:05:40 +0000 (18:05 +0700)
Required by #865. This doesn't introduce any method to view which
parameters are accessed.

We record which config items are accessed even if we don't intend to
output them, as we assume it will be a relatively cheap operation.

src/config.rs

index 63c33cdf6fc14600947b814e52bdc3712637a1a5..44c40f2ac6cb4828b80b70ddf0fc6ba7584cc5d0 100644 (file)
@@ -10,6 +10,8 @@
 
 extern crate toml;
 
+use std::cell::RefCell;
+use std::collections::HashSet;
 use std::error;
 use std::result;
 
@@ -211,10 +213,34 @@ pub fn default(&self) -> &'static str {
     }
 }
 
+/// This is used by Config to track which config parameters are accessed during
+/// formatting. It uses a RefCell for interior mutability, as we don't want to
+/// require a mutable reference to Config in order to access configuration.
+#[derive(Clone, Default)]
+struct ConfigTracker {
+    set: RefCell<HashSet<&'static str>>,
+}
+
+impl ConfigTracker {
+    fn mark_accessed(&self, name: &'static str) {
+        // We don't ever expect borrowing to fail, as our use of RefCell is very
+        // simple.
+        let mut set = self.set.borrow_mut();
+        set.insert(name);
+    }
+
+    fn was_accessed(&self, name: &'static str) -> bool {
+        self.set.borrow().contains(name)
+    }
+}
+
 macro_rules! create_config {
     ($($i:ident: $ty:ty, $def:expr, $( $dstring:expr ),+ );+ $(;)*) => (
         #[derive(Deserialize, Clone)]
         pub struct Config {
+            #[serde(skip_deserializing)]
+            tracker: ConfigTracker,
+
             $($i: $ty),+
         }
 
@@ -232,6 +258,7 @@ impl Config {
 
             $(
             pub fn $i(&self) -> $ty {
+                self.tracker.mark_accessed(stringify!($i));
                 self.$i.clone()
             }
             )+
@@ -324,6 +351,7 @@ pub fn print_docs() {
         impl Default for Config {
             fn default() -> Config {
                 Config {
+                    tracker: ConfigTracker::default(),
                     $(
                         $i: $def,
                     )+