1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
use std::cell::{Ref, RefMut};
use std::mem::transmute;

// TODO: Parameterize output lifetime instead of making it always 'static. This would require GAT

pub unsafe fn extend_lifetime<T: ExtendableLife>(r: T) -> T::Out {
    r.extend_lifetime()
}


pub unsafe trait ExtendableLife {
    type Out;

    unsafe fn extend_lifetime(self) -> Self::Out;
}

unsafe impl<'a, T: ?Sized + 'static> ExtendableLife for &'a T {
    type Out = &'static T;
    unsafe fn extend_lifetime(self) -> Self::Out {
        transmute(self)
    }
}

unsafe impl<'a, T: ?Sized + 'static> ExtendableLife for &'a mut T {
    type Out = &'static mut T;
    unsafe fn extend_lifetime(self) -> Self::Out {
        transmute(self)
    }
}

unsafe impl<'a, T: ExtendableLife + Sized> ExtendableLife for Option<T> {
    type Out = Option<T::Out>;
    unsafe fn extend_lifetime(self) -> Self::Out {
        match self {
            None => None,
            Some(inner) => Some(inner.extend_lifetime()),
        }
    }
}

unsafe impl<'a, T: 'static> ExtendableLife for Ref<'a, T> {
    type Out = Ref<'static, T>;
    unsafe fn extend_lifetime(self) -> Self::Out {
        transmute(self)
    }
}

unsafe impl<'a, T: 'static> ExtendableLife for RefMut<'a, T> {
    type Out = RefMut<'static, T>;
    unsafe fn extend_lifetime(self) -> Self::Out {
        transmute(self)
    }
}