Skip to main content

Layer

Struct Layer 

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

A container for the local environment, usually used to represent a pending addition to it.

The environment is type-indexed, and access is provided through read-only references. Call Layer::offer to include new values in the environment for called functions and get or expect to retrieve references to the offered values.

Aside: one interesting implication of the above is the ability to define “private scoped global values” which are private to functions which are nonetheless propagating the values with their control flow. This can be useful for runtimes to offer themselves execution-local values in functions which are invoked by external code. It can also be severely abused, like any implicit state, and should be used with caution.

§Examples

Code always sees the contents of the “bottom-most” Layer:

illicit::Layer::new().offer(String::new()).offer(5u16).enter(|| {
    assert!(illicit::expect::<String>().is_empty());
    assert_eq!(*illicit::expect::<u16>(), 5);

    illicit::Layer::new().offer(10u16).enter(|| {
        assert!(illicit::expect::<String>().is_empty());
        assert_eq!(*illicit::expect::<u16>(), 10);
    });

    assert!(illicit::expect::<String>().is_empty());
    assert_eq!(*illicit::expect::<u16>(), 5);
});

Implementations§

Source§

impl Layer

Source

pub fn new() -> Self

Construct a new layer which defaults to the contents of the current one. Call Layer::offer to add values to the new layer before calling Layer::enter to run a closure with access to the new and old values.

Source

pub fn offer<E>(self, v: E) -> Self
where E: Debug + 'static,

Adds the new item and returns the modified layer.

The offered type must implement Debug to allow get’s errors to display the contents of the illicit environment. It must also satisfy the 'static lifetime because get is unable to express any lifetime constraints at its callsite.

Source

pub fn enter<R>(self, child_fn: impl FnOnce() -> R) -> R

Call child_fn with this layer as the local environment.

Trait Implementations§

Source§

impl Clone for Layer

Source§

fn clone(&self) -> Layer

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Layer

Source§

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

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

impl Default for Layer

Source§

fn default() -> Self

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

impl From<Snapshot> for Layer

Source§

fn from(snapshot: Snapshot) -> Self

Converts to this type from the input type.
Source§

impl RefUnwindSafe for Layer

Source§

impl UnwindSafe for Layer

Auto Trait Implementations§

§

impl Freeze for Layer

§

impl !Send for Layer

§

impl !Sync for Layer

§

impl Unpin for Layer

§

impl UnsafeUnpin for Layer

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> AsContext for T
where T: Debug + 'static,

Source§

fn offer<R>(self, op: impl FnOnce() -> R) -> R

Call op within the context of a Layer containing self.
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> 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.
Source§

impl<T> Erased for T