Skip to main content

SettingsRegistry

Struct SettingsRegistry 

Source
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

Source

pub fn new() -> Self

A fresh, empty registry.

Source

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.

Source

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).

Source

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.

Source

pub fn live_count(&self) -> usize

Number of live (upgradeable) entries. Exposed for tests.

Trait Implementations§

Source§

impl Clone for SettingsRegistry

Source§

fn clone(&self) -> SettingsRegistry

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for SettingsRegistry

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for SettingsRegistry

Source§

fn default() -> SettingsRegistry

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.