Fast, initializable, and thread safe static variables

Overview

tagged_cell

CI Crates.io API reference

Fast, initializable, and thread safe static variables

Overview

Borrows the excellent ZST based tagging implementation (linked below) to guarantee the cell is initialized exactly once before an access is attempted. https://www.hardmo.de/article/2021-03-14-zst-proof-types.md#proof-of-work

This is implemented via TaggedCell and a Tag type, which must be unique for each instance of the TaggedCell for safe operation. The TaggedCell must then be initialized via TaggedCell::init(), which initializes the underlying data using a user provided function or closure, and then returns a special zero-sized Init used to access the Cell's data.

To ensure unique tag types are used for each cell, the tagged_cell! macro is provided. The macro creates a new tag type based on the variable's name, and applies it in the declaration.

use tagged_cell::tagged_cell;
tagged_cell!{
   static BAR: TaggedCell<Vec<usize>, _> = TaggedCell::new();
}

let tag = BAR.init(|| vec![0, 10, 20]);
let vec = BAR.get(tag);

assert_eq!(vec[2], 20);

To allow for usage across threads, only the first invocation of TaggedCell::init will initialize the Cell's data. All future TaggedCell::init calls will just return a new tag. It is undetermined which thread will initialize the Cell's data.

use std::thread;
use tagged_cell::tagged_cell;

tagged_cell!{
    static TABLE: TaggedCell<Vec<usize>, _> = TaggedCell::new();
}

thread::spawn(move || {
    let tag = TABLE.init(|| vec![0, 10, 20]);
    let table = TABLE.get(tag);
    assert_eq!(table[2], 20);
});

thread::spawn(move || {
    let tag = TABLE.init(|| vec![0, 10, 20]);
    let table = TABLE.get(tag);
    assert_eq!(table[1], 10);
});
You might also like...
Membrane is an opinionated crate that generates a Dart package from a Rust library. Extremely fast performance with strict typing and zero copy returns over the FFI boundary via bincode.

Membrane is an opinionated crate that generates a Dart package from a Rust library. Extremely fast performance with strict typing and zero copy returns over the FFI boundary via bincode.

⚡️ Fast MagicString port driven by Rust and N-API

magic-string-rs 100% API compatible (port) MagicString by Rich-Harris implementation for Node and modern browsers, also, for rust, of course. Installa

Blazing fast linter for JavaScript and TypeScript written in Rust

deno_lint A Rust crate for writing fast JavaScript and TypeScript linters. This crate powers deno lint, but is not Deno specific and can be used to wr

Fast, compact and all-around subdomain enumeration tool written in Rust
Fast, compact and all-around subdomain enumeration tool written in Rust

Fast, compact and all-around subdomain enumeration tool written in Rust, which uses dns bruteforce, internet search and recursive http content search.

Simple and fast git helper functions

Simple and fast git helper functions

Build your service-server fast, easy (and without hosting!)
Build your service-server fast, easy (and without hosting!)

service-io is a library to build servers that offering services with really little effort. Choose an input connector. Choose an output connector. Choo

Fast and scalable phylogenomic utilities 🐱 .

ogcat Fast and scalable phylogenomic utilities 🐱 . Installation Prebuilt binaries See releases. The musl binary for Linux should be the most compatib

Online-statistics is crate 🦀 for Blazingly fast, generic and serializable online statistics

Online statistics in Rust 🦀 for Blazingly fast, generic and serializable online statistics. Quickstart Let's compute th

syncmap is a fast, concurrent cache library built with a focus on performance and correctness.

syncmap syncmap syncmap is a fast, concurrent cache library syncmap is a fast, concurrent cache library built with a focus on performance and correctn

Comments
  • Restrict TaggedCell creation and require initialization closure in tagged_cell! macro

    Restrict TaggedCell creation and require initialization closure in tagged_cell! macro

    Currently, the following safety issues exist in this repo:

    1. Users can manually create multiple TaggedCells with the same Tag type if they opt to forgo using tagged_cell!
    2. Users can define conflicting functions or closures in different thread's init() invocations, and only one random one will actually run.

    Future solution for these is to expand the tagged_cell! macro to both include the initialization closure, and to restrict TaggedCells to only be declared via the macro

    enhancement 
    opened by Dasch0 1
Owner
David Schwarz
David Schwarz
Rust Lambda Extension for any Runtime to preload SSM Parameters as 🔐 Secure Environment Variables!

?? Crypteia Rust Lambda Extension for any Runtime to preload SSM Parameters as Secure Environment Variables! Super fast and only performaned once duri

Custom Ink 34 Jan 7, 2023
Mix async code with CPU-heavy thread pools using Tokio + Rayon

tokio-rayon Mix async code with CPU-heavy thread pools using Tokio + Rayon Resources Documentation crates.io TL;DR Sometimes, you're doing async stuff

Andy Barron 74 Jan 2, 2023
untyped-arena provides an Arena allocator implementation that is safe and untyped with minimal complexity

untyped-arena untyped-arena provides an Arena allocator implementation that is safe and untyped with minimal complexity Usage let arena = Arena::new()

Max Bruce 1 Jan 9, 2022
This crate allows you to safely initialize Dynamically Sized Types (DST) using only safe Rust.

This crate allows you to safely initialize Dynamically Sized Types (DST) using only safe Rust.

Christofer Nolander 11 Dec 22, 2022
✨ Safe, global singletons initialized at program start

✨ magic_static Safe, global singletons initialized at program start. Usage Simply add magic_static as a dependency in your Cargo.toml to get started:

William 5 Nov 8, 2022
Type-safe IPC for Tauri using GraphQL

Tauri Plugin graphql A plugin for Tauri that enables type-safe IPC through GraphQL. Install Rust [dependencies] tauri-plugin-graphql = "0.2" JavaScrip

Jonas Kruckenberg 40 Dec 29, 2022
A safe `Pin`-based intrusive doubly-linked list in Rust

pin-list This crate provides PinList, a safe Pin-based intrusive doubly linked list. Example A thread-safe unfair async mutex. use pin_project_lite::p

Sabrina Jewson 7 Oct 26, 2022
A safe sync/async multi-producer, multi-consumer channel

Loole A safe async/sync multi-producer multi-consumer channel. Producers can send and consumers can receive messages asynchronously or synchronously:

Mahdi Shojaee 50 Oct 6, 2023
Fusion is a cross-platform App Dev ToolKit build on Rust . Fusion lets you create Beautiful and Fast apps for mobile and desktop platform.

Fusion is a cross-platform App Dev ToolKit build on Rust . Fusion lets you create Beautiful and Fast apps for mobile and desktop platform.

Fusion 1 Oct 19, 2021
Czkawka is a simple, fast and easy to use app to remove unnecessary files from your computer.

Multi functional app to find duplicates, empty folders, similar images etc.

Rafał Mikrut 9.2k Jan 4, 2023