Create a thread_local! per type instantiated

Consider the following code:

pub struct FooManager<T> {
    data: Vec<T>,
}

impl FooManager<T> {
    fn new() -> FooManager {
        foo { data: Vec::new() }
    }

    pub fn get_per_thread() -> &FooManager {
        &MANAGER<T>

    }
}

thread_local! {
    static MANAGER<T>: FooManager<T> = FooManager::new();
}

If we dropped the <T>, it is easy to make the code work. Question is: is there a way t make thsi work with the <T>, i.e. I want to be able to have:

FooManager<usize>, FooManager<String>, FooManager<Cat>, FooManager<Dog>

and have a separate thread_local variable created for each.

There are some old proposals for generic statics, which could solve this, but none of them seem to have gone very far.

For now, putting an AnyMap into a thread_local is one possible alternative.

1 Like