Skip to main content

Env

Struct Env 

Source
pub struct Env(/* private fields */);
Expand description

Evaluation environment — flattened binding map with structural sharing.

Internally an Rc<EnvInner>, so cloning is always O(1) (refcount bump). child() clones the FxHashMap (O(1) structural sharing) instead of building a parent chain. bind() uses Rc::make_mut for copy-on-write: if the Rc is shared, only then does it clone the inner data.

Implementations§

Source§

impl Env

Source

pub fn new() -> Self

Create a root environment with no bindings.

Source

pub fn child(&self) -> Self

Create a child environment that inherits from this one.

O(1) — the FxHashMap clone is structural sharing (refcount bump on internal tree nodes), not a deep copy.

Source

pub fn with_scope(self, value: Value) -> Self

Attach a with scope to this environment.

If the value is a thunk that’s ALREADY evaluated (OnceCell cache hit), pre-populate the with-scope cache immediately. This avoids creating deferred WithIdent thunks when the fixpoint is already resolved — critical for the overlay chain where multiple stages access the same fixpoint through different with self; scopes.

Source

pub fn bind(&mut self, name: String, value: Value)

Bind a name to a value in this environment’s own scope.

Uses copy-on-write: if the inner Rc is shared, clones the inner data before mutating.

Source

pub fn bind_many(&mut self, pairs: impl IntoIterator<Item = (String, Value)>)

Bind many names in ONE copy-on-write step: a single Rc::make_mut on the inner env, then N inserts on the owned map — instead of N successive bind() calls each re-borrowing + re-make_mut-ing self.0.

Byte-identical to calling bind once per pair in the same order (same intern, same insert sequence, same final HAMT) — a byte-SAFE RedundantWrite-class optimization: it removes intermediate re-borrows, not any observable value. Consumed by pattern-lambda binding (bind_param), where an N-formal pattern otherwise pays N make_mut refcount checks.

Source

pub fn eval_file(&self) -> Option<&PathBuf>

Get the eval_file for this environment.

Source

pub fn set_eval_file(&mut self, file: Option<PathBuf>)

Set the eval_file for this environment.

Source

pub fn source_id(&self) -> u32

The source_id of the parse tree this env belongs to (0 = top level).

Source

pub fn set_source_id(&mut self, id: u32)

Set the source_id for this environment (called by eval_with_file for an imported parse tree).

Source

pub fn binding_count(&self) -> usize

Number of direct bindings in this environment (debug).

Source

pub fn binding_names_preview(&self, n: usize) -> Vec<String>

First N binding names (debug).

Source

pub fn with_scope_count(&self) -> usize

Number of with scopes (debug).

Source

pub fn lookup_lexical(&self, name: &str) -> Option<Value>

Lookup in LEXICAL scope only (no with-scopes). Used by maybe_thunk to avoid forcing with-scope fixpoints during attrset construction.

Source

pub fn lookup_lexical_sym(&self, sym: Symbol) -> Option<Value>

Lookup in LEXICAL scope only, by pre-interned Symbol — the Symbol-keyed sibling of lookup_lexical.

Probes ONLY the lexical bindings map (the first thing lookup_fast does, by the same Symbol) — never the with-chain. The ENV-RESOLVE M0 fast path uses this: a Resolution::Lexical{sym} reference probes here directly with its precomputed Symbol; on a hit the returned value is byte-identical to lookup_fast’s (same map, same Symbol); on a miss the caller falls back to today’s exact runtime path.

Source

pub fn lookup_with_cache_only(&self, name: &str) -> Option<Value>

Look up a name using ONLY with-scope caches (no forcing). Returns Some if the name is in a cached with-scope, None otherwise. Used by maybe_thunk to resolve with-scope idents without forcing fixpoints.

Source

pub fn innermost_with_scope( &self, ) -> Option<(Rc<RefCell<Option<NixAttrs>>>, Value)>

Get the innermost with-scope’s cache and value for creating WithIdent thunks. Returns None if there are no with-scopes.

Source

pub fn lookup(&self, name: &str) -> Option<Value>

Lookup matching Nix semantics:

  1. Probe the flattened binding map (single O(log32 n) lookup). Any explicit let/rec/function-arg binding wins over every with scope.
  2. If no lexical binding matched, iterate with_scopes in reverse order (innermost first). So with X; with Y; x finds x in Y if Y has it, otherwise in X.
Source

pub fn lookup_fresh(&self, name: &str) -> Option<Value>

Cache-BYPASSING with-scope lookup: force each with-scope value FRESH (through the full thunk chain) and check for name, refreshing the per-scope cache on the way. A force that errors (a mid-fixpoint blackhole or a with (throw …); … namespace) is caught and the scope skipped.

This exists ONLY for the last-ditch retry on the about-to-throw UndefinedVar path (see the WithIdent force): the normal cache-first [lookup_fast] can trust a stale mid-fixpoint PARTIAL cached for a scope (e.g. f self before makeScope merged callPackage into self) and skip it; a fresh force sees the now-completed scope. Never call this on a hot path — it re-forces every scope.

Source

pub fn lookup_fast(&self, sym: Symbol, name: &str) -> Option<Value>

Lookup by pre-interned Symbol + string name. Avoids re-interning.

Source

pub fn lookup_sym(&self, sym: Symbol) -> Option<Value>

Look up a binding by pre-interned Symbol.

Same semantics as lookup but skips the intern() call — for use when the caller has already cached the symbol (e.g. via intern_cached).

Trait Implementations§

Source§

impl Clone for Env

Source§

fn clone(&self) -> Env

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Env

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Env

Source§

fn default() -> Env

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl !RefUnwindSafe for Env

§

impl !Send for Env

§

impl !Sync for Env

§

impl !UnwindSafe for Env

§

impl Freeze for Env

§

impl Unpin for Env

§

impl UnsafeUnpin for Env

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> ArchivePointee for T

Source§

type ArchivedMetadata = ()

The archived version of the pointer metadata for this type.
Source§

fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata, ) -> <T as Pointee>::Metadata

Converts some archived metadata to the pointer metadata for itself.
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> LayoutRaw for T

Source§

fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>

Returns the layout of the type.
Source§

impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
where T: SharedNiching<N1, N2>, N1: Niching<T>, N2: Niching<T>,

Source§

unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool

Returns whether the given value has been niched. Read more
Source§

fn resolve_niched(out: Place<NichedOption<T, N1>>)

Writes data to out indicating that a T is niched.
Source§

impl<T> Pointee for T

Source§

type Metadata = ()

The metadata type for pointers and references to this type.
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more