Skip to main content

ConfigCell

Struct ConfigCell 

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

Process-wide storage for one configuration type.

Lives in a static, which is the only place a device has to put anything that outlives a function:

static SETTINGS: ConfigCell<Settings> = ConfigCell::new();

§Why a critical section

A reader has to see either the old configuration or the new one, never a mixture. On a host that is an ArcSwap; on a device without an allocator it is a few instructions with interrupts masked, which is what critical_section provides and what every embedded HAL implements.

The section is held for a clone of the value and nothing else. Keep the configuration struct small — which a device’s configuration is — and that is a memcpy with interrupts off, measured in microseconds.

Implementations§

Source§

impl<T> ConfigCell<T>

Source

pub const fn new() -> Self

An empty cell.

Source§

impl<T: Clone> ConfigCell<T>

Source

pub fn store(&self, value: T)

Installs value, replacing whatever was there.

For compiled-in defaults at start-up, and for anything that builds a configuration without parsing one.

Source

pub fn get(&self) -> Option<T>

The current configuration, or None before anything is stored.

Cloned out: there is no allocator, so there is no Arc to hand back. Call it once and reuse the value — two calls could straddle a store and let one piece of work observe two configurations.

Source

pub fn is_set(&self) -> bool

Whether anything has been stored.

Source

pub fn changes(&'static self) -> Changes<T>

Available on crate feature async only.

A handle that resolves each time the configuration is replaced.

Source§

impl<T: Clone + DeserializeOwned + Validate> ConfigCell<T>

Source

pub fn apply(&self, document: &[u8], format: Format) -> Result<(), Error>

Parses document and installs it, if it is usable.

Everything that can fail happens before anything is installed: a document that does not parse, does not fit, or does not validate leaves the previous configuration serving. That is the whole reason this is one call rather than parse-then-store.

§Errors

If the bytes are not valid in format, do not fit T, or T rejects itself.

Trait Implementations§

Source§

impl<T> 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> Send for ConfigCell<T>
where T: Send,

§

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

§

impl<T> Unpin for ConfigCell<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for ConfigCell<T>
where T: UnsafeUnpin,

§

impl<T> UnwindSafe for ConfigCell<T>
where T: UnwindSafe,

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