Rust does NOT monomorphize it's [static generic items]. This is dissappointing when you have a
generic function that contains a static variable depending on that generic. You'll get the
following error:
text
error[E0401]: can't use generic parameters from outer function
That's pretty frustrating when you want to write a singleton pattern that rely's on a generic parameter. That's where this crate saves the day!
generic_singleton
uses [anymap] behind the scenes to store a map of each generic type. The first
time you call get_or_init
we initialize the singleton in thread local storage. Subsequent
calls to get_or_init
will retrieve the singleton from the map.
DO NOT USE WITH A TYPE YOUR CRATE DOES NOT PRIVATELY OWN!!! The map is shared across crates, so if you use a type that is publicly available, then another crate will be able to mutate your singleton, breaking whichever rules you've got in place and vice-versa. Use the [new type pattern] if you need to use a public struct in the map.
```rust use std::cell::RefCell; use std::collections::HashMap; use std::ops::{Deref, DerefMut, Mul};
use genericsingleton::getor_init;
// The expensive function we're trying to cache using a singleton map
fn multiply
// Private struct to use in the get_or_init
function
struct MyPrivateHashMap
impl
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl
fn multiplywithcache
fn main() {
// This call will create the AnyMap and the MyPrivateHashMap
// This call will create a second MyPrivateHashMap< and do the multiplication
multiply_with_cache::<i32>(-1, -10);
// This call will only retrieve the value of the multiplication from MyPrivateHashMap
multiply_with_cache::<i32>(-1, -10);
} ```
get_or_init_local
and get_or_init_global
if I
can figure out how to implement concurrent access and the required trait bound to make it safe.get_or_init
on the same type will
return the SAME singleton. Even across crate boundaries! I might be able to limit
it by providing a macro that salts the key in AnyMap
with a context about which function it's
being called from.