Skip to main content

lexical_lifetime/
lib.rs

1use std::marker::PhantomData;
2
3/// Extends a lifetime to the end of the lexical scope.
4pub struct LexicalLifetime<'a> {
5    _marker: PhantomData<&'a ()>,
6}
7
8impl LexicalLifetime<'_> {
9    #[inline(always)]
10    pub const fn new() -> Self {
11        Self {
12            _marker: PhantomData,
13        }
14    }
15}
16
17impl Drop for LexicalLifetime<'_> {
18    #[inline(always)]
19    // This line is where the magic happens. The drop impl forces
20    // the compiler to extend the lifetime to the end of the lexical
21    // scope where the value is dropped. Without this, the lifetime is
22    // invalidated as soon as the owning value is no longer used.
23    fn drop(&mut self) {}
24}