Skip to main content

ConfigCell

Struct ConfigCell 

Source
pub struct ConfigCell<T> { /* private fields */ }
Expand description

Holds the current configuration snapshot for one type.

ConfigCell::new() is const, so this lives in a static — which is how #[dynamic_config] emits it.

Reads are lock-free. load clones an Arc out of an ArcSwap, so a reload never blocks a request handler and a reader that already holds an Arc keeps observing its own generation until it drops it. Call it once per unit of work: calling it twice within one request can straddle a reload and observe two different configurations.

§Example

use dynamic_config::ConfigCell;

static PORT: ConfigCell<u16> = ConfigCell::new();

assert!(PORT.load().is_none());

PORT.store(8080);
assert_eq!(*PORT.load().unwrap(), 8080);

Implementations§

Source§

impl<T> ConfigCell<T>

Source

pub const fn new() -> Self

Available on non-loom only.

An empty cell.

Source

pub fn store(&self, value: T)

Atomically installs value as the current snapshot.

Reload callbacks run, and with the async feature every waiting task is woken. Installing the first snapshot is not a reload, so on_reload callbacks do not fire for it — there is nothing to compare against. on_reload_with does fire, with previous: None, which is the difference between having somewhere to say that and not.

The install is recorded as ReloadReason::Manual: something in the program stored it. Use store_with where more is known.

Source

pub fn store_with(&self, value: T, reason: ReloadReason) -> Arc<T>

store, stating why — and handing back what it installed.

The reason travels from the call site that knows it — the watcher knows the file, init knows it is the first — to the hooks and to status. Nothing downstream can reconstruct it: by the time a hook runs, every install is the same swap.

The returned Arc is this call’s snapshot, not whatever is current when it returns: a reload landing a moment later would make a following load answer differently, and the caller that installed a configuration means the one it installed. It costs nothing — the Arc was allocated here anyway — and ignoring it is the ordinary case.

Source

pub fn record_failure(&self, error: &Error)

Records a reload that installed nothing.

Called by whatever decided not to install — a load that failed, a validation that refused — so that status can answer did the last attempt work and how many have failed since one did. The next successful install resets the streak.

Only the failure’s category and key path are kept; see FailureStatus.

Source

pub fn status(&self) -> ConfigStatus

What is true of this configuration right now.

A handful of atomic loads and no I/O — nothing is re-read, nothing is recomputed — so an exporter can call it per scrape. See ConfigStatus for what it deliberately does not carry.

Source

