• Stars
    star
    1,502
  • Rank 31,239 (Top 0.7 %)
  • Language
    Rust
  • License
    Apache License 2.0
  • Created over 6 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Rust library for single assignment cells and lazy statics without macros

once_cell

Build Status Crates.io API reference

Overview

once_cell provides two new cell-like types, unsync::OnceCell and sync::OnceCell. OnceCell might store arbitrary non-Copy types, can be assigned to at most once and provide direct access to the stored contents. In a nutshell, API looks roughly like this:

impl OnceCell<T> {
    fn new() -> OnceCell<T> { ... }
    fn set(&self, value: T) -> Result<(), T> { ... }
    fn get(&self) -> Option<&T> { ... }
}

Note that, like with RefCell and Mutex, the set method requires only a shared reference. Because of the single assignment restriction get can return an &T instead of Ref<T> or MutexGuard<T>.

once_cell also has a Lazy<T> type, build on top of OnceCell which provides the same API as the lazy_static! macro, but without using any macros:

use std::{sync::Mutex, collections::HashMap};
use once_cell::sync::Lazy;

static GLOBAL_DATA: Lazy<Mutex<HashMap<i32, String>>> = Lazy::new(|| {
    let mut m = HashMap::new();
    m.insert(13, "Spica".to_string());
    m.insert(74, "Hoyten".to_string());
    Mutex::new(m)
});

fn main() {
    println!("{:?}", GLOBAL_DATA.lock().unwrap());
}

More patterns and use-cases are in the docs!

Related crates

Parts of once_cell API are included into std as of Rust 1.70.0.

More Repositories

1

cargo-xtask

843
star
2

xshell

Rust
692
star
3

rust-course

CSS
220
star
4

minipratt

Rust
144
star
5

fall

Rust
121
star
6

crt

Rust
96
star
7

xflags

Rust
79
star
8

pale-fire

Port of Emacs Zenburn theme to VS Code
Rust
72
star
9

arbtest

A minimalist property-based testing library
Rust
64
star
10

proc-caesar

A proc-macro to break Rust IDEs
Rust
63
star
11

matklad.github.io

My coding related blog
TypeScript
58
star
12

cov-mark

Rust
55
star
13

config

configuration.nix is better than dot files
Rust
55
star
14

lock-bench

Rust
42
star
15

tom

tom: a format-preserving TOML parser in Rust
Rust
38
star
16

djot-rs

Rust
35
star
17

macro-dep-test

32
star
18

dlx

Rust
27
star
19

typed_key

A Rust library for strongly-typed string keys for configuration.
Rust
26
star
20

uncover

More maintainable Rust test
Rust
26
star
21

rustraytracer

A ray tracer written in Rust
Rust
26
star
22

always-assert

Rust
20
star
23

paxosnotes

TLA
19
star
24

spin-of-death

Rust
19
star
25

miniml

Rust
19
star
26

elapsed

Measure execution time of a block of Rust code
Rust
16
star
27

jthread-rs

Rust
15
star
28

drop_bomb

A Rust library for runtime-checked linearish types
Rust
14
star
29

jod-thread

std::thread which joins on drop by default.
Rust
14
star
30

bounds-check-cost

Rust
14
star
31

10k_linux_threads

Rust
13
star
32

backtrace-on-stack-overflow

Rust
12
star
33

abont

Finally, a shell for all seasons!
Rust
11
star
34

webassembly-test

Rust
9
star
35

write-json

Rust
9
star
36

tracing-span-tree

Rust
9
star
37

aoc2022

Zig
8
star
38

learnOpenGL

Rust
8
star
39

benchmarks

Rust
7
star
40

versuch

An experiment in try-fns
Rust
6
star
41

devils

Rust
5
star
42

rust-idea-plugin

Java
5
star
43

rust-inline

5
star
44

properly-concurrent

Rust
5
star
45

my-desire

Rust
5
star
46

repros

Rust
4
star
47

hashbench

Rust
4
star
48

m_lexer

Rust
4
star
49

raytracer

A ray tracer written in Haskell
Haskell
4
star
50

aoc2019

Rust
4
star
51

cargo-xtask-sqlx-example

Rust
3
star
52

glass-pipes

Rust
3
star
53

quadheap

Rust
3
star
54

join_to_string

Rust
3
star
55

auchat

Rust
3
star
56

varint

Rust
3
star
57

wsmeta

Shell
3
star
58

xaction

Rust
3
star
59

bunny

An exercise with glium
Rust
3
star
60

ftl

Rust
3
star
61

hello-getzig

Shell
3
star
62

open-notes

TypeScript
2
star
63

mu

meta circular interpreter
mupad
2
star
64

t-cmd

Rust
2
star
65

timeit

Rust
2
star
66

RobinHood

Robin Hood hashing unbenchmark
Rust
2
star
67

parse_tree

Rust
2
star
68

multithread

Rust
2
star
69

s

Rust
2
star
70

iwp

Rust
2
star
71

vec-vs-list

Compare vector vs linked list in terms of performance
Rust
2
star
72

calc

Rust
2
star
73

kotlin-rust-dp

Rust
2
star
74

workspace-vs-feaures

Rust
2
star
75

mandelbrot

Rust
2
star
76

tree_bench

Rust
1
star
77

with

Rust
1
star
78

noyaml

Rust
1
star
79

scope

Rust
1
star
80

diploma

TeX
1
star
81

nested-mod

Rust
1
star
82

mom

Rust
1
star
83

group-by-challenge

Rust
1
star
84

tigerbeetle-filtered

Zig
1
star
85

compilers

Rust
1
star
86

thrench

Rust
1
star
87

rustlexspec

Rust
1
star
88

dylib-rs

Rust
1
star
89

typed_index_derive

Rust
1
star
90

aoc2020

Rust
1
star
91

asyncbench

Clojure
1
star
92

resilient-ll-parsing

Rust
1
star
93

ocamlraytracer

OCaml
1
star
94

.emacs.d

Emacs Lisp
1
star
95

rust-ws

Nix
1
star
96

hashset-vs-vec-bench

Rust
1
star
97

get-example

Rust
1
star
98

flat

Python
1
star
99

cradle-of-tar

Rust
1
star
100

feeds.json

JavaScript
1
star