Open AZWN opened 2 months ago
The regular 'scope ownership'/permission-to-extend is rather similar to Rust's scoping/lifetime rules.
Perhaps, we can leverage the Rust type system to close scopes automatically.
This could look as follows:
struct ScopeRef(u64) struct ScopeExt<LABEL> { ref: &ScopeRef, lbl: &LABEL, _sg: &ScopeGraph<...> }
where ScopeRef can be used for querying, but a ScopeExt is required for extension:
ScopeRef
ScopeExt
struct NewScope<...> { ref: ScopeRef, ext: Vec<ScopeExt> } impl ScopeGraph<...> { pub fn add_scope(&self, data: &DATA, open_edges: &[LABEL]) -> NewScope { ... } pub fn get_edges(&self, scope: &ScopeRef, lbl: &LABEL) -> impl Iterator<Item = &ScopeRef> { ... } pub fn add_edge(&self, ext: &ScopeExt, tgt: &ScopeRef) { ... } }
The NewScope type carries all information of the new scope:
NewScope
ref
open_edges
The trick now is the following. We can implement a custom Drop for ScopeExt:
Drop
impl Drop for ScopeExt<...> { fn drop(&mut self) { self._sg.close(&self.ref, &self.lbl); } }
This ensures that a scope is closed when the ScopeExt is deallocated.
Perhaps, it would be better to wrap the API in a macro that unpacks the ScopeExt instances:
let (scope, ext_lex, ext_def) = new_scope!(sg, data, Lbl::Lexical, Lbl::Definition);
The regular 'scope ownership'/permission-to-extend is rather similar to Rust's scoping/lifetime rules.
Perhaps, we can leverage the Rust type system to close scopes automatically.
This could look as follows:
where
ScopeRef
can be used for querying, but aScopeExt
is required for extension:The
NewScope
type carries all information of the new scope:ref
)open_edges
, aScopeExt
entry (in the same order).The trick now is the following. We can implement a custom
Drop
forScopeExt
:This ensures that a scope is closed when the
ScopeExt
is deallocated.Macro
Perhaps, it would be better to wrap the API in a macro that unpacks the
ScopeExt
instances: