pub struct Namespace { /* private fields */ }Expand description
A named scope of symbol bindings: the core value of the namespace organ.
A namespace holds local and imported NamespaceEntry bindings, a set of
exported names, and any Diagnostics raised while building it. Bindings
are ordered by symbol so resolution and iteration are deterministic. Imports
flow from one namespace’s exports into another via
Namespace::import_from.
§Examples
use sim_kernel::Symbol;
use sim_lib_namespace::{ImportOptions, Namespace, NamespaceBindingSource};
let mut source = Namespace::package(Symbol::qualified("pkg", "sequence"));
source
.define(Symbol::new("map"), Symbol::qualified("sequence", "map.v1"))
.unwrap();
source.export(Symbol::new("map")).unwrap();
let mut user = Namespace::module(Symbol::qualified("module", "user"));
user.import_from(
&source,
&Symbol::new("map"),
ImportOptions::new().rename(Symbol::new("seq-map")),
)
.unwrap();
let entry = user.resolve(&Symbol::new("seq-map")).unwrap();
assert_eq!(entry.target(), &Symbol::qualified("sequence", "map.v1"));
assert_eq!(
entry.source(),
&NamespaceBindingSource::Import {
namespace: Symbol::qualified("pkg", "sequence"),
exported: Symbol::new("map"),
}
);Implementations§
Source§impl Namespace
impl Namespace
Sourcepub fn new(symbol: Symbol, kind: NamespaceKind) -> Self
pub fn new(symbol: Symbol, kind: NamespaceKind) -> Self
Create an empty namespace named symbol of the given kind.
Sourcepub fn kind(&self) -> NamespaceKind
pub fn kind(&self) -> NamespaceKind
Whether this namespace is a package or a module.
Sourcepub fn diagnostics(&self) -> &[Diagnostic]
pub fn diagnostics(&self) -> &[Diagnostic]
Diagnostics accumulated while building this namespace (e.g. shadow conflicts).
Sourcepub fn define(&mut self, name: Symbol, target: Symbol) -> Result<()>
pub fn define(&mut self, name: Symbol, target: Symbol) -> Result<()>
Define a local binding from name to target in this namespace.
§Errors
Returns an error and records a diagnostic if name is already bound.
Sourcepub fn export(&mut self, name: Symbol) -> Result<()>
pub fn export(&mut self, name: Symbol) -> Result<()>
Mark an existing binding name as exported so others may import it.
§Errors
Returns Error::UnknownSymbol if
name is not bound in this namespace.
Sourcepub fn import_from(
&mut self,
source: &Namespace,
exported: &Symbol,
options: ImportOptions,
) -> Result<()>
pub fn import_from( &mut self, source: &Namespace, exported: &Symbol, options: ImportOptions, ) -> Result<()>
Import an exported binding from source into this namespace.
The imported entry keeps the source’s resolution target but records an
NamespaceBindingSource::Import origin. options may rename the
binding or allow it to shadow an existing one.
§Errors
Returns an error if exported is not exported by source, or if the
destination name is already bound and shadowing was not allowed.
Sourcepub fn resolve(&self, name: &Symbol) -> Option<&NamespaceEntry>
pub fn resolve(&self, name: &Symbol) -> Option<&NamespaceEntry>
Look up the binding for name, returning None if it is unbound.
Sourcepub fn exported_entry(&self, name: &Symbol) -> Result<&NamespaceEntry>
pub fn exported_entry(&self, name: &Symbol) -> Result<&NamespaceEntry>
Look up the binding for name, requiring that it is also exported.
§Errors
Returns Error::UnknownSymbol if
name is not exported (or not bound) by this namespace.