[−][src]Struct illicit::Layer
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]
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.
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
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> AsContext for T where
T: Debug + 'static,
[src]
T: Debug + 'static,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> Erased for T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
fn to_owned(&self) -> T
[src]
fn clone_into(&self, target: &mut T)
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,