pub fn on_reload(&self, hook: impl Fn(&Arc<T>, &Arc<T>) + Send + Sync + 'static)

Registers a callback for every later reload.

The callback receives the outgoing and incoming snapshots, in that order, and runs on whichever thread performed the reload — the watcher thread, usually. Keep it short, and do not store again from inside one: that recurses rather than deadlocking, which is worse.

Callbacks registered this way cannot be removed — a hook for the life of the process, which is what a server wants. Anything with a shorter life — a test, a plugin, a subsystem that can be torn down — should use on_reload_scoped and hold the guard.

A hook that panics is caught, reported, and skipped for that reload; the remaining hooks still run and the watcher thread survives. It is not unregistered — a bug in a hook should be loud on every reload, not once.

§Concurrent reloads

Each call sees a consistent (previous, current) pair: both were installed, and current was installed after previous.

The order of calls is not defined when two reloads overlap. Two hooks may observe the same pair, and one hook may see (A, B) after another saw (B, C). A hook that needs a total order should read generation — which is monotonic — rather than infer one from its arguments.

Reloads are deliberately not serialised against each other. The same store that dispatches these hooks wakes async waiters before running them, so that one slow callback cannot delay every reader; a lock held across user callbacks would undo that on purpose, and a hook that blocked would then block reloads.

Source

pub fn on_reload_with( &self, hook: impl Fn(&ReloadEvent<T>) + Send + Sync + 'static, )

on_reload, told why.

The callback receives a ReloadEvent: both snapshots, the ReloadReason, and the SnapshotMeta of the install. Everything the pair form promises holds here — same list, same order of registration, same panic isolation, same absence of an order across overlapping reloads — with one difference the pair form’s signature makes impossible: this fires for the first install too, with previous: None. A hook that only wants reloads matches on that, or registers through on_reload.

use dynamic_config::{ConfigCell, ReloadReason};

static PORT: ConfigCell<u16> = ConfigCell::new();

PORT.on_reload_with(|event| {
    if let ReloadReason::FileChanged(path) = &event.reason {
        println!("generation {} came from {}", event.meta.generation, path.display());
    }
});
Source

pub fn on_reload_scoped( &'static self, hook: impl Fn(&Arc<T>, &Arc<T>) + Send + Sync + 'static, ) -> HookGuard<T>

on_reload, scoped: dropping the returned guard unregisters the hook.

For anything whose life is shorter than the process — the permanent variant would keep a torn-down subsystem’s callback firing forever.

The same concurrency contract as on_reload: a consistent pair every call, in no defined order across overlapping reloads.

Source

pub fn on_reload_with_scoped( &'static self, hook: impl Fn(&ReloadEvent<T>) + Send + Sync + 'static, ) -> HookGuard<T>

on_reload_with, scoped: dropping the returned guard unregisters the hook.

Source

pub fn load(&self) -> Option<Arc<T>>

The current snapshot, or None if nothing has been stored yet.

Source

pub fn generation(&self) -> u64

Installs since the process started; zero before the first.

Monotonic, so it is the number a reload hook should read when it needs a total order — on_reload does not define one across overlapping reloads.

Source

pub fn meta(&self) -> Option<SnapshotMeta>

What is true of the installed snapshot, or None before the first.

A load of its own: load is untouched by this and stays one atomic load, which means the value and its metadata can be one install apart. See SnapshotMeta.

Source

pub fn get_or_panic(&self, type_name: &str) -> Arc<T>

The current snapshot, panicking if there is none.

type_name is used to build the message; the generated code passes the annotated struct’s name so the panic names the type the caller wrote.

§Panics

If nothing has been stored yet.

Source

pub fn changes(&'static self) -> Changes<T>
where T: Send + Sync,

Available on crate feature async only.

A handle woken by every later store.

The snapshot current at this call counts as already seen, so the first changed() waits for the next store. Read the value you start from with load.

Runtime-agnostic: it is a Future, and any executor drives it.

Trait Implementations§

Source§

impl<T: Debug> Debug for ConfigCell<T>

Source§

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

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

impl<T> Default for ConfigCell<T>

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<T> !Freeze for ConfigCell<T>

§

impl<T> !RefUnwindSafe for ConfigCell<T>

§

impl<T> !UnwindSafe for ConfigCell<T>

§

impl<T> Send for ConfigCell<T>
where T: Sync + Send,

§

impl<T> Sync for ConfigCell<T>
where T: Sync + Send,

§

impl<T> Unpin for ConfigCell<T>

§

impl<T> UnsafeUnpin for ConfigCell<T>

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> Paint for T
where T: ?Sized,

Source§

fn fg(&self, value: Color) -> Painted<&T>

Returns a styled value derived from self with the foreground set to value.

This method should be used rarely. Instead, prefer to use color-specific builder methods like red() and green(), which have the same functionality but are pithier.

§Example

Set foreground color to white using fg():

use yansi::{Paint, Color};

painted.fg(Color::White);

Set foreground color to white using white().

use yansi::Paint;

painted.white();
Source§

fn primary(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: Primary].

§Example
println!("{}", value.primary());
Source§

fn fixed(&self, color: u8) -> Painted<&T>

Returns self with the fg() set to [Color :: Fixed].

§Example
println!("{}", value.fixed(color));
Source§

fn rgb(&self, r: u8, g: u8, b: u8) -> Painted<&T>

Returns self with the fg() set to [Color :: Rgb].

§Example
println!("{}", value.rgb(r, g, b));
Source§

fn black(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: Black].

§Example
println!("{}", value.black());
Source§

fn red(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: Red].

§Example
println!("{}", value.red());
Source§

fn green(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: Green].

§Example
println!("{}", value.green());
Source§

fn yellow(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: Yellow].

§Example
println!("{}", value.yellow());
Source§

fn blue(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: Blue].

§Example
println!("{}", value.blue());
Source§

fn magenta(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: Magenta].

§Example
println!("{}", value.magenta());
Source§

fn cyan(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: Cyan].

§Example
println!("{}", value.cyan());
Source§

fn white(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: White].

§Example
println!("{}", value.white());
Source§

fn bright_black(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: BrightBlack].

§Example
println!("{}", value.bright_black());
Source§

fn bright_red(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: BrightRed].

§Example
println!("{}", value.bright_red());
Source§

fn bright_green(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: BrightGreen].

§Example
println!("{}", value.bright_green());
Source§

fn bright_yellow(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: BrightYellow].

§Example
println!("{}", value.bright_yellow());
Source§

fn bright_blue(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: BrightBlue].

§Example
println!("{}", value.bright_blue());
Source§

fn bright_magenta(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: BrightMagenta].

§Example
println!("{}", value.bright_magenta());
Source§

fn bright_cyan(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: BrightCyan].

§Example
println!("{}", value.bright_cyan());
Source§

fn bright_white(&self) -> Painted<&T>

Returns self with the fg() set to [Color :: BrightWhite].

§Example
println!("{}", value.bright_white());
Source§

fn bg(&self, value: Color) -> Painted<&T>

Returns a styled value derived from self with the background set to value.

This method should be used rarely. Instead, prefer to use color-specific builder methods like on_red() and on_green(), which have the same functionality but are pithier.

§Example

Set background color to red using fg():

use yansi::{Paint, Color};

painted.bg(Color::Red);

Set background color to red using on_red().

use yansi::Paint;

painted.on_red();
Source§

fn on_primary(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: Primary].

§Example
println!("{}", value.on_primary());
Source§

fn on_fixed(&self, color: u8) -> Painted<&T>

Returns self with the bg() set to [Color :: Fixed].

§Example
println!("{}", value.on_fixed(color));
Source§

fn on_rgb(&self, r: u8, g: u8, b: u8) -> Painted<&T>

Returns self with the bg() set to [Color :: Rgb].

§Example
println!("{}", value.on_rgb(r, g, b));
Source§

fn on_black(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: Black].

§Example
println!("{}", value.on_black());
Source§

fn on_red(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: Red].

§Example
println!("{}", value.on_red());
Source§

fn on_green(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: Green].

§Example
println!("{}", value.on_green());
Source§

fn on_yellow(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: Yellow].

§Example
println!("{}", value.on_yellow());
Source§

fn on_blue(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: Blue].

§Example
println!("{}", value.on_blue());
Source§

fn on_magenta(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: Magenta].

§Example
println!("{}", value.on_magenta());
Source§

fn on_cyan(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: Cyan].

§Example
println!("{}", value.on_cyan());
Source§

fn on_white(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: White].

§Example
println!("{}", value.on_white());
Source§

fn on_bright_black(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: BrightBlack].

§Example
println!("{}", value.on_bright_black());
Source§

fn on_bright_red(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: BrightRed].

§Example
println!("{}", value.on_bright_red());
Source§

fn on_bright_green(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: BrightGreen].

§Example
println!("{}", value.on_bright_green());
Source§

fn on_bright_yellow(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: BrightYellow].

§Example
println!("{}", value.on_bright_yellow());
Source§

fn on_bright_blue(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: BrightBlue].

§Example
println!("{}", value.on_bright_blue());
Source§

fn on_bright_magenta(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: BrightMagenta].

§Example
println!("{}", value.on_bright_magenta());
Source§

fn on_bright_cyan(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: BrightCyan].

§Example
println!("{}", value.on_bright_cyan());
Source§

fn on_bright_white(&self) -> Painted<&T>

Returns self with the bg() set to [Color :: BrightWhite].

§Example
println!("{}", value.on_bright_white());
Source§

fn attr(&self, value: Attribute) -> Painted<&T>

Enables the styling Attribute value.

This method should be used rarely. Instead, prefer to use attribute-specific builder methods like bold() and underline(), which have the same functionality but are pithier.

§Example

Make text bold using attr():

use yansi::{Paint, Attribute};

painted.attr(Attribute::Bold);

Make text bold using using bold().

use yansi::Paint;

painted.bold();
Source§

fn bold(&self) -> Painted<&T>

Returns self with the attr() set to [Attribute :: Bold].

§Example
println!("{}", value.bold());
Source§

fn dim(&self) -> Painted<&T>

Returns self with the attr() set to [Attribute :: Dim].

§Example
println!("{}", value.dim());
Source§

fn italic(&self) -> Painted<&T>

Returns self with the attr() set to [Attribute :: Italic].

§Example
println!("{}", value.italic());
Source§

fn underline(&self) -> Painted<&T>

Returns self with the attr() set to [Attribute :: Underline].

§Example
println!("{}", value.underline());

Returns self with the attr() set to [Attribute :: Blink].

§Example
println!("{}", value.blink());

Returns self with the attr() set to [Attribute :: RapidBlink].

§Example
println!("{}", value.rapid_blink());
Source§

fn invert(&self) -> Painted<&T>

Returns self with the attr() set to [Attribute :: Invert].

§Example
println!("{}", value.invert());
Source§

fn conceal(&self) -> Painted<&T>

Returns self with the attr() set to [Attribute :: Conceal].

§Example
println!("{}", value.conceal());
Source§

fn strike(&self) -> Painted<&T>

Returns self with the attr() set to [Attribute :: Strike].

§Example
println!("{}", value.strike());
Source§

fn quirk(&self, value: Quirk) -> Painted<&T>

Enables the yansi Quirk value.

This method should be used rarely. Instead, prefer to use quirk-specific builder methods like mask() and wrap(), which have the same functionality but are pithier.

§Example

Enable wrapping using .quirk():

use yansi::{Paint, Quirk};

painted.quirk(Quirk::Wrap);

Enable wrapping using wrap().

use yansi::Paint;

painted.wrap();
Source§

fn mask(&self) -> Painted<&T>

Returns self with the quirk() set to [Quirk :: Mask].

§Example
println!("{}", value.mask());
Source§

fn wrap(&self) -> Painted<&T>

Returns self with the quirk() set to [Quirk :: Wrap].

§Example
println!("{}", value.wrap());
Source§

fn linger(&self) -> Painted<&T>

Returns self with the quirk() set to [Quirk :: Linger].

§Example
println!("{}", value.linger());
Source§

fn clear(&self) -> Painted<&T>

👎Deprecated since 1.0.1:

renamed to resetting() due to conflicts with Vec::clear(). The clear() method will be removed in a future release.

Returns self with the quirk() set to [Quirk :: Clear].

§Example
println!("{}", value.clear());
Source§

fn resetting(&self) -> Painted<&T>

Returns self with the quirk() set to [Quirk :: Resetting].

§Example
println!("{}", value.resetting());
Source§

fn bright(&self) -> Painted<&T>

Returns self with the quirk() set to [Quirk :: Bright].

§Example
println!("{}", value.bright());
Source§

fn on_bright(&self) -> Painted<&T>

Returns self with the quirk() set to [Quirk :: OnBright].

§Example
println!("{}", value.on_bright());
Source§

fn whenever(&self, value: Condition) -> Painted<&T>

Conditionally enable styling based on whether the Condition value applies. Replaces any previous condition.

See the crate level docs for more details.

§Example

Enable styling painted only when both stdout and stderr are TTYs:

use yansi::{Paint, Condition};

painted.red().on_yellow().whenever(Condition::STDOUTERR_ARE_TTY);
Source§

fn new(self) -> Painted<Self>
where Self: Sized,

Create a new Painted with a default Style. Read more
Source§

fn paint<S>(&self, style: S) -> Painted<&Self>
where S: Into<Style>,

Apply a style wholesale to self. Any previous style is replaced. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more