rustr/
macros.rs

1//! Helper macro
2//!
3//!
4
5use error::*;
6use rdll::{R_ToplevelExec, Rboolean};
7
8#[inline]
9pub fn rbool(x: Rboolean) -> bool {
10    x == Rboolean::TRUE
11}
12
13/// Check User Interrupt
14/// 
15#[macro_export]
16macro_rules! check_interrupt_return {
17    () => (
18        unsafe{
19            return rerror($crate::error::REKind::Interrupted("user interrpted".into()))
20        }
21        	)
22}
23
24pub fn check_interrupt() -> RResult<()> {
25    unsafe {
26        if R_ToplevelExec(::std::option::Option::Some(::util::check_interrupt_fn),
27                          ::std::ptr::null_mut()) == Rboolean::FALSE {
28            // NULL=0
29            rerror(REKind::Interrupted("user interrpted".into()))
30        } else {
31            Ok(())
32        }
33    }
34}
35
36
37/// For rustinr codegen, it moves error to R
38/// 
39#[macro_export]
40macro_rules! unwrapr_void {
41    ($expr:expr) => (match $expr {
42        Ok(val) => val,
43        Err(err) => {
44        	$crate::error::forward_exception_to_r(err);
45            return;
46        }
47    	}
48    )
49}
50
51/// For rustinr codegen, it moves error to R
52/// 
53#[macro_export]
54macro_rules! rtry {
55    ($expr:expr) => (match $expr {
56        Ok(val) => val,
57        Err(err) => {
58        	return Err($crate::error::RError::other(err));
59        }
60    	}
61    )
62}
63
64/// For rustinr codegen, it moves error to R
65/// 
66#[macro_export]
67macro_rules! unwrapr {
68    ($expr:expr) => (match $expr {
69        Ok(val) => val,
70        Err(err) => {
71        	$crate::error::forward_exception_to_r(err);
72        	unsafe{
73				return R_NilValue;
74        	}
75        }
76    })
77}
78
79/// For RPtr
80/// 
81#[macro_export]
82macro_rules! gen_traits_rptr{
83	($typ:ident)=>(
84
85impl<Obj:Any, T: SEXPbucket> IntoR for $typ<Obj, T> {
86    fn intor(&self) -> RResult<SEXP> {
87        Ok(unsafe{self.data.s()})
88    }
89}
90
91
92impl<Obj:Any, T: SEXPbucket> UIntoR for $typ<Obj, T> {
93    unsafe fn uintor(&self) -> SEXP {
94        {self.data.s()}
95    }
96}
97
98impl<Obj:Any, T: SEXPbucket> RAttribute for $typ<Obj, T> {}
99impl<Obj:Any, T: SEXPbucket> RSlot for $typ<Obj, T> {}
100
101impl<Obj:Any, T: SEXPbucket> ToSEXP for $typ<Obj, T> {
102    unsafe fn s(&self) -> SEXP {
103        self.data.s()
104    }
105}
106
107impl<Obj:Any, T: SEXPbucket> SetSelf for $typ<Obj, T> {
108    fn set(&mut self, x: Self) {
109        unsafe{self.data.set(x.s());}
110    }
111}
112
113impl<Obj:Any,T: SEXPbucket> Shallow for $typ<Obj,T> {
114    fn sc(&self) ->Self{
115        unsafe{ $typ{data: T::new(self.s()),obj:PhantomData, tag:self.tag}}
116    }
117}
118
119impl<Obj:Any,T: SEXPbucket> Clone for $typ<Obj,T> {
120    fn clone(&self) -> Self {
121        unsafe { $typ { data: T::new(Rf_duplicate(self.data.s())), obj:PhantomData,tag:self.tag } }
122    }
123}
124impl<Obj:Any,T: SEXPbucket> RObjClass for $typ<Obj,T> {
125    type Origin = $typ<Obj,T> ;
126}
127	
128	)
129}
130
131/// For SEXP
132/// 
133#[macro_export]
134macro_rules! gen_traits_sexp{
135	($typ:ident)=>(
136		
137#[derive(Debug)]
138pub struct $typ<T: SEXPbucket> {
139    data: T,
140}
141
142impl< T: SEXPbucket> IntoR for $typ< T> {
143    fn intor(&self) -> RResult<SEXP> {
144        Ok(unsafe{self.data.s()})
145    }
146}
147
148
149impl< T: SEXPbucket> UIntoR for $typ< T> {
150    unsafe fn uintor(&self) -> SEXP {
151        self.data.s()
152    }
153}
154
155impl<T: SEXPbucket> RAttribute for $typ<T> {}
156impl<T: SEXPbucket> RSlot for $typ<T> {}
157
158impl<T: SEXPbucket> ToSEXP for $typ<T> {
159    unsafe fn s(&self) -> SEXP {
160        self.data.s()
161    }
162}
163
164impl<T: SEXPbucket> SetSelf for $typ<T> {
165    fn set(&mut self, x: Self) {
166        unsafe{self.data.set(x.s());}
167    }
168}
169
170impl<T: SEXPbucket> Shallow for $typ<T> {
171    fn sc(&self) ->Self{
172        unsafe{ $typ{data: T::new(self.s())}}
173    }
174}
175
176impl<T: SEXPbucket> Clone for $typ<T> {
177    fn clone(&self) -> Self {
178        unsafe { $typ { data: T::new(Rf_duplicate(self.data.s()))} }
179    }
180}
181impl<T: SEXPbucket> RObjClass for $typ<T> {
182    type Origin = $typ<T>;
183}
184
185impl<T: SEXPbucket> Named for $typ<T>{}
186
187	)
188}