Skip to main content

ScopedFlags

Struct ScopedFlags 

Source
pub struct ScopedFlags { /* private fields */ }
Expand description

Flags fixed on one scope, remembering what it has already resolved.

Build one per request — Flags::for_scope, or FlagsExt::scoped_flags inside a handler — and ask it everything. Each flag is resolved at most once for as long as the view lives, so a resolver that costs a query costs it once however many times the page asks.

The memory is a memo, not a lock: two tasks checking the same unresolved flag on one clone may both run the resolver, and one of the two answers is kept. Serialising them would mean holding a lock across the resolver’s await — turning every slow resolver into a queue — to save a duplicated call that produces the same answer anyway.

It is also not a cache with a lifetime. An override written after a ScopedFlags has resolved a flag is not seen by that view; call ScopedFlags::refresh or, more usually, let the request end.

Implementations§

Source§

impl ScopedFlags

Source

pub fn scope(&self) -> &Scope

Who this view is answering for.

Source

pub fn flags(&self) -> &Flags

The registry behind the view.

Source

pub async fn active(&self, flag: &str) -> Result<bool>

Is this flag on for the view’s scope?

Source

pub async fn inactive(&self, flag: &str) -> Result<bool>

The negation.

Source

pub async fn values<I, S>(&self, flags: I) -> Result<BTreeMap<String, bool>>
where I: IntoIterator<Item = S>, S: Into<String>,

Several flags at once, in one pass.

The shape a template wants: hand it a map and let it ask by name, rather than threading a bool per feature through the view model.

let flags = Flags::new().define("a", |_| async { true }).define("b", |_| async { false });
let values = flags.for_scope(Scope::user_id(41)).values(["a", "b"]).await.unwrap();

assert!(values["a"]);
assert!(!values["b"]);

Resolved one after another rather than concurrently: a resolver that takes a database connection would otherwise take as many as there are flags on the page, all at once, which is how a feature-flag call turns into a connection-pool outage.

Source

pub async fn all(&self) -> Result<BTreeMap<String, bool>>

Every defined flag, for this scope.

Note the word: a flag that only exists as a stored override or a flags.on entry is not in the list, because nothing has told this process it exists. Use it for a debugging endpoint, not as the truth about what the installation has switched on.

Source

pub async fn any<I, S>(&self, flags: I) -> Result<bool>
where I: IntoIterator<Item = S>, S: Into<String>,

Whether any of these flags is on. Stops at the first one that is.

Source

pub fn refresh(&self)

Forget what has been resolved, so the next check asks again.

For the one case that needs it: code that writes an override and then wants to see its own change through the same view.

Source

pub fn resolved_count(&self) -> usize

How many flags this view has resolved so far. Tests use it to prove the memo exists; a debugging endpoint can show it.

Trait Implementations§

Source§

impl Clone for ScopedFlags

Source§

fn clone(&self) -> Self

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 Debug for ScopedFlags

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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

Source§

type Output = T

Should always be Self
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 = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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.