pub struct LazyTable { /* private fields */ }Expand description
Table whose entry values are produced on demand by ValueLoader closures
and memoized after their first force.
Each entry’s loader runs at most once; the first result (value or error)
is cached and returned for every later access. Metadata operations
(has/keys/len) do not force loaders, while get/del/entries and
encoding do. Implements the kernel Table contract and the
object-encoding contracts, round-tripping through its
LazyTableDescriptor citizen form. The entry
map is guarded by an RwLock, so a LazyTable is shareable and mutable
through a shared reference.
Implementations§
Source§impl LazyTable
impl LazyTable
Sourcepub fn with_loaders(pairs: Vec<(Symbol, ValueLoader)>) -> Self
pub fn with_loaders(pairs: Vec<(Symbol, ValueLoader)>) -> Self
Construct a lazy table whose entries are produced on demand by the given loaders.
Each loader runs at most once, on first access of its key, and its result is memoized.
§Examples
use std::sync::Arc;
use sim_kernel::{Cx, DefaultFactory, NoopEvalPolicy, Result, Symbol, Table, Value};
use sim_table_lazy::{LazyTable, ValueLoader};
let mut cx = Cx::new(Arc::new(NoopEvalPolicy), Arc::new(DefaultFactory));
let loader: ValueLoader = Arc::new(|cx: &mut Cx| cx.factory().bool(true));
let table = LazyTable::with_loaders(vec![(Symbol::new("x"), loader)]);
// Metadata does not force the loader.
assert!(table.has(&mut cx, Symbol::new("x")).unwrap());
// get forces it (once) and memoizes the result.
let forced = table.get(&mut cx, Symbol::new("x")).unwrap();
assert_eq!(forced, table.get(&mut cx, Symbol::new("x")).unwrap());Sourcepub fn with_entries(entries: Vec<(Symbol, Value)>) -> Self
pub fn with_entries(entries: Vec<(Symbol, Value)>) -> Self
Construct a lazy table pre-populated with already-computed values.
Each entry is stored as an eager (pre-cached) loader, so accessing it performs no further computation. Later entries with the same key overwrite earlier ones.
Sourcepub fn put_lazy(&self, key: Symbol, loader: ValueLoader)
pub fn put_lazy(&self, key: Symbol, loader: ValueLoader)
Insert (or replace) a lazily computed entry under key.
The loader runs at most once, on first access of key, and its result
is memoized.
Trait Implementations§
Source§impl Citizen for LazyTable
impl Citizen for LazyTable
Source§fn citizen_symbol() -> Symbol
fn citizen_symbol() -> Symbol
namespace/name class symbol.Source§fn citizen_version() -> u32
fn citizen_version() -> u32
Source§fn citizen_arity() -> usize
fn citizen_arity() -> usize
Source§fn citizen_fields() -> &'static [&'static str]
fn citizen_fields() -> &'static [&'static str]
Source§impl Object for LazyTable
impl Object for LazyTable
Source§fn display(&self, _cx: &mut Cx) -> Result<String>
fn display(&self, _cx: &mut Cx) -> Result<String>
Source§fn header(&self) -> &ObjectHeader
fn header(&self) -> &ObjectHeader
Source§fn op(&self, _key: &OpKey) -> Option<&dyn Op>
fn op(&self, _key: &OpKey) -> Option<&dyn Op>
key, if any.Source§impl ObjectCompat for LazyTable
impl ObjectCompat for LazyTable
Source§fn class(&self, cx: &mut Cx) -> Result<ClassRef>
fn class(&self, cx: &mut Cx) -> Result<ClassRef>
Source§fn as_expr(&self, cx: &mut Cx) -> Result<Expr>
fn as_expr(&self, cx: &mut Cx) -> Result<Expr>
Source§fn as_table_impl(&self) -> Option<&dyn Table>
fn as_table_impl(&self) -> Option<&dyn Table>
Source§fn as_object_encoder(&self) -> Option<&dyn ObjectEncode>
fn as_object_encoder(&self) -> Option<&dyn ObjectEncode>
Source§fn as_callable(&self) -> Option<&dyn Callable>
fn as_callable(&self) -> Option<&dyn Callable>
Source§fn as_read_constructor(&self) -> Option<&dyn ReadConstructor>
fn as_read_constructor(&self) -> Option<&dyn ReadConstructor>
Source§fn as_number_domain(&self) -> Option<&(dyn NumberDomain + 'static)>
fn as_number_domain(&self) -> Option<&(dyn NumberDomain + 'static)>
Source§fn as_number_value(&self) -> Option<&dyn NumberValue>
fn as_number_value(&self) -> Option<&dyn NumberValue>
Source§fn as_eval_fabric(&self) -> Option<&dyn EvalFabric>
fn as_eval_fabric(&self) -> Option<&dyn EvalFabric>
Source§fn as_sequence(&self) -> Option<&dyn Sequence>
fn as_sequence(&self) -> Option<&dyn Sequence>
Source§fn as_list(&self) -> Option<&(dyn ListValue + 'static)>
fn as_list(&self) -> Option<&(dyn ListValue + 'static)>
Source§fn as_dir(&self) -> Option<&(dyn Dir + 'static)>
fn as_dir(&self) -> Option<&(dyn Dir + 'static)>
Source§impl ObjectEncode for LazyTable
impl ObjectEncode for LazyTable
Source§fn object_encoding(&self, cx: &mut Cx) -> Result<ObjectEncoding>
fn object_encoding(&self, cx: &mut Cx) -> Result<ObjectEncoding>
ObjectEncoding this object should be rendered as.Source§impl Table for LazyTable
impl Table for LazyTable
Source§fn backend_symbol(&self) -> Symbol
fn backend_symbol(&self) -> Symbol
Source§fn get(&self, cx: &mut Cx, key: Symbol) -> Result<Value>
fn get(&self, cx: &mut Cx, key: Symbol) -> Result<Value>
key, returning nil when absent.Source§fn set(&self, _cx: &mut Cx, key: Symbol, value: Value) -> Result<()>
fn set(&self, _cx: &mut Cx, key: Symbol, value: Value) -> Result<()>
key.Source§fn del(&self, cx: &mut Cx, key: Symbol) -> Result<Value>
fn del(&self, cx: &mut Cx, key: Symbol) -> Result<Value>
key, returning its prior value or nil.