Skip to main content

Crate generic_static_cache

Crate generic_static_cache 

Source
Expand description

§generic_static_cache

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 speed up the lookup by allocating the storage using inline assembly: Accessing a generic static provided by this crate is instant, whereas using a hashmap takes more than 10×instant.

§⚠ Caveats ⚠

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

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

§no_std

On supported platforms, global is always available.

With the alloc feature, generic_static! and non_zeroable_global become available.

With the std feature, everything also becomes available on unsupported platforms.

Re-exports§

pub use bytemuck;

Modules§

non_zeroable_globalalloc and (std or x86-64 or AArch64 or ARM or x86)
Access a generic global that can contain non-zeroable types.

Macros§

generic_staticalloc and (std or x86-64 or AArch64 or ARM or x86)
Declare a static variable that is not shared across different monomorphizations of the containing functions.

Structs§

NoOpTypeIdBuildHasherstd
Hasher for TypeIdMap.

Functions§

globalstd or x86-64 or AArch64 or ARM or x86
Access a zero-initialized global static instance of T.
local_globalstd or x86-64 or AArch64 or ARM or x86
Like global, but the symbol identifying the storage will not be exported.

Type Aliases§

TypeIdMapstd
Fast map keyed by TypeId.