Skip to main content

Namespace

Struct Namespace 

Source
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

Source

pub fn package(symbol: Symbol) -> Self

Create an empty package namespace named symbol.

Source

pub fn module(symbol: Symbol) -> Self

Create an empty module namespace named symbol.

Source

pub fn new(symbol: Symbol, kind: NamespaceKind) -> Self

Create an empty namespace named symbol of the given kind.

Source

pub fn symbol(&self) -> &Symbol

The symbol that names this namespace.

Source

pub fn kind(&self) -> NamespaceKind

Whether this namespace is a package or a module.

Source

pub fn diagnostics(&self) -> &[Diagnostic]

Diagnostics accumulated while building this namespace (e.g. shadow conflicts).

Source

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.

Source

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.

Source

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.

Source

pub fn resolve(&self, name: &Symbol) -> Option<&NamespaceEntry>

Look up the binding for name, returning None if it is unbound.

Source

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.

Trait Implementations§

Source§

impl Clone for Namespace

Source§

fn clone(&self) -> Namespace

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 Namespace

Source§

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

Formats the value using the given formatter. Read more

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