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
impl MapEnv
Sourcepub fn with_root(self, name: Name, export: Export) -> Self
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());Sourcepub fn with_prelude(self, name: Name, ns: Namespace, export: Export) -> Self
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());Sourcepub fn with_member(
self,
container: Res,
name: Name,
ns: Namespace,
export: Export,
) -> Self
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 Env for MapEnv
impl Env for MapEnv
Source§fn root(&self, name: Name) -> Option<Export>
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>
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>
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),
)
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))
fn for_each_root(&self, f: &mut dyn FnMut(Name))
Calls
f for every root name, for suggestions.Auto Trait Implementations§
impl Freeze for MapEnv
impl RefUnwindSafe for MapEnv
impl Send for MapEnv
impl Sync for MapEnv
impl Unpin for MapEnv
impl UnsafeUnpin for MapEnv
impl UnwindSafe for MapEnv
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