[][src]Struct illicit::Layer

pub struct Layer { /* fields omitted */ }

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

impl Layer[src]

pub fn new() -> Self[src]

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.

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

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.

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

Call child_fn with this layer as the local environment.

Trait Implementations

impl Clone for Layer[src]

impl Debug for Layer[src]

impl Default for Layer[src]

impl From<Snapshot> for Layer[src]

impl RefUnwindSafe for Layer[src]

impl UnwindSafe for Layer[src]

Auto Trait Implementations

impl !Send for Layer

impl !Sync for Layer

impl Unpin for Layer

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> AsContext for T where
    T: Debug + 'static, 
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Erased for T

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.