[][src]Trait moore::score::NodeStorage

pub trait NodeStorage<I> {
    type Node;
    fn get(&self, id: &I) -> Option<&Self::Node>;
fn set(&mut self, id: I, node: Self::Node) -> Option<Self::Node>; }

The NodeStorage trait allows for references to nodes to be stored and retrieved via a unique node ID.

Once a node is created for example in an arena, a reference to it can be stored in a NodeStorage to associate it with an ID. If that ID is presented to the NodeStorage again, that same reference will be produced. Implementors of this trait are expected to implement it multiple times, once for each different ID/node type pair that they support. This then allows for nodes to be looked up in a type safe manner based on their ID.

The NodeStorage does not assume ownership over the nodes added to it. Therefore all nodes are references of at least the lifetime 'tn.

Example

use moore_common::score::NodeStorage;
use std::collections::HashMap;

#[derive(PartialEq, Eq, Debug)]
struct Foo;
#[derive(PartialEq, Eq, Debug)]
struct Bar;

#[derive(Clone, Copy, Hash, PartialEq, Eq, Debug)]
struct FooId(usize);
#[derive(Clone, Copy, Hash, PartialEq, Eq, Debug)]
struct BarId(usize);

struct Table<'tn> {
    foos: HashMap<FooId, &'tn Foo>,
    bars: HashMap<BarId, &'tn Bar>,
}

