-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.rs
54 lines (41 loc) · 1012 Bytes
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
use rustler::Env;
use rustler::ResourceArc;
use std::mem::drop;
use std::sync::Mutex;
struct Counter {
num: Mutex<i64>,
}
type CounterArc = ResourceArc<Counter>;
#[rustler::nif]
fn new() -> CounterArc {
ResourceArc::new(Counter { num: Mutex::new(0) })
}
#[rustler::nif]
fn increment(counter: CounterArc) -> CounterArc {
let mut counter_lock = counter.num.lock().unwrap();
*counter_lock = *counter_lock + 1;
drop(counter_lock);
counter
}
#[rustler::nif]
fn decrement(counter: CounterArc) -> CounterArc {
let mut counter_lock = counter.num.lock().unwrap();
*counter_lock = *counter_lock - 1;
drop(counter_lock);
counter
}
#[rustler::nif]
fn read(counter: CounterArc) -> i64 {
let counter_lock = counter.num.lock().unwrap();
let data = *counter_lock;
drop(counter_lock);
data
}
rustler::init!(
"Elixir.Counter",
[new, increment, decrement, read],
load = |env: Env, _| {
rustler::resource!(Counter, env);
true
}
);