rustr/protect/
stackp.rs

1use ::traits::*;
2use ::rdll::*;
3
4// use ::std::borrow::Borrow;
5#[inline]
6pub unsafe fn rustr_protect(x: SEXP) -> SEXP {
7    if x != R_NilValue {
8        Rf_protect(x);
9    }
10    x
11}
12
13// use ::std::borrow::Borrow;
14#[inline]
15pub unsafe fn rustr_unprotect(x: SEXP) {
16    if x != R_NilValue {
17        Rf_unprotect(1)
18    }
19}
20/// Shield
21///
22///Shield is safe to use
23pub struct Shield {
24    t: SEXP,
25}
26
27
28impl Shield {
29    pub fn new<T: ToSEXP>(t_: T) -> Shield {
30        unsafe {
31            // println!("{:?}", "protecting \n");
32            Shield { t: rustr_protect(t_.s()) }
33        }
34    }
35}
36
37impl Drop for Shield {
38    fn drop(&mut self) {
39        unsafe {
40            // println!("{:?}", "droping \n");
41            if self.t != R_NilValue {
42                Rf_unprotect(1)
43            };
44        }
45    }
46}
47
48impl ToSEXP for Shield {
49    unsafe fn s(&self) -> SEXP {
50        self.t
51    }
52}
53
54impl Moves for Shield {
55    fn moves(mut x: Self) -> Shield {
56        let res = Shield { t: x.t };
57        unsafe {
58            x.t = R_NilValue;
59        }
60
61        res
62    }
63}
64
65
66impl Others for Shield {
67    fn others(x: &Shield) -> Shield {
68        unsafe { Shield { t: rustr_protect(x.t) } }
69    }
70}