Last active
July 29, 2018 00:16
-
-
Save vadixidav/bd10f17658680a94a6a9bf470cf64647 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// A lexicon contains all the definitions known in a given context. | |
#[derive(Clone, Debug, Default)] | |
struct Lexicon<'a> { | |
scopes: Vec<&'a Scope>, | |
} | |
impl<'a> Lexicon<'a> { | |
/// Erases the lifetime of the Scope reference internally since | |
/// it will be forgotten when this is dropped. | |
fn with<'b>(&'b mut self, scope: &Scope) -> ScopedLex<'b, 'a> { | |
self.scopes.push(::std::mem::transmute(scope)); | |
ScopedLex { lex: self } | |
} | |
} | |
/// A `ScopedLex` scopes a `Scope` being added to a lexicon to its lifetime. | |
/// This is helpful to ensure a sanitary `Lexicon` in the engine. | |
struct ScopedLex<'a, 'b> { | |
lex: &'a mut Lexicon<'b>, | |
} | |
impl<'a, 'b> Deref for ScopedLex<'a, 'b> { | |
type Target = Lexicon<'a>; | |
fn deref(&self) -> &'a Lexicon { | |
self.lex | |
} | |
} | |
impl<'a, 'b> DerefMut for ScopedLex<'a, 'b> { | |
fn deref_mut(&mut self) -> &'a mut Lexicon { | |
use std::mem; | |
unsafe { mem::transmute(self.lex) } | |
} | |
} | |
impl<'a, 'b> Drop for ScopedLex<'a, 'b> { | |
fn drop(&mut self) { | |
self.lex.scopes.pop(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment