pub struct SettingsRegistry { /* private fields */ }Expand description
Registry mapping a canonical settings path to the live Reloadable
handle that owns it, so a file-watcher event naming that path can be
dispatched to the right in-memory state.
Clone is cheap (an Rc bump) — every clone shares the same
underlying map, matching the rest of this crate’s handle types.
Holds only Weak references: see the module docs’ “the registry”
section for the full ownership contract.
Implementations§
Source§impl SettingsRegistry
impl SettingsRegistry
Sourcepub fn register(&self, reloadable: Rc<dyn Reloadable>) -> Rc<dyn Reloadable>
pub fn register(&self, reloadable: Rc<dyn Reloadable>) -> Rc<dyn Reloadable>
Register reloadable under its canonical path and return it back
unchanged, so a caller can register and retain in one expression:
use teksilo_settings::{SettingsRegistry, SettingsFile, Migrator, Versioned};
use serde::{Serialize, Deserialize};
use std::rc::Rc;
#[derive(Serialize, Deserialize, Default, Clone, PartialEq)]
struct Prefs { version: u32 }
impl Versioned for Prefs {
const CURRENT_VERSION: u32 = 1;
fn version(&self) -> u32 { self.version }
fn set_version(&mut self, v: u32) { self.version = v; }
}
let dir = tempfile::tempdir().unwrap();
let file: SettingsFile<Prefs> =
SettingsFile::load(dir.path().join("prefs.toml"), Migrator::new()).unwrap();
let registry = SettingsRegistry::new();
// Keep `handle` alive for as long as reload should keep working.
let handle = registry.register(Rc::new(file.clone()));
drop(handle); // dropping it deregisters: no leak, no dangling call.The caller is responsible for keeping the returned Rc alive —
only a Weak is retained internally, by design (see the module
docs). Registering a second Reloadable under the same canonical
path replaces the first entry.
Sourcepub fn dispatch(&self, changed_path: &Path) -> Result<bool, SettingsFileError>
pub fn dispatch(&self, changed_path: &Path) -> Result<bool, SettingsFileError>
Look up changed_path’s registered owner and call
Reloadable::reload_from_disk on it.
Returns Ok(true) if the owner’s in-memory state actually
changed, Ok(false) if nothing needed to change (including: the
path names nothing registered, or its owner has been dropped —
in the latter case the dead entry is pruned from the map so it
doesn’t accumulate forever).
Sourcepub fn registered_paths(&self) -> Vec<PathBuf>
pub fn registered_paths(&self) -> Vec<PathBuf>
The canonical paths currently registered (including entries whose
owner has since been dropped but not yet pruned by a dispatch
call). Exposed for tests and diagnostics.
Sourcepub fn live_count(&self) -> usize
pub fn live_count(&self) -> usize
Number of live (upgradeable) entries. Exposed for tests.
Trait Implementations§
Source§impl Clone for SettingsRegistry
impl Clone for SettingsRegistry
Source§fn clone(&self) -> SettingsRegistry
fn clone(&self) -> SettingsRegistry
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more