Static variable with an empty vector of `Cell`
⚓ Rust 📅 2025-12-18 👤 surdeus 👁️ 4Hello, I need a static variable with a dummy value of !Sync type, but the type is !Sync because it has a vector of !Sync elements, so if the vector is empty then the value of a type is Sync. Is there some way / crate to express it without me writing unsafe?
pub struct Foo(pub Vec<Cell<u8>>);
// No way to get `&mut Foo` thus we can't append elements thus no `Cell` thus `Sync`.
static FOO: Foo = Foo(Vec::new());
fn bar(foo: &Foo) {
for element in foo.0.iter() {
element.update(|x| x.wrapping_add(1));
}
}
bar(&'static FOO); // It is fine to use that dummy across the threads
3 posts - 3 participants
🏷️ Rust_feed