# Non-static data in \`static\`

**URL:** <https://internals.rust-lang.org/t/non-static-data-in-static/21247>\
**Category:** language design\
**Created:** [July 25, 2024, 11:33am UTC](https://internals.rust-lang.org/t/non-static-data-in-static/21247 "2024-07-25T11:33:45Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ddystopia](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ddystopia/32/10915_2.png) [@Ddystopia](https://internals.rust-lang.org/u/Ddystopia)\
**Post date:** [July 25, 2024, 11:33am UTC](https://internals.rust-lang.org/t/non-static-data-in-static/21247/1 "2024-07-25T11:33:45Z")

</div>

Hello,

what I basically need is to have something like `Box`, but with data living in `static` variable, not heap. But I cannot create a static variable that will hold the value. Partially because I cannot name required lifetimes, and TAIT wont help there either - statics cannot mention types from outter scope, so even if TAIT finds lifetime, it is still getting rejected. What can we do about it and should we?

Another possibility is that one day it will be possible to write an `Allocator` trait which will have `Aligned<align, [u8; size]>` as a backing buffer (`Aligned` trait is implementable in stable rust, see [elain](https://docs.rs/elain/latest/src/elain/lib.rs.html#1-323)) and give `Box<T, impl Allocator>`. I guess concrete allocator type should remain erased.

The Some code example:

```rust
use std::{mem::MaybeUninit, ptr::{addr_of_mut, drop_in_place}};

fn main() {
    struct Foo<'a>(&'a i32);
    static mut FOO: MaybeUninit<Foo<'?>> = MaybeUninit::uninit();

    unsafe {
        let num: i32 = 42;
        let foo = Foo(&num);
        let foo_uninit = &mut *addr_of_mut!(FOO);
        let foo = foo_uninit.write(foo);
        println!("{}", foo.0);
        drop_in_place(foo);
    }
}

```

TAIT feature also does not allow this - or at least not in my hands.

Maybe if there was trait `Staticalize` that would be something like that: `Staticalize<for<'a> Foo<'a>>::Static -> Foo<'static>` (and work with futures etc), then one might use it or TAIT to get job done?

---

<div class="post-metadata">

**Author:** ![chrefr](https://avatars.discourse-cdn.com/v4/letter/c/e480ec/32.png) [@chrefr](https://internals.rust-lang.org/u/chrefr)\
**Post date:** [July 25, 2024, 12:16pm UTC](https://internals.rust-lang.org/t/non-static-data-in-static/21247/2 "2024-07-25T12:16:57Z")

</div>

Well, this will require unsafe code anyway, so you can just `transmute()` to `Foo<'static>`.

---

<div class="post-metadata">

**Author:** ![Ddystopia](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ddystopia/32/10915_2.png) [@Ddystopia](https://internals.rust-lang.org/u/Ddystopia)\
**Post date:** [July 25, 2024, 12:56pm UTC](https://internals.rust-lang.org/t/non-static-data-in-static/21247/3 "2024-07-25T12:56:49Z")

</div>

What if `Foo` cannot be named? Type inference cannot understand, that `Dst` should be the same as `Src` but with `'static` bounds

---

<div class="post-metadata">

**Author:** ![kornel](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/kornel/32/2711_2.png) [@kornel](https://internals.rust-lang.org/u/kornel)\
**Post date:** [July 26, 2024, 9:16am UTC](https://internals.rust-lang.org/t/non-static-data-in-static/21247/4 "2024-07-26T09:16:20Z")

</div>

From lifetimes perspective, this is not allowed, because there can be code running after `main` exits, e.g. `Termination` formatter, panic hook, atexit handlers, other threads, and they could all access `'static` data that outlives data in main.

* * *

To initialize static data at run time, Rust uses OnceCell/LazyCell and similar. There's a cost of a check on every access.

If you ensure the data is always initialised before it's used and isn't mutated afterwards, you could access it safely without a check. It does require `unsafe` to implement.

---

<div class="post-metadata">

**Author:** ![Ddystopia](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ddystopia/32/10915_2.png) [@Ddystopia](https://internals.rust-lang.org/u/Ddystopia)\
**Post date:** [July 26, 2024, 9:30am UTC](https://internals.rust-lang.org/t/non-static-data-in-static/21247/5 "2024-07-26T09:30:01Z")

</div>

You cannot do it even with unsafe

---

<div class="post-metadata">

**Author:** ![Ddystopia](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ddystopia/32/10915_2.png) [@Ddystopia](https://internals.rust-lang.org/u/Ddystopia)\
**Post date:** [July 26, 2024, 9:36am UTC](https://internals.rust-lang.org/t/non-static-data-in-static/21247/6 "2024-07-26T09:36:03Z")

</div>

I've opened [What do we need to get the same functionality without `generic_const_exprs`?](https://internals.rust-lang.org/t/what-do-we-need-to-get-the-same-functionality-without-generic-const-exprs/21255) that elaborates the question. There is real unsafe implemention, but it requires feature generic\_const\_expressions. I would really appreciate if you have a knowledge on how to make it "just using unsafe". _Especially for unnamable types, like futures - not just store Foo\<'static\> and unsafely write Foo\<'a\>, because you cannot rewrite lifetimes of unnamable type - maximum it just get it with TAIT feature_

---

<div class="post-metadata">

**Author:** ![kornel](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/kornel/32/2711_2.png) [@kornel](https://internals.rust-lang.org/u/kornel)\
**Post date:** [July 26, 2024, 11:22am UTC](https://internals.rust-lang.org/t/non-static-data-in-static/21247/7 "2024-07-26T11:22:20Z")

</div>

Sorry, I've meant you'd have to use a `'static` lifetime for the type, and actually have it obey such lifetime.

I don't think Rust has any way to reason about shorter lifetimes accessed through anything global, since lifetimes are very scope-oriented.

---

<div class="post-metadata">

**Author:** ![Ddystopia](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ddystopia/32/10915_2.png) [@Ddystopia](https://internals.rust-lang.org/u/Ddystopia)\
**Post date:** [July 26, 2024, 1:00pm UTC](https://internals.rust-lang.org/t/non-static-data-in-static/21247/8 "2024-07-26T13:00:03Z")

</div>

Well, I agree that Rust is not obligated to reason about them, but at least give ability to use `unsafe`, so users can reason about them themselves. Now there is just _no way_ to do this without replacing 'a to 'static by hand, but you can only do it with nameable types

---

<div class="post-metadata">

**Author:** ![toc](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/toc/32/6692_2.png) [@toc](https://internals.rust-lang.org/u/toc)\
**Post date:** [July 26, 2024, 2:17pm UTC](https://internals.rust-lang.org/t/non-static-data-in-static/21247/9 "2024-07-26T14:17:07Z")

</div>

Instead of accessing the mutable static directly post initialization, you could access it via a function. A function could read any sort of type out of a backing `static mut [u64; BIG_ENOUGH]`.

---

<div class="post-metadata">

**Author:** ![Ddystopia](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ddystopia/32/10915_2.png) [@Ddystopia](https://internals.rust-lang.org/u/Ddystopia)\
**Post date:** [July 26, 2024, 2:40pm UTC](https://internals.rust-lang.org/t/non-static-data-in-static/21247/10 "2024-07-26T14:40:12Z")

</div>

This is exactly what I did in liked post and to be ergonomic it requires (maybe Perma) unstable feature `generic_const_expressions`

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [October 24, 2024, 2:40pm UTC](https://internals.rust-lang.org/t/non-static-data-in-static/21247/11 "2024-10-24T14:40:41Z")

</div>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
