Skip to main content

MapEnv

Struct MapEnv 

Source
pub struct MapEnv { /* private fields */ }
Expand description

A ready-made in-memory Env: roots, a prelude, and members of outside containers, each registered once.

Lookups are O(log n); enumeration is in name order, so suggestions are deterministic.

§Examples

use hir_lang::{Name, Prim, Res};
use intern_lang::Interner;
use resolve_lang::{DefKind, Env, Export, MapEnv, Namespace};

let mut names = Interner::new();
let int = Name::new(names.intern("int"));
let env = MapEnv::new().with_prelude(int, Namespace::Type, Export::new(Res::Prim(Prim::I64), DefKind::Prim));
assert_eq!(env.prelude(int, Namespace::Type).map(|e| e.res), Some(Res::Prim(Prim::I64)));
assert!(env.prelude(int, Namespace::Value).is_none());

Implementations§

Source§

impl MapEnv

Source

pub fn new() -> Self

An empty environment.

§Examples
use resolve_lang::MapEnv;

let _env = MapEnv::new();
Source

pub fn with_root(self, name: Name, export: Export) -> Self

Adds a root name (replacing an earlier one of the same name).

§Examples
use hir_lang::{Name, Res};
use intern_lang::Interner;
use resolve_lang::{DefKind, Env, Export, MapEnv};

let mut names = Interner::new();
let std = names.intern("std");
let env = MapEnv::new().with_root(Name::new(std), Export::new(Res::Extern(std), DefKind::Extern));
assert!(env.root(Name::new(std)).is_some());
Source

pub fn with_prelude(self, name: Name, ns: Namespace, export: Export) -> Self

Adds a prelude name in one namespace.

§Examples
use hir_lang::{Name, Res};
use intern_lang::Interner;
use resolve_lang::{DefKind, Env, Export, MapEnv, Namespace};

let mut names = Interner::new();
let print = names.intern("print");
let env = MapEnv::new().with_prelude(Name::new(print), Namespace::Value, Export::new(Res::Extern(print), DefKind::Extern));
assert!(env.prelude(Name::new(print), Namespace::Value).is_some());
Source

pub fn with_member( self, container: Res, name: Name, ns: Namespace, export: Export, ) -> Self

Adds a member of an outside container (a Res::Def or Res::Extern; other resolutions are ignored).

§Examples
use hir_lang::{Name, Res};
use intern_lang::Interner;
use resolve_lang::{DefKind, Env, Export, MapEnv, Namespace};

let mut names = Interner::new();
let (os, path) = (names.intern("os"), names.intern("path"));
let env = MapEnv::new().with_member(Res::Extern(os), Name::new(path), Namespace::Module, Export::new(Res::Extern(path), DefKind::Extern));
assert!(env.member(Res::Extern(os), Name::new(path), Namespace::Module).is_some());

Trait Implementations§

Source§

impl Clone for MapEnv

Source§

fn clone(&self) -> Self

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 MapEnv

Source§

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

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

impl Default for MapEnv

Source§

fn default() -> Self

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

impl Env for MapEnv

Source§

fn root(&self, name: Name) -> Option<Export>

A name visible as the first segment of a path in every unit, after every lexical scope: another package, a precompiled unit, an extern module. Usually a DefKind::Module or DefKind::Extern.
Source§

fn prelude(&self, name: Name, ns: Namespace) -> Option<Export>

The prelude: names visible everywhere, after lexical scopes and roots (builtins, primitive type names).
Source§

fn member(&self, container: Res, name: Name, ns: Namespace) -> Option<Export>

A member of a container the program does not define: a module or class of another package, an extern module. container is the resolution the path reached so far.
Source§

fn for_each_member( &self, container: Res, f: &mut dyn FnMut(Name, Namespace, Export), )

Calls f for every member of an outside container, for glob imports and suggestions.
Source§

fn for_each_root(&self, f: &mut dyn FnMut(Name))

Calls f for every root name, for suggestions.
Source§

fn for_each_prelude(&self, f: &mut dyn FnMut(Name, Namespace))

Calls f for every prelude name, for suggestions.

Auto Trait Implementations§

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> 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 = !

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

fn try_from(value: U) -> Result<T, !>

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.