Skip to main content

Crate generic_static_cache

Crate generic_static_cache 

Source
Expand description

Quoting the Rust Reference:

A static item defined in a generic scope (for example in a blanket or default implementation) will result in exactly one static item being defined, as if the static definition was pulled out of the current scope into the module. There will not be one item per monomorphization.

One way to work around this is to use a HashMap<TypeId,Data>. This is a simple & usually the best solution. If lookup performance is important, you can skip hashing the TypeId for minor gains as it already contains a good-quality hash.

This crate aims to further fully remove the lookup by allocating the storage using inline assembly.

§⚠ Caveats ⚠

THIS PLACE IS NOT A PLACE OF HONOR.
NO HIGHLY ESTEEMED DEED IS COMMEMORATED HERE.
NOTHING VALUED IS HERE.
WHAT IS HERE WAS DANGEROUS AND REPULSIVE TO US.
THE DANGER IS IN A PARTICULAR LOCATION.
THE DANGER IS STILL PRESENT, IN YOUR TIME, AS IT WAS IN OURS.

Different compilation units may access different instances of the data (at least without share-generics).

Supported targets are x86-64, aarch64, arm and x86; on other targets, this crate falls back to a hashmap.

The linker may not use a smaller align for the data section than that of the types used with global.

This crate isn’t as well-tested as is should be.

§Usage

The generic_static macro defines a static inside of a function, with every instantiation of the function having it’s own instance of the static:

fn get_and_inc<T>() -> i32 {
    generic_static!{
        static COUNTER: &AtomicI32 = &AtomicI32::new(1);
    }
    COUNTER.fetch_add(1, Ordering::Relaxed)
}
assert_eq!(get_and_inc::<bool>(), 1);
assert_eq!(get_and_inc::<bool>(), 2);
assert_eq!(get_and_inc::<String>(), 1);
assert_eq!(get_and_inc::<bool>(), 3);

Underlying this is the global::<T>() function, which allocates a shared global static for each type it’s used with.

Re-exports§

pub use bytemuck;

Modules§

non_zeroable_globalx86-64 or AArch64 or ARM or x86
Access a generic global that can contain non-zeroable types. This is done via an indirection to the heap, so use global if this is not needed.

Macros§

generic_staticx86-64 or AArch64 or ARM or x86
Declare a static variable that is not shared across different monomorphizations of the containing functions. Its type must be a shared reference to a Sync type.

Structs§

Box
A pointer type that uniquely owns a heap allocation of type T.

Functions§

global
Access a global instance of T, zero-initialized at program start.