pub struct ConfigWatcher<T>{ /* private fields */ }Expand description
Hot-reload configuration watcher
Watches a configuration file for changes and automatically reloads it. Validates new configurations before applying them.
§Type Parameters
T- The configuration type (must implementDeserializeOwned + Clone + Send + Sync)
§Example
use mecha10::config_watcher::ConfigWatcher;
use serde::Deserialize;
#[derive(Debug, Clone, Deserialize)]
struct AppConfig {
port: u16,
debug: bool,
}
let watcher = ConfigWatcher::new("config.yaml", |config: &AppConfig| {
println!("Port changed to: {}", config.port);
})?;
// Get current config
let config = watcher.get();Implementations§
Source§impl<T> ConfigWatcher<T>
impl<T> ConfigWatcher<T>
Sourcepub fn new<P, F>(path: P, on_change: F) -> Result<Self>
pub fn new<P, F>(path: P, on_change: F) -> Result<Self>
Create a new configuration watcher
§Arguments
path- Path to configuration fileon_change- Callback invoked when configuration changes
§Returns
A new ConfigWatcher instance
§Errors
Returns error if:
- File doesn’t exist
- Initial configuration is invalid
- File watcher can’t be created
§Example
let watcher = ConfigWatcher::new("config.yaml", |config: &MyConfig| {
// Handle configuration change
println!("Config updated!");
})?;Sourcepub fn get(&self) -> T
pub fn get(&self) -> T
Get the current configuration
Returns a clone of the current configuration.
§Example
let config = watcher.get();
println!("Current rate: {}", config.rate);Sourcepub async fn get_async(&self) -> T
pub async fn get_async(&self) -> T
Get the current configuration asynchronously
Returns a clone of the current configuration.
§Example
let config = watcher.get_async().await;
println!("Current rate: {}", config.rate);Auto Trait Implementations§
impl<T> Freeze for ConfigWatcher<T>
impl<T> !RefUnwindSafe for ConfigWatcher<T>
impl<T> Send for ConfigWatcher<T>
impl<T> Sync for ConfigWatcher<T>
impl<T> Unpin for ConfigWatcher<T>
impl<T> !UnwindSafe for ConfigWatcher<T>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more