impl<'tn> NodeStorage<FooId> for Table<'tn> {
    type Node = &'tn Foo;
    fn get(&self, id: &FooId) -> Option<&&'tn Foo> { self.foos.get(id) }
    fn set(&mut self, id: FooId, node: &'tn Foo) -> Option<&'tn Foo> { self.foos.insert(id, node) }
}

impl<'tn> NodeStorage<BarId> for Table<'tn> {
    type Node = &'tn Bar;
    fn get(&self, id: &BarId) -> Option<&&'tn Bar> { self.bars.get(id) }
    fn set(&mut self, id: BarId, node: &'tn Bar) -> Option<&'tn Bar> { self.bars.insert(id, node) }
}

// Store node refs in table:
let foo = Foo;
let bar = Bar;
let mut tbl = Table{ foos: HashMap::new(), bars: HashMap::new() };
tbl.set(FooId(1), &foo);
tbl.set(BarId(2), &bar);

// Retrieve node refs again:
assert_eq!(tbl.get(&FooId(1)), Some(&&foo));
assert_eq!(tbl.get(&BarId(2)), Some(&&bar));
assert_eq!(tbl.get(&BarId(1)), None);
assert_eq!(tbl.get(&FooId(2)), None);

// The following would produce a compiler error due to type mismatch:
// let _: &Foo = *tbl.get(&BarId(1)).unwrap();
// let _: &Bar = *tbl.get(&FooId(2)).unwrap();

Associated Types

type Node

The type of the node that is returned when presented with an ID of type I.

Loading content...

Required methods

fn get(&self, id: &I) -> Option<&Self::Node>

Obtains a reference to the node with the given ID.

Returns None when no node with the given ID exists.

fn set(&mut self, id: I, node: Self::Node) -> Option<Self::Node>

Store a reference to a node under the given ID.

Later that reference can be retrieved again by presenting the same ID to the get function. Returns the previously stored entry, if any.

Loading content...

Implementations on Foreign Types

impl<K, V> NodeStorage<K> for BTreeMap<K, V> where
    K: Ord
[src]

type Node = V

impl<K, V> NodeStorage<K> for HashMap<K, V, RandomState> where
    K: Hash + Eq
[src]

type Node = V

impl<'ast> NodeStorage<SubtypeDeclRef> for AstTable<'ast>[src]

type Node = (ScopeRef, &'ast SubtypeDecl)

impl<'ctx> NodeStorage<LibRef> for HirTable<'ctx>[src]

type Node = &'ctx Lib

impl<'sb, 'ast, 'ctx> NodeStorage<AssertStmtRef> for LazyHirTable<'sb, 'ast, 'ctx>[src]

type Node = LazyNode<Box<dyn Fn(&'a ScoreContext<'b, 'sb, 'ast, 'ctx>) + 'sb>>

impl<'ast> NodeStorage<IntfTypeRef> for AstTable<'ast>[src]

type Node = (ScopeRef, &'ast TypeDecl)

impl<'sb, 'ast, 'ctx> NodeStorage<IfStmtRef> for LazyHirTable<'sb, 'ast, 'ctx>[src]

type Node = LazyNode<Box<dyn Fn(&'a ScoreContext<'b, 'sb, 'ast, 'ctx>) + 'sb>>

impl<'ast> NodeStorage<ArchRef> for AstTable<'ast>[src]

type Node = (LibRef, CtxItemsRef, &'ast ArchBody)

impl<'ctx> NodeStorage<CaseStmtRef> for HirTable<'ctx>[src]

type Node = &'ctx Stmt<CaseStmt>

impl<'sb, 'ast, 'ctx> NodeStorage<VarAssignStmtRef> for LazyHirTable<'sb, 'ast, 'ctx>[src]

type Node = LazyNode<Box<dyn Fn(&'a ScoreContext<'b, 'sb, 'ast, 'ctx>) + 'sb>>

impl<'ctx> NodeStorage<ReportStmtRef> for HirTable<'ctx>[src]

type Node = &'ctx Stmt<ReportStmt>

impl<'ast> NodeStorage<CfgRef> for AstTable<'ast>[src]

type Node = (LibRef, CtxItemsRef, &'ast CfgDecl)

impl<'ctx> NodeStorage<SubprogBodyRef> for HirTable<'ctx>[src]

type Node = &'ctx SubprogBody

impl<'ast> NodeStorage<PkgBodyRef> for AstTable<'ast>[src]

type Node = (ScopeRef, &'ast PkgBody)

impl<'ctx> NodeStorage<SignalDeclRef> for HirTable<'ctx>[src]

type Node = &'ctx Decl<SignalDecl>

impl<'ast> NodeStorage<TypeDeclRef> for AstTable<'ast>[src]

type Node = (ScopeRef, &'ast TypeDecl)

impl<'sb, 'ast, 'ctx> NodeStorage<ExprRef> for LazyHirTable<'sb, 'ast, 'ctx>[src]

type Node = LazyNode<Box<dyn Fn(&'a ScoreContext<'b, 'sb, 'ast, 'ctx>) + 'sb>>

impl<'sb, 'ast, 'ctx> NodeStorage<SubtypeIndRef> for LazyHirTable<'sb, 'ast, 'ctx>[src]

type Node = LazyNode<Box<dyn Fn(&'a ScoreContext<'b, 'sb, 'ast, 'ctx>) + 'sb>>

impl<'ast> NodeStorage<SigAssignStmtRef> for AstTable<'ast>[src]

type Node = (ScopeRef, &'ast Stmt)

impl<'ctx> NodeStorage<LoopStmtRef> for HirTable<'ctx>[src]

type Node = &'ctx Stmt<LoopStmt>

impl<'ctx> NodeStorage<PkgDeclRef> for HirTable<'ctx>[src]

type Node = &'ctx Package

impl<'ctx> NodeStorage<IfStmtRef> for HirTable<'ctx>[src]

type Node = &'ctx Stmt<IfStmt>

impl<'ctx> NodeStorage<SubprogInstRef> for HirTable<'ctx>[src]

type Node = &'ctx SubprogInst

impl<'sb, 'ast, 'ctx> NodeStorage<ConstDeclRef> for LazyHirTable<'sb, 'ast, 'ctx>[src]

type Node = LazyNode<Box<dyn Fn(&'a ScoreContext<'b, 'sb, 'ast, 'ctx>) + 'sb>>

impl<'ctx> NodeStorage<PkgBodyRef> for HirTable<'ctx>[src]

type Node = &'ctx PackageBody

impl<'ast> NodeStorage<ProcessStmtRef> for AstTable<'ast>[src]

type Node = (ScopeRef, &'ast Stmt)

impl<'ctx> NodeStorage<AssertStmtRef> for HirTable<'ctx>[src]

type Node = &'ctx Stmt<AssertStmt>

impl<'ast> NodeStorage<SubprogBodyRef> for AstTable<'ast>[src]

type Node = (ScopeRef, &'ast Subprog)

impl<'ctx> NodeStorage<EntityRef> for HirTable<'ctx>[src]

type Node = &'ctx Entity

impl<'ctx> NodeStorage<FileDeclRef> for HirTable<'ctx>[src]

type Node = &'ctx Decl<FileDecl>

impl<'ctx> NodeStorage<AggregateRef> for HirTable<'ctx>[src]

type Node = &'ctx Aggregate

impl<'ctx> NodeStorage<ConstDeclRef> for HirTable<'ctx>[src]

type Node = &'ctx Decl<ConstDecl>

impl<'ast> NodeStorage<AliasDeclRef> for AstTable<'ast>[src]

type Node = (ScopeRef, &'ast AliasDecl)

impl<'sb, 'ast, 'ctx> NodeStorage<NullStmtRef> for LazyHirTable<'sb, 'ast, 'ctx>[src]

type Node = LazyNode<Box<dyn Fn(&'a ScoreContext<'b, 'sb, 'ast, 'ctx>) + 'sb>>

impl<'ctx> NodeStorage<NexitStmtRef> for HirTable<'ctx>[src]

type Node = &'ctx Stmt<NexitStmt>

impl<'ctx> NodeStorage<SigAssignStmtRef> for HirTable<'ctx>[src]

type Node = &'ctx SigAssignStmt

impl<'ast> NodeStorage<IntfConstRef> for AstTable<'ast>[src]

type Node = (ScopeRef, &'ast IntfObjDecl, SubtypeIndRef, &'ast Ident)

impl<'ast> NodeStorage<CfgSpecRef> for AstTable<'ast>[src]

type Node = (ScopeRef, &'ast CfgSpec)

impl<'ast> NodeStorage<DisconSpecRef> for AstTable<'ast>[src]

type Node = (ScopeRef, &'ast DisconSpec)

impl<'ast> NodeStorage<GroupTempRef> for AstTable<'ast>[src]

type Node = (ScopeRef, &'ast GroupDecl)

impl<'ast> NodeStorage<ExprRef> for AstTable<'ast>[src]

type Node = (ScopeRef, &'ast Expr)

impl<'ast> NodeStorage<IntfSignalRef> for AstTable<'ast>[src]

type Node = (ScopeRef, &'ast IntfObjDecl, SubtypeIndRef, &'ast Ident)

impl<'ctx> NodeStorage<ProcessStmtRef> for HirTable<'ctx>[src]

type Node = &'ctx ProcessStmt

impl<'ctx> NodeStorage<LatentSubprogRef> for HirTable<'ctx>[src]

type Node = Spanned<SubprogRef>

impl<'ast> NodeStorage<LatentSubprogRef> for AstTable<'ast>[src]

type Node = (ScopeRef, LatentName<'ast>)

impl<'ctx> NodeStorage<PkgInstRef> for HirTable<'ctx>[src]

type Node = &'ctx PackageInst

impl<'sb, 'ast, 'ctx> NodeStorage<SignalDeclRef> for LazyHirTable<'sb, 'ast, 'ctx>[src]

type Node = LazyNode<Box<dyn Fn(&'a ScoreContext<'b, 'sb, 'ast, 'ctx>) + 'sb>>

impl<'ctx> NodeStorage<ReturnStmtRef> for HirTable<'ctx>[src]

type Node = &'ctx Stmt<ReturnStmt>

impl<'ctx> NodeStorage<TypeDeclRef> for HirTable<'ctx>[src]

type Node = &'ctx TypeDecl

impl<'sb, 'ast, 'ctx> NodeStorage<AggregateRef> for LazyHirTable<'sb, 'ast, 'ctx>[src]

type Node = LazyNode<Box<dyn Fn(&'a ScoreContext<'b, 'sb, 'ast, 'ctx>) + 'sb>>

impl<'ctx> NodeStorage<ArchRef> for HirTable<'ctx>[src]

type Node = &'ctx Arch

impl<'ast> NodeStorage<IntfSubprogRef> for AstTable<'ast>[src]

type Node = (ScopeRef, &'ast IntfSubprogDecl)

impl<'ctx> NodeStorage<SubprogDeclRef> for HirTable<'ctx>[src]

type Node = &'ctx Subprog

impl<'sb, 'ast, 'ctx> NodeStorage<ReturnStmtRef> for LazyHirTable<'sb, 'ast, 'ctx>[src]

type Node = LazyNode<Box<dyn Fn(&'a ScoreContext<'b, 'sb, 'ast, 'ctx>) + 'sb>>

impl<'sb, 'ast, 'ctx> NodeStorage<TypeDeclRef> for LazyHirTable<'sb, 'ast, 'ctx>[src]

type Node = LazyNode<Box<dyn Fn(&'a ScoreContext<'b, 'sb, 'ast, 'ctx>) + 'sb>>

impl<'sb, 'ast, 'ctx> NodeStorage<CallStmtRef> for LazyHirTable<'sb, 'ast, 'ctx>[src]

type Node = LazyNode<Box<dyn Fn(&'a ScoreContext<'b, 'sb, 'ast, 'ctx>) + 'sb>>

impl<'ast> NodeStorage<EntityRef> for AstTable<'ast>[src]

type Node = (LibRef, CtxItemsRef, &'ast EntityDecl)

impl<'ctx> NodeStorage<ExprRef> for HirTable<'ctx>[src]

type Node = &'ctx Expr

impl<'ast> NodeStorage<IntfPkgRef> for AstTable<'ast>[src]

type Node = (ScopeRef, &'ast PkgInst)

impl<'ctx> NodeStorage<CallStmtRef> for HirTable<'ctx>[src]

type Node = &'ctx Stmt<CallStmt>

impl<'ast> NodeStorage<SubprogInstRef> for AstTable<'ast>[src]

type Node = (ScopeRef, &'ast Subprog)

impl<'sb, 'ast, 'ctx> NodeStorage<VarDeclRef> for LazyHirTable<'sb, 'ast, 'ctx>[src]

type Node = LazyNode<Box<dyn Fn(&'a ScoreContext<'b, 'sb, 'ast, 'ctx>) + 'sb>>

impl<'ast> NodeStorage<ArrayTypeIndexRef> for AstTable<'ast>[src]

type Node = (ScopeRef, &'ast Expr)

impl<'ctx> NodeStorage<SubtypeDeclRef> for HirTable<'ctx>[src]

type Node = &'ctx SubtypeDecl

impl<'ast> NodeStorage<VarAssignStmtRef> for AstTable<'ast>[src]

type Node = (ScopeRef, &'ast Stmt)

impl<'sb, 'ast, 'ctx> NodeStorage<NexitStmtRef> for LazyHirTable<'sb, 'ast, 'ctx>[src]

type Node = LazyNode<Box<dyn Fn(&'a ScoreContext<'b, 'sb, 'ast, 'ctx>) + 'sb>>

impl<'ast> NodeStorage<AttrSpecRef> for AstTable<'ast>[src]

type Node = (ScopeRef, &'ast AttrDecl)

impl<'sb, 'ast, 'ctx> NodeStorage<FileDeclRef> for LazyHirTable<'sb, 'ast, 'ctx>[src]

type Node = LazyNode<Box<dyn Fn(&'a ScoreContext<'b, 'sb, 'ast, 'ctx>) + 'sb>>

impl<'ctx> NodeStorage<VarDeclRef> for HirTable<'ctx>[src]

type Node = &'ctx Decl<VarDecl>

impl<'sb, 'ast, 'ctx> NodeStorage<ReportStmtRef> for LazyHirTable<'sb, 'ast, 'ctx>[src]

type Node = LazyNode<Box<dyn Fn(&'a ScoreContext<'b, 'sb, 'ast, 'ctx>) + 'sb>>

impl<'ast> NodeStorage<GroupDeclRef> for AstTable<'ast>[src]

type Node = (ScopeRef, &'ast GroupDecl)

impl<'sb, 'ast, 'ctx> NodeStorage<LoopStmtRef> for LazyHirTable<'sb, 'ast, 'ctx>[src]

type Node = LazyNode<Box<dyn Fn(&'a ScoreContext<'b, 'sb, 'ast, 'ctx>) + 'sb>>

impl<'ast> NodeStorage<AttrDeclRef> for AstTable<'ast>[src]

type Node = (ScopeRef, &'ast AttrDecl)

impl<'ctx> NodeStorage<ArrayTypeIndexRef> for HirTable<'ctx>[src]

type Node = &'ctx Spanned<ArrayTypeIndex>

impl<'sb, 'ast, 'ctx> NodeStorage<CaseStmtRef> for LazyHirTable<'sb, 'ast, 'ctx>[src]

type Node = LazyNode<Box<dyn Fn(&'a ScoreContext<'b, 'sb, 'ast, 'ctx>) + 'sb>>

impl<'ctx> NodeStorage<WaitStmtRef> for HirTable<'ctx>[src]

type Node = &'ctx Stmt<WaitStmt>

impl<'ctx> NodeStorage<SubtypeIndRef> for HirTable<'ctx>[src]

type Node = &'ctx SubtypeInd

impl<'ctx> NodeStorage<IntfSignalRef> for HirTable<'ctx>[src]

type Node = &'ctx IntfSignal

impl<'sb, 'ast, 'ctx> NodeStorage<WaitStmtRef> for LazyHirTable<'sb, 'ast, 'ctx>[src]

type Node = LazyNode<Box<dyn Fn(&'a ScoreContext<'b, 'sb, 'ast, 'ctx>) + 'sb>>

impl<'ast> NodeStorage<SubprogDeclRef> for AstTable<'ast>[src]

type Node = (ScopeRef, &'ast Subprog)

impl<'ast> NodeStorage<PkgInstRef> for AstTable<'ast>[src]

type Node = (ScopeRef, &'ast PkgInst)

impl<'ast> NodeStorage<PkgDeclRef> for AstTable<'ast>[src]

type Node = (ScopeRef, &'ast PkgDecl)

impl<'ast> NodeStorage<CtxItemsRef> for AstTable<'ast>[src]

type Node = (ScopeRef, &'ast [CtxItem])

impl<'ctx> NodeStorage<LatentTypeMarkRef> for HirTable<'ctx>[src]

type Node = Spanned<TypeMarkRef>

impl<'ast> NodeStorage<LatentPkgRef> for AstTable<'ast>[src]

type Node = (ScopeRef, LatentName<'ast>)

impl<'sb, 'ast, 'ctx> NodeStorage<SigAssignStmtRef> for LazyHirTable<'sb, 'ast, 'ctx>[src]

type Node = LazyNode<Box<dyn Fn(&'a ScoreContext<'b, 'sb, 'ast, 'ctx>) + 'sb>>

impl<'ast> NodeStorage<CompDeclRef> for AstTable<'ast>[src]

type Node = (ScopeRef, &'ast CompDecl)

impl<'ast> NodeStorage<LatentTypeMarkRef> for AstTable<'ast>[src]

type Node = (ScopeRef, LatentName<'ast>)

impl<'ast> NodeStorage<CtxRef> for AstTable<'ast>[src]

type Node = (LibRef, CtxItemsRef, &'ast CtxDecl)

impl<'ctx> NodeStorage<NullStmtRef> for HirTable<'ctx>[src]

type Node = &'ctx Stmt<NullStmt>

impl<'ctx> NodeStorage<VarAssignStmtRef> for HirTable<'ctx>[src]

type Node = &'ctx Stmt<VarAssignStmt>

impl<'ctx> NodeStorage<LatentPkgRef> for HirTable<'ctx>[src]

type Node = Spanned<PkgRef>

Loading content...

Implementors

Loading content...