LexicalScope

Enum LexicalScope 

Source
pub enum LexicalScope<K, V> {
    Empty,
    Root(Box<HashMap<K, V>>),
    Nested(Rc<LexicalScope<K, V>>, Box<HashMap<K, V>>),
}
Expand description

A lexically scoped environment is essentially a hierarchical mapping of keys to values, where keys may be defined at multiple levels, but only a single definition at any point in the program is active - the definition closest to that point.

A LexicalScope starts out at the root, empty. Any number of keys may be inserted at the current scope, or a new nested scope may be entered. When entering a new nested scope, keys inserted there take precedence over the same keys in previous (higher) scopes, but do not overwrite those definitions. When exiting a scope, all definitions in that scope are discarded, and definitions from the parent scope take effect again.

When searching for keys, the search begins in the current scope, and searches upwards in the scope tree until either the root is reached and the search terminates, or the key is found in some intervening scope.

Variants§

§

Empty

An empty scope, this is the default state in which all LexicalScope start

§

Root(Box<HashMap<K, V>>)

Represents a non-empty, top-level (root) scope

§

Nested(Rc<LexicalScope<K, V>>, Box<HashMap<K, V>>)

Represents a (possibly empty) nested scope, as a tuple of the parent scope and the environment of the current scope.

Implementations§

Source§

impl<K, V> LexicalScope<K, V>

Source

pub fn is_empty(&self) -> bool

Returns true if this scope is empty

Source§

impl<K, V> LexicalScope<K, V>
where K: Clone, V: Clone,

Source

pub fn enter(&mut self)

Returns true if this scope is empty Enters a new, nested lexical scope

Source

pub fn exit(&mut self)

Exits the current lexical scope

Source§

impl<K, V> LexicalScope<K, V>
where K: Eq + Hash,

Source

pub fn insert(&mut self, k: K, v: V) -> Option<V>

Inserts a new binding in the current scope, returning a conflicting definition if one is present (i.e. the same name was already declared in the same (current) scope).

NOTE: This does not return Some if a previous definition exists in an outer scope, the new definition will shadow that one, but is not considered in conflict with it.

Source

pub fn get<Q>(&self, key: &Q) -> Option<&V>
where K: Borrow<Q>, Q: Eq + Hash + ?Sized,

Source

pub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
where K: Borrow<Q>, Q: Eq + Hash + ?Sized,

Source

pub fn get_key_value<Q>(&self, key: &Q) -> Option<(&K, &V)>
where K: Borrow<Q>, Q: Eq + Hash + ?Sized,

Source

pub fn get_key<Q>(&self, key: &Q) -> Option<&K>
where K: Borrow<Q>, Q: Eq + Hash + ?Sized,

Gets the value of the key stored in this structure by key

This is used in some cases where a field of the key contains useful metadata (such as source spans), but is not part of the eq/hash impl. This function allows you to obtain the actual key stored in the map.

Trait Implementations§

Source§

impl<K, V> Clone for LexicalScope<K, V>
where K: Clone, V: Clone,

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<K: Debug, V: Debug> Debug for LexicalScope<K, V>

Source§

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

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

impl<K, V> Default for LexicalScope<K, V>

Source§

fn default() -> Self

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

impl<K, V, Q> Index<&Q> for LexicalScope<K, V>
where K: Eq + Hash + Borrow<Q>, Q: Eq + Hash + ?Sized,

Source§

type Output = V

The returned type after indexing.
Source§

fn index(&self, key: &Q) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<K, V, Q> IndexMut<&Q> for LexicalScope<K, V>
where K: Eq + Hash + Borrow<Q>, Q: Eq + Hash + ?Sized,

Source§

fn index_mut(&mut self, key: &Q) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more

Auto Trait Implementations§

§

impl<K, V> Freeze for LexicalScope<K, V>

§

impl<K, V> RefUnwindSafe for LexicalScope<K, V>

§

impl<K, V> !Send for LexicalScope<K, V>

§

impl<K, V> !Sync for LexicalScope<K, V>

§

impl<K, V> Unpin for LexicalScope<K, V>

§

impl<K, V> UnwindSafe for LexicalScope<K, V>

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> 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, 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
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.