Abstractions

The options framework contains a common set traits and behaviors for numerous scenarios.

Ref is a type alias depending on which features are enabled:

  • default: options::Refstd::rc::Rc
  • async: options::Refstd::sync::Arc
  • async + di: options::Refdi::Ref

Options

pub trait Options<T> {
    fn value(&self) -> Ref<T>;
}
  • Does not support:
    • Reading of configuration data after the application has started.
  • Is registered as a Singleton and can be injected into any service lifetime when using dependency injection.

Options Snapshot

pub trait OptionsSnapshot<T> {
    fn get(&self, name: Option<&str>) -> Ref<T>;
}
  • Is useful in scenarios where options should be recomputed on every request.
  • Is registered as Scoped and therefore can't be injected into a Singleton service when using dependency injection.

Options Monitor

pub trait OptionsMonitor<T> {
    fn current_value(&self) -> Ref<T>;
    fn get(&self, name: Option<&str>) -> Ref<T>;
    fn on_change(
        &self,
        listener: Box<dyn Fn(Option<&str>, Ref<T>) + Send + Sync>) -> Subscription<T>;
}
  • Is used to retrieve options and manage options notifications for T instances.
  • Is registered as a Singleton and can be injected into any service lifetime when using dependency injection.
  • Supports:
    • Change notifications
    • Reloadable configuration
    • Selective options invalidation (OptionsMonitorCache)

Options Monitor Cache

pub trait OptionsMonitorCache<T> {
    fn get_or_add(
        &self,
        name: Option<&str>,
        create_options: &dyn Fn(Option<&str>) -> T) -> Ref<T>;
    fn try_add(&self, name: Option<&str>, options: T) -> bool;
    fn try_remove(&self, name: Option<&str>) -> bool;
    fn clear(&self);
}
  • A cache of T instances.
  • Handles invaliding monitored instances when underlying changes occur.

Configure Options

pub trait ConfigureOptions<T> {
    fn configure(&self, name: Option<&str>, options: &mut T);
}
  • Configures options when they are being instantiated.

Post-Configure Options

pub trait PostConfigureOptions<T> {
    fn post_configure(&self, name: Option<&str>, options: &mut T);
}
  • Configures options after they have been instantiated.
  • Enable setting or changing options after all ConfigureOptions configuration occurs.

Validate Options

pub trait ValidateOptions<T> {
    fn validate(&self, name: Option<&str>, options: &T) -> ValidateOptionsResult;
}
  • Validates options after they have been instantiated and configured.

Options Factory

pub trait OptionsFactory<T> {
    fn create(&self, name: Option<&str>) -> Result<T, ValidateOptionsResult>;
}
  • Responsible for creating new options instances.
  • The default implementation run all configured instance of:
    • ConfigureOptions
    • PostConfigureOptions
    • ValidateOptions