Bind statement to source buffer lifetime to prevent misuse

next
Sayan Nandan 2 years ago
parent 55b506aff9
commit 2c69f7509f
No known key found for this signature in database
GPG Key ID: 8BC07A0A4D41DD52

@ -30,6 +30,7 @@ use {
lexer::{Keyword, Lexer, Token, Type, TypeExpression},
RawSlice,
},
crate::util::Life,
core::{marker::PhantomData, mem::transmute, ptr},
};
@ -197,9 +198,9 @@ impl<'a> Compiler<'a> {
impl<'a> Compiler<'a> {
#[inline(always)]
/// Compile the given BlueQL source
pub fn compile(src: &[u8]) -> LangResult<Statement> {
pub fn compile(src: &'a [u8]) -> LangResult<Life<'a, Statement>> {
let tokens = Lexer::lex(src)?;
Self::new(&tokens).eval()
Self::new(&tokens).eval().map(Life::new)
}
#[inline(always)]
pub const fn new(tokens: &[Token]) -> Self {

@ -146,7 +146,11 @@ impl<T: Clone> Clone for Wrapper<T> {
/// a compiler error, it is always good to do so to avoid misuse of the previously mentioned
/// fat pointers. This is exactly what this type does. It binds a context-dependent lifetime
/// to some type which preferably has no other lifetime (something like an `UnsafeSlice`, for
/// example)
/// example).
///
/// How do you access this? Always consider using [`AsRef::as_ref`] to get a ref to the inner
/// type and then do whatever you like. Move semantics to the inner type are prohibited (and
/// marked unsafe)
///
/// ## Important notes
/// - lifetimes are context captured by the compiler. so if this doesn't work, we'll need
@ -170,6 +174,12 @@ impl<'a, T> Life<'a, T> {
_lt: PhantomData,
}
}
/// Get the inner value
/// # Safety
/// The caller must ensure that the returned value outlives the proposed lifetime
pub unsafe fn into_inner(self) -> T {
self.v
}
}
impl<'a, T> From<T> for Life<'a, T> {

Loading…
Cancel
Save