Debian Codegraph @

third_party/rust/wast/src/gensym.rs

raw
1 use crate::token::{Id, Span};
2 use std::cell::Cell;
3
4 thread_local!(static NEXT: Cell<u32> = Cell::new(0));
5
6 pub fn reset() {
7 NEXT.with(|c| c.set(0));
8 }
9
10 pub fn gen(span: Span) -> Id<'static> {
11 NEXT.with(|next| {
12 let gen = next.get() + 1;
13 next.set(gen);
14 Id::gensym(span, gen)
15 })
16 }
17
18 pub fn fill<'a>(span: Span, slot: &mut Option<Id<'a>>) -> Id<'a> {
19 *slot.get_or_insert_with(|| gen(span))
20 }