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
impl Layer
Sourcepub fn new() -> Self
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.