Track which config items are accessed.

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.
This commit is contained in:
Michael Killough 2017-05-16 18:05:40 +07:00
parent c0bdbfa531
commit 4d879662a9

View 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 @@ impl ConfigHelpItem {
}
}
/// 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 @@ macro_rules! create_config {
$(
pub fn $i(&self) -> $ty {
self.tracker.mark_accessed(stringify!($i));
self.$i.clone()
}
)+
@ -324,6 +351,7 @@ macro_rules! create_config {
impl Default for Config {
fn default() -> Config {
Config {
tracker: ConfigTracker::default(),
$(
$i: $def,
)+