pub struct Parameter { /* private fields */ }Expand description
A dynamic parameter: a named fluid binding with a fallback default.
Backed by a DynamicEnv, a parameter reads the innermost dynamic binding
for its name and falls back to its default when none is active. This is the
binding organ’s surface for parameterize-style rebinding.
Implementations§
Source§impl Parameter
impl Parameter
Sourcepub fn new(name: Symbol, default: Value) -> Self
pub fn new(name: Symbol, default: Value) -> Self
Creates a parameter over a fresh DynamicEnv with the given default.
Sourcepub fn with_dynamic_env(
name: Symbol,
default: Value,
dynamic: DynamicEnv,
) -> Self
pub fn with_dynamic_env( name: Symbol, default: Value, dynamic: DynamicEnv, ) -> Self
Creates a parameter bound to an existing DynamicEnv.
Use this to share one environment across parameters that must observe each other’s frames.
Sourcepub fn get(&self) -> Result<Value>
pub fn get(&self) -> Result<Value>
Returns the current value: the innermost binding, or the default.
Sourcepub fn with_value<T>(
&self,
value: Value,
body: impl FnOnce() -> Result<T>,
) -> Result<T>
pub fn with_value<T>( &self, value: Value, body: impl FnOnce() -> Result<T>, ) -> Result<T>
Runs body with the parameter rebound to value for that dynamic extent.
The previous value is restored when body returns or unwinds.
§Examples
use std::sync::Arc;
use sim_kernel::{Cx, DefaultFactory, NoopEvalPolicy, Symbol};
use sim_lib_binding::Parameter;
let mut cx = Cx::new(Arc::new(NoopEvalPolicy), Arc::new(DefaultFactory));
let default = cx.factory().symbol(Symbol::new("default")).unwrap();
let temporary = cx.factory().symbol(Symbol::new("temporary")).unwrap();
let parameter = Parameter::new(Symbol::new("current"), default.clone());
parameter
.with_value(temporary.clone(), || {
assert_eq!(parameter.get()?, temporary);
Ok(())
})
.unwrap();
assert_eq!(parameter.get().unwrap(), default);Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for Parameter
impl !UnwindSafe for Parameter
impl Freeze for Parameter
impl Send for Parameter
impl Sync for Parameter
impl Unpin for Parameter
impl UnsafeUnpin for Parameter
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more