1

For a niche bit of code (dealing with custom allocators), I need to keep track of methods that return T that could potentially be holding on to a reference to the custom allocation.

For those references, I need to clean up differently than for T that does not contain any references (essentially, things that are completely owned).

However, I cannot find a way to check if T is 'static. I know that lifetimes aren't available at runtime, so I would like to monomorphize for T: 'static and for T: 'a where 'a is a lifetime I can give out.

I have tried this bit of code (taken from the specialization hacks)

struct Wrap<'a, T: 'a> {
    _t: PhantomData<&'a ()>,
    t: T,
}
impl<'a, T: 'a> Wrap<'a, T> {
    fn new(t: T) -> Self {
        Self {
            _t: PhantomData {},
            t,
        }
    }
}

trait NotStatify {
    fn is_static(&self) -> bool {
        false
    }
}
impl<'a, T: 'a> NotStatify for &Wrap<'a, T> {}

trait Statify {
    fn is_static(&self) -> bool {
        true
    }
}
impl<T: 'static> Statify for Wrap<'static, T> {}

#[test]
fn test_statify() {
    let g = "abc".to_string();

    dbg!((&&&Wrap::new(g.as_str())).is_static());
    dbg!((&&&Wrap::new(10)).is_static());
}

But this, unfortunately, does not work. How would I go about crafting a function that takes T and returns true/false depending on if T holds onto references?

Even if you think is an XY problem, I would still like some ideas on how to get is_static working.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Jon Kelley
  • 61
  • 4

0 Answers0