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
55
56
57
58
59
60
use std::{marker::PhantomData, ops::Deref, sync::Once};
use ocaml_boxroot_sys::{
boxroot_create, boxroot_delete, boxroot_get, boxroot_get_ref, boxroot_modify, boxroot_setup,
BoxRoot as PrimitiveBoxRoot,
};
use crate::{memory::OCamlCell, OCaml, OCamlRef, OCamlRuntime};
pub struct BoxRoot<T: 'static> {
boxroot: PrimitiveBoxRoot,
_marker: PhantomData<T>,
}
impl<T> BoxRoot<T> {
pub fn new(val: OCaml<T>) -> BoxRoot<T> {
static INIT: Once = Once::new();
INIT.call_once(|| unsafe {
boxroot_setup();
});
BoxRoot {
boxroot: unsafe { boxroot_create(val.raw) },
_marker: PhantomData,
}
}
pub fn get<'a>(&self, cr: &'a OCamlRuntime) -> OCaml<'a, T> {
unsafe { OCaml::new(cr, boxroot_get(self.boxroot)) }
}
pub fn keep<'tmp>(&'tmp mut self, val: OCaml<T>) -> OCamlRef<'tmp, T> {
unsafe {
boxroot_modify(&mut self.boxroot, val.raw);
&*(boxroot_get_ref(self.boxroot) as *const OCamlCell<T>)
}
}
}
impl<T> Drop for BoxRoot<T> {
fn drop(&mut self) {
unsafe { boxroot_delete(self.boxroot) }
}
}
impl<T> Deref for BoxRoot<T> {
type Target = OCamlCell<T>;
fn deref(&self) -> OCamlRef<T> {
unsafe { &*(boxroot_get_ref(self.boxroot) as *const OCamlCell<T>) }
}
}