Skip to main content

Pref

Struct Pref 

Source
pub struct Pref<T>
where T: PrefValue,
{ /* private fields */ }
Expand description

A typed, persisted, reactive setting (docs/settings.md), declared once as a static:

ⓘ
pub static SNAP: Pref<bool> = Pref::new("sketch.snap", || true);

The signal behind it is created on the UI thread on first use, seeded from the store (or the default when the key is absent or unparsable), and lives for the process. Pref is Copy, so a row or a closure takes it by value.

Implementations§

Source§

impl<T> Pref<T>
where T: PrefValue,

Source

pub const fn new(key: &'static str, default: fn() -> T) -> Pref<T>

Declare a pref under key with a default. default is a function so the static holds no value that needs Sync (|| String::new(), || Unit::Celsius).

Source

pub const fn env(self, var: &'static str) -> Pref<T>

The env-wins rule: while var is set in the process environment the stored value is ignored for this run (the signal seeds from the default), so a day launch --env run and every CI variant loop stay deterministic whatever an earlier run persisted. Writes still persist.

Source

pub fn key(&self) -> &'static str

The store key.

Source

pub fn default(&self) -> T

The default value.

Source

pub fn env_forced(&self) -> bool

Whether the env-wins variable is set for this run.

Source

pub fn signal(&self) -> Signal<T>

The signal behind the pref: hand it to a control or a binding. Created and seeded on first call.

Source

pub fn get(&self) -> T

Tracked read: subscribes the running computation.

Source

pub fn get_untracked(&self) -> T

Untracked read.

Source

pub fn set(&self, value: T)

Write: the signal, which persists (see Self::signal). An unchanged value is a no-op.

Source

pub fn reset(&self)

Back to the default: the key leaves the store.

Source

pub fn is_default(&self) -> bool

Whether the current value equals the default.

Source

pub fn watch(&self, f: impl Fn(&T) + 'static)

Run f on every later change (not on subscription), in the current reactive scope.

Trait Implementations§

Source§

impl<T> Clone for Pref<T>
where T: PrefValue,

Source§

fn clone(&self) -> Pref<T>

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<T> Copy for Pref<T>
where T: PrefValue,

Auto Trait Implementations§

§

impl<T> Freeze for Pref<T>

§

impl<T> RefUnwindSafe for Pref<T>

§

impl<T> Send for Pref<T>

§

impl<T> Sync for Pref<T>

§

impl<T> Unpin for Pref<T>

§

impl<T> UnsafeUnpin for Pref<T>

§

impl<T> UnwindSafe for Pref<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<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
§

impl<T> ErasedDestructor for T
where T: 'static,

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> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

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
Source§

impl<T> IntoReactive<T, StaticMark> for T
where T: Clone + 'static,

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.