Debian Codegraph @

debian/rust-vendor/ctor/examples/ctor-example.rs

raw
1 //! Examples from the `README`.
2 // Ensure we don't blow a low recursion limit.
3 #![recursion_limit = "62"]
4
5 use ctor::ctor;
6 use std::collections::HashMap;
7 use std::sync::atomic::{AtomicBool, Ordering};
8
9 static INITED: AtomicBool = AtomicBool::new(false);
10
11 #[ctor(unsafe)]
12 fn foo() {
13 INITED.store(true, Ordering::SeqCst);
14 }
15
16 #[ctor(unsafe)]
17 /// This is an immutable static, evaluated at init time.
18 static STATIC_CTOR: HashMap<u32, &'static str> = {
19 let mut m = HashMap::new();
20 m.insert(0, "foo");
21 m.insert(1, "bar");
22 m.insert(2, "baz");
23 m
24 };
25
26 fn main() {
27 assert!(INITED.load(Ordering::SeqCst));
28 assert_eq!(STATIC_CTOR.get(&1), Some(&"bar"));
29 }