rustr/traits/
intor.rs

1use ::rdll::*;
2use ::rtype::*;
3use error::{RResult, REKind, rerror};
4use traits::*;
5use ::protect::stackp::*;
6use std::ffi::{CString, CStr};
7use std::collections::{HashMap, VecDeque, BTreeMap, BTreeSet, BinaryHeap, HashSet, LinkedList};
8use REKind::*;
9use util::*;
10
11impl IntoR for () {
12    fn intor(&self) -> RResult<SEXP> {
13
14        unsafe { Ok(Self::uintor(self)) }
15    }
16}
17
18impl UIntoR for () {
19    unsafe fn uintor(&self) -> SEXP {
20        R_NilValue
21    }
22}
23
24impl IntoR for bool {
25    fn intor(&self) -> RResult<SEXP> {
26        unsafe { Ok(Self::uintor(self)) }
27    }
28}
29
30impl UIntoR for bool {
31    unsafe fn uintor(&self) -> SEXP {
32        let rvec = Shield::new(Rf_allocVector(LGLSXP, 1));
33        let ptr = LOGICAL(rvec.s());
34        *ptr.offset(0) = *self as ::std::os::raw::c_int;
35        rvec.s()
36    }
37}
38
39impl RNew for bool {
40    fn rnew(x: SEXP) -> RResult<bool> {
41        if RTYPEOF(x) != LGLSXP && unsafe { Rf_xlength(x) == 1 } {
42            return rerror(NotCompatible("expecting a boolean".into()));
43        }
44        unsafe { Ok(Self::urnew(x)) }
45    }
46}
47
48impl URNew for bool {
49    unsafe fn urnew(x: SEXP) -> bool {
50        let ptr = LOGICAL(x);
51        *ptr.offset(0) == 1
52    }
53}
54
55// fromr integer /////////////////////////////
56
57macro_rules! gen_fromr_int {
58    ($sexp:ident; $sexpget:ident; $err:expr; $into:ty; $($x:ty),*) => (
59		$(
60// main block
61impl RNew for $x {
62	fn rnew(x:SEXP) -> RResult<$x> {
63		unsafe {
64			if RTYPEOF(x) != $sexp  || Rf_xlength(x) != 1{
65    			return rerror(REKind::NotCompatible(concat!("expecting a ",$err).into()));
66			}
67			Ok(Self::urnew(x))
68		}
69	}
70}
71
72impl URNew for $x {
73	unsafe fn urnew(x:SEXP) -> $x {
74			let rptr = $sexpget(x);
75			*rptr.offset(0) as $x
76	}
77}
78
79impl UIntoR for $x {
80    unsafe fn uintor(&self) -> SEXP {
81            let rvec = Shield::new(Rf_allocVector($sexp, 1));
82            let rptr = $sexpget(rvec.s());
83            *rptr.offset(0) = (*self) as $into;
84            rvec.s()
85    }
86}
87
88impl IntoR for $x {
89    fn intor(&self) -> RResult<SEXP> {
90		unsafe{Ok(Self::uintor(self))}
91    }
92}
93			// end main block
94		)*
95    )
96}
97
98// expanded
99//
100// impl FromR<u64> for SEXP {
101// 	fn fromr(&self) -> RResult<u64> {
102// 		...
103// 		...
104// 	}
105// }
106
107gen_fromr_int!(INTSXP; INTEGER; "integer"; ::std::os::raw::c_int; u64,u32,u16,i64,i32,i16,i8,usize,isize);
108gen_fromr_int!(REALSXP; REAL; "value"; ::std::os::raw::c_double; f64,f32);
109
110// for u8
111
112impl RNew for u8 {
113    fn rnew(x: SEXP) -> RResult<u8> {
114        unsafe {
115            if (RTYPEOF(x) != INTSXP && RTYPEOF(x) != RAWSXP) || Rf_xlength(x) != 1 {
116                return rerror(REKind::NotCompatible("expecting a INTSXP or RAWSXP".into()));
117            }
118            Ok(Self::urnew(x))
119        }
120    }
121}
122
123impl URNew for u8 {
124    unsafe fn urnew(x: SEXP) -> u8 {
125        if RTYPEOF(x) == INTSXP {
126            let rptr = INTEGER(x);
127            let res = *rptr.offset(0) as u8;
128            return res;
129        }
130        let rptr = RAW(x);
131        *rptr.offset(0) as u8
132    }
133}
134
135impl UIntoR for u8 {
136    unsafe fn uintor(&self) -> SEXP {
137        let rvec = Shield::new(Rf_allocVector(INTSXP, 1));
138        let rptr = INTEGER(rvec.s());
139        *rptr.offset(0) = (*self) as ::std::os::raw::c_int;
140        rvec.s()
141    }
142}
143
144impl IntoR for u8 {
145    fn intor(&self) -> RResult<SEXP> {
146        unsafe { Ok(Self::uintor(self)) }
147    }
148}
149
150
151
152// intor fromr vec number /////////////////////////////
153
154macro_rules! gen_fromr_vec {
155    ($collection:ident; $push:ident; $sexp:ident; $sexpget:ident; $err:expr; $into:ty ; $($x:ty),*) => (
156		$(
157// main block
158impl RNew for $collection<$x> {
159    fn rnew(x:SEXP) -> RResult<$collection<$x>> {
160        unsafe {
161            if RTYPEOF(x) != $sexp {
162                return rerror(REKind::NotCompatible(concat!("expecting a ",$err).into()));
163            }
164            Ok(Self::urnew(x))
165        }
166    }
167}
168
169impl URNew for $collection<$x> {
170    unsafe fn urnew(x:SEXP) -> $collection<$x> {
171            let lens = Rf_xlength(x);
172            let mut vecs: $collection<$x> = $collection::with_capacity(lens as usize);
173            let mut rptr = $sexpget(x);
174            for _ in 0..lens {
175                vecs.$push(*rptr.offset(0) as $x);
176                rptr = rptr.offset(1);
177            }
178            vecs
179        }
180    }
181
182
183impl UIntoR for $collection<$x> {
184    unsafe fn uintor(&self) -> SEXP {
185        let size_x = self.len();
186
187            let rvec = Shield::new(Rf_allocVector($sexp, size_x as R_xlen_t));
188            let mut rptr = $sexpget(rvec.s());
189            for ii in self {
190                *rptr.offset(0) = ii.clone() as $into ;
191				rptr = rptr.offset(1);
192            }
193            rvec.s()
194
195    }
196}
197
198impl IntoR for $collection<$x> {
199    fn intor(&self) -> RResult<SEXP> {
200        unsafe{Ok(Self::uintor(self))}
201    }
202}
203
204// end main block
205		)*
206    )
207}
208
209gen_fromr_vec!(Vec; push; INTSXP; INTEGER; "integer vector"; ::std::os::raw::c_int; u64,u32,u16,i64,i32,i16,i8,usize,isize);
210gen_fromr_vec!(Vec; push; REALSXP; REAL; "numeric vector"; ::std::os::raw::c_double; f64,f32);
211gen_fromr_vec!(BinaryHeap; push;INTSXP; INTEGER; "integer vector"; ::std::os::raw::c_int; u64,u32,u16,i64,i32,i16,i8,usize,isize);
212gen_fromr_vec!(HashSet; insert;INTSXP; INTEGER; "integer vector"; ::std::os::raw::c_int; u64,u32,u16,i64,i32,i16,i8,usize,isize);
213gen_fromr_vec!(VecDeque; push_back; INTSXP; INTEGER; "integer vector"; ::std::os::raw::c_int; u64,u32,u16,i64,i32,i16,i8,usize,isize);
214gen_fromr_vec!(VecDeque; push_back; REALSXP; REAL; "numeric vector"; ::std::os::raw::c_double; f64,f32);
215
216// bool
217
218macro_rules! gen_fromr_vec_bool {
219    ($collection:ident; $push:ident; $sexp:ident; $sexpget:ident; $err:expr;  $($x:ty),*) => (
220		$(
221// main block
222impl RNew for $collection<$x> {
223    fn rnew(x:SEXP) -> RResult<$collection<$x>> {
224        unsafe {
225            if RTYPEOF(x) != $sexp {
226                return rerror(REKind::NotCompatible(concat!("expecting a ",$err).into()));
227            }
228            Ok(Self::urnew(x))
229        }
230    }
231}
232
233impl URNew for $collection<$x> {
234    unsafe fn urnew(x:SEXP) -> $collection<$x> {
235            let lens = Rf_xlength(x);
236            let mut vecs: $collection<$x> = $collection::with_capacity(lens as usize);
237            let mut rptr = $sexpget(x);
238            for _ in 0..lens {
239                vecs.$push(*rptr.offset(0) ==1 );
240                rptr = rptr.offset(1);
241            }
242            vecs
243        }
244    }
245
246impl UIntoR for $collection<$x> {
247    unsafe fn uintor(&self) -> SEXP {
248        let size_x = self.len();
249
250            let rvec = Shield::new(Rf_allocVector($sexp, size_x as R_xlen_t));
251            let mut rptr = $sexpget(rvec.s());
252            for ii in self {
253                *rptr.offset(0) = ii.clone() as ::std::os::raw::c_int  ;
254				rptr = rptr.offset(1);
255            }
256            rvec.s()
257    }
258}
259
260impl IntoR for $collection<$x> {
261    fn intor(&self) -> RResult<SEXP> {
262        unsafe{Ok(Self::uintor(self))}
263    }
264}
265
266// end main block
267		)*
268    )
269}
270
271gen_fromr_vec_bool!(Vec; push; LGLSXP; LOGICAL; "boolean vector"; bool);
272gen_fromr_vec_bool!(BinaryHeap; push;LGLSXP; LOGICAL; "boolean vector"; bool);
273gen_fromr_vec_bool!(HashSet; insert;LGLSXP; LOGICAL; "boolean vector"; bool);
274gen_fromr_vec_bool!(VecDeque; push_back; LGLSXP; LOGICAL; "boolean vector"; bool);
275
276
277// u8
278macro_rules! gen_u8_collection{
279	($collection:ident; $push:ident)=>(
280
281impl RNew for $collection<u8> {
282    fn rnew(x:SEXP) -> RResult<$collection<u8>> {
283        unsafe {
284            if RTYPEOF(x) != INTSXP && RTYPEOF(x) != RAWSXP {
285                return rerror(REKind::NotCompatible("expecting a INTSXP or RAWSXP".into()));
286            }
287            Ok(Self::urnew(x))
288        }
289    }
290}
291
292impl URNew for $collection<u8> {
293    unsafe fn urnew(x:SEXP) -> $collection<u8> {
294            let lens = Rf_xlength(x);
295            let mut vecs: $collection<u8> = $collection::with_capacity(lens as usize);
296            if RTYPEOF(x) ==INTSXP{
297	            let mut rptr = INTEGER(x);
298	            for _ in 0..lens {
299	                vecs.$push(*rptr.offset(0) as u8);
300	                rptr = rptr.offset(1);
301	            }
302	             return vecs;
303            }
304            let mut rptr = RAW(x);
305            for _ in 0..lens {
306                vecs.$push(*rptr.offset(0) as u8);
307                rptr = rptr.offset(1);
308            }
309            vecs
310        }
311    }
312
313
314impl UIntoR for $collection<u8> {
315    unsafe fn uintor(&self) -> SEXP {
316        let size_x = self.len();
317            let rvec = Shield::new(Rf_allocVector(INTSXP, size_x as R_xlen_t));
318            let mut rptr = INTEGER(rvec.s());
319
320            for ii in self {
321                *rptr.offset(0) = ii.clone() as ::std::os::raw::c_int ;
322				rptr = rptr.offset(1);
323            }
324            rvec.s()
325    }
326}
327
328impl IntoR for $collection<u8> {
329    fn intor(&self) -> RResult<SEXP> {
330        unsafe{Ok(Self::uintor(self))}
331    }
332}
333	)}
334
335gen_u8_collection!(Vec; push);
336gen_u8_collection!(BinaryHeap; push);
337gen_u8_collection!(HashSet; insert);
338gen_u8_collection!(VecDeque; push_back);
339
340// end u8
341macro_rules! gen_ref_int{
342	($INTEGER:ident; $INTSXP:ident; $tyy:ty; $($usize:ident),*) =>(
343$(impl<'a> UIntoR for &'a [$usize]{
344	unsafe fn uintor(&self)->SEXP{
345		let size_x = self.len();
346            let rvec = Shield::new(Rf_allocVector($INTSXP, size_x as R_xlen_t));
347            let mut rptr = $INTEGER(rvec.s());
348
349            for ii in *self {
350                *rptr.offset(0) = ii.clone() as $tyy ;
351				rptr = rptr.offset(1);
352            }
353            rvec.s()
354	}
355}
356
357impl<'a> IntoR for &'a [$usize]{
358	fn intor(&self)->RResult<SEXP>{
359		unsafe{Ok(Self::uintor(self))}
360}
361}
362)*
363	)
364}
365
366gen_ref_int!(INTEGER; INTSXP; ::std::os::raw::c_int; u64,u32,u16,u8,i64,i32,i16,i8,usize,isize);
367gen_ref_int!(REAL; REALSXP; ::std::os::raw::c_double; f64,f32);
368
369
370
371macro_rules! gen_fromr_linklist {
372    ($collection:ident; $push:ident;$sexp:ident; $sexpget:ident; $err:expr; $into:ty ; $($x:ty),*) => (
373		$(
374// main block
375impl RNew for $collection<$x> {
376    fn rnew(x:SEXP) -> RResult<$collection<$x>> {
377        unsafe {
378            if RTYPEOF(x) != $sexp {
379                return rerror(REKind::NotCompatible(concat!("expecting a ",$err).into()));
380            }
381            Ok(Self::urnew(x))
382        }
383    }
384}
385
386impl URNew for $collection<$x> {
387    unsafe fn urnew(x:SEXP) -> $collection<$x> {
388            let lens = Rf_xlength(x);
389            let mut vecs: $collection<$x> = $collection::new();
390            let mut rptr = $sexpget(x);
391            for _ in 0..lens {
392                vecs.$push(*rptr.offset(0) as $x);
393                rptr = rptr.offset(1);
394            }
395            vecs
396    }
397}
398
399impl IntoR for $collection<$x> {
400    fn intor(&self) -> RResult<SEXP> {
401        unsafe{Ok(Self::uintor(self))}
402    }
403}
404
405impl UIntoR for $collection<$x> {
406    unsafe fn uintor(&self) -> SEXP {
407        let size_x = self.len();
408
409            let rvec = Shield::new(Rf_allocVector($sexp, size_x as R_xlen_t));
410            let mut rptr = $sexpget(rvec.s());
411
412            for ii in self {
413                *rptr.offset(0) = ii.clone() as $into ;
414				rptr = rptr.offset(1);
415            }
416            
417            rvec.s()
418
419    }
420}
421
422
423// end main block
424		)*
425    )
426}
427
428gen_fromr_linklist!(LinkedList;push_front;INTSXP; INTEGER; "integer vector"; ::std::os::raw::c_int; u64,u32,u16,i64,i32,i16,i8,usize,isize);
429gen_fromr_linklist!(LinkedList;push_front; REALSXP; REAL; "numeric vector"; ::std::os::raw::c_double; f64,f32);
430gen_fromr_linklist!(BTreeSet;insert;INTSXP; INTEGER; "integer vector"; ::std::os::raw::c_int; u64,u32,u16,i64,i32,i16,i8,usize,isize);
431
432// bool
433
434macro_rules! gen_fromr_linklist_bool {
435    ($collection:ident; $push:ident;$sexp:ident; $sexpget:ident; $err:expr;  $($x:ty),*) => (
436		$(
437// main block
438impl RNew for $collection<$x> {
439    fn rnew(x:SEXP) -> RResult<$collection<$x>> {
440        unsafe {
441            if RTYPEOF(x) != $sexp {
442                return rerror(REKind::NotCompatible(concat!("expecting a ",$err).into()));
443            }
444            Ok(Self::urnew(x))
445        }
446    }
447}
448
449impl URNew for $collection<$x> {
450    unsafe fn urnew(x:SEXP) -> $collection<$x> {
451            let lens = Rf_xlength(x);
452            let mut vecs: $collection<$x> = $collection::new();
453            let mut rptr = $sexpget(x);
454            for _ in 0..lens {
455                vecs.$push(*rptr.offset(0) ==1);
456                rptr = rptr.offset(1);
457            }
458            vecs
459    }
460}
461
462impl IntoR for $collection<$x> {
463    fn intor(&self) -> RResult<SEXP> {
464        unsafe{Ok(Self::uintor(self))}
465    }
466}
467
468impl UIntoR for $collection<$x> {
469    unsafe fn uintor(&self) -> SEXP {
470        let size_x = self.len();
471
472            let rvec = Shield::new(Rf_allocVector($sexp, size_x as R_xlen_t));
473            let mut rptr = $sexpget(rvec.s());
474
475            for ii in self {
476                *rptr.offset(0) = ii.clone() as ::std::os::raw::c_int ;
477				rptr = rptr.offset(1);
478            }
479            rvec.s()
480    }
481}
482
483
484// end main block
485		)*
486    )
487}
488
489gen_fromr_linklist_bool!(LinkedList;push_front;INTSXP; INTEGER; "integer vector"; bool);
490gen_fromr_linklist_bool!(BTreeSet;insert;INTSXP; INTEGER; "integer vector"; bool);
491
492
493// u8
494macro_rules! gen_u8_link{
495	($collection:ident; $push:ident)=>(
496impl RNew for $collection<u8> {
497    fn rnew(x:SEXP) -> RResult<$collection<u8>> {
498        unsafe {
499            if RTYPEOF(x) != INTSXP && RTYPEOF(x) != RAWSXP {
500                return rerror(REKind::NotCompatible("expecting a INTSXP or RAWSXP".into()));
501            }
502            Ok(Self::urnew(x))
503        }
504    }
505}
506
507impl URNew for $collection<u8> {
508    unsafe fn urnew(x:SEXP) -> $collection<u8> {
509            let lens = Rf_xlength(x);
510            let mut vecs: $collection<u8> = $collection::new();
511            if RTYPEOF(x) ==INTSXP{
512				let mut rptr = INTEGER(x);
513	            for _ in 0..lens {
514	                vecs.$push(*rptr.offset(0) as u8);
515	                rptr = rptr.offset(1);
516	            }
517	            return vecs;
518            }
519            let mut rptr = RAW(x);
520            for _ in 0..lens {
521                vecs.$push(*rptr.offset(0) as u8);
522                rptr = rptr.offset(1);
523            }
524            vecs
525    }
526}
527
528impl IntoR for $collection<u8> {
529    fn intor(&self) -> RResult<SEXP> {
530        unsafe{Ok(Self::uintor(self))}
531    }
532}
533
534impl UIntoR for $collection<u8> {
535    unsafe fn uintor(&self) -> SEXP {
536        let size_x = self.len();
537
538            let rvec = Shield::new(Rf_allocVector(INTSXP, size_x as R_xlen_t));
539            let mut rptr = INTEGER(rvec.s());
540
541            for ii in self {
542                *rptr.offset(0) = ii.clone() as ::std::os::raw::c_int ;
543				rptr = rptr.offset(1);
544            }
545            rvec.s()
546    }
547}
548)}
549// end u8
550
551gen_u8_link!(LinkedList;push_front);
552gen_u8_link!(BTreeSet;insert);
553
554// when specialize is merge
555// impl<T:Iterator<Item=f64>+ExactSizeIterator> IntoR for T{
556//    fn intor(&self) -> RResult<SEXP> {
557//        let size_x = self.len();
558//        unsafe {
559//            let rvec = Shield::new(Rf_allocVector(REALSXP, size_x as R_xlen_t));
560//            let rptr = REAL(rvec.s());
561//            let mut index = 0;
562//
563//            for ii in self {
564//                *rptr.offset(index) = ii.clone() as ::std::os::raw::c_double ;
565// 				index = index + 1;
566//            }
567//            return Ok(rvec.s());
568//        }
569//
570//    }
571// }
572
573// hashmap ////////////////////////
574impl<D: IntoR> IntoR for HashMap<String, D> {
575    fn intor(&self) -> RResult<SEXP> {
576        let size_x = self.len();
577        unsafe {
578            let name = Shield::new(Rf_allocVector(STRSXP, size_x as R_xlen_t));
579            let names = name.s();
580            let rvec = Shield::new(Rf_allocVector(VECSXP, size_x as R_xlen_t));
581            let rvecs = rvec.s();
582            let mut index = 0;
583            for (mname, mitem) in self {
584                let strs: &str = mname.as_ref();
585                SET_STRING_ELT(names, index, Rf_mkChar(try!(CString::new(strs)).as_ptr()));
586                SET_VECTOR_ELT(rvecs, index, try!(mitem.intor()));
587                index = index + 1;
588            }
589            Rf_setAttrib(rvecs, R_NamesSymbol, names);
590            Ok(rvecs)
591        }
592    }
593}
594
595impl<D: RNew> RNew for HashMap<String, D> {
596    fn rnew(x: SEXP) -> RResult<HashMap<String, D>> {
597
598        unsafe {
599            let names = Rf_getAttrib(x, R_NamesSymbol);
600            if Rf_isString(names) != Rboolean::TRUE || RTYPEOF(x) != VECSXP {
601                return rerror(REKind::NotCompatible("expecting a named list".into()));
602            }
603            let lens = Rf_xlength(x);
604            let mut res = HashMap::new();
605
606            let selfs = x.s();
607
608            for ii in 0..lens {
609                res.insert(try!(CStr::from_ptr(R_CHAR(STRING_ELT(names, ii as R_xlen_t)))
610                                    .to_owned()
611                                    .into_string()),
612                           try!(D::rnew(VECTOR_ELT(selfs, ii as R_xlen_t))));
613            }
614            Ok(res)
615        }
616    }
617}
618
619// hashmap cstring
620impl<D: IntoR> IntoR for HashMap<CString, D> {
621    fn intor(&self) -> RResult<SEXP> {
622        let size_x = self.len();
623        unsafe {
624            let name = Shield::new(Rf_allocVector(STRSXP, size_x as R_xlen_t));
625            let names = name.s();
626            let rvec = Shield::new(Rf_allocVector(VECSXP, size_x as R_xlen_t));
627            let rvecs = rvec.s();
628            let mut index = 0;
629            for (mname, mitem) in self {
630                SET_STRING_ELT(names, index, Rf_mkChar(mname.as_ptr()));
631                SET_VECTOR_ELT(rvecs, index, try!(mitem.intor()));
632                index = index + 1;
633            }
634            Rf_setAttrib(rvecs, R_NamesSymbol, names);
635            Ok(rvecs)
636        }
637    }
638}
639
640impl<D: RNew> RNew for HashMap<CString, D> {
641    fn rnew(x: SEXP) -> RResult<HashMap<CString, D>> {
642
643        unsafe {
644            let names = Rf_getAttrib(x, R_NamesSymbol);
645            if Rf_isString(names) != Rboolean::TRUE || RTYPEOF(x) != VECSXP {
646                return rerror(REKind::NotCompatible("expecting a named list".into()));
647            }
648            let lens = Rf_xlength(x);
649            let mut res = HashMap::new();
650
651            let selfs = x.s();
652
653            for ii in 0..lens {
654                res.insert(CStr::from_ptr(R_CHAR(STRING_ELT(names, ii as R_xlen_t))).to_owned(),
655                           try!(D::rnew(VECTOR_ELT(selfs, ii as R_xlen_t))));
656            }
657            Ok(res)
658        }
659    }
660}
661
662// btreemap /////////////////////////
663
664impl<D: IntoR> IntoR for BTreeMap<String, D> {
665    fn intor(&self) -> RResult<SEXP> {
666        let size_x = self.len();
667        unsafe {
668            let name = Shield::new(Rf_allocVector(STRSXP, size_x as R_xlen_t));
669            let names = name.s();
670            let rvec = Shield::new(Rf_allocVector(VECSXP, size_x as R_xlen_t));
671            let rvecs = rvec.s();
672            let mut index = 0;
673            for (mname, mitem) in self {
674                let strs: &str = mname.as_ref();
675                SET_STRING_ELT(names, index, Rf_mkChar(try!(CString::new(strs)).as_ptr()));
676                SET_VECTOR_ELT(rvecs, index, try!(mitem.intor()));
677                index = index + 1;
678            }
679            Rf_setAttrib(rvecs, R_NamesSymbol, names);
680            Ok(rvecs)
681        }
682    }
683}
684
685impl<D: RNew> RNew for BTreeMap<String, D> {
686    fn rnew(x: SEXP) -> RResult<BTreeMap<String, D>> {
687
688        unsafe {
689            let names = Rf_getAttrib(x, R_NamesSymbol);
690            if Rf_isString(names) != Rboolean::TRUE || RTYPEOF(x) != VECSXP {
691                return rerror(REKind::NotCompatible("expecting a named list".into()));
692            }
693            let lens = Rf_xlength(x);
694            let mut res = BTreeMap::new();
695
696            let selfs = x.s();
697
698            for ii in 0..lens {
699                res.insert(try!(CStr::from_ptr(R_CHAR(STRING_ELT(names, ii as R_xlen_t)))
700                                    .to_owned()
701                                    .into_string()),
702                           try!(D::rnew(VECTOR_ELT(selfs, ii as R_xlen_t))));
703            }
704            Ok(res)
705        }
706    }
707}
708
709// BTreeMap cstring
710impl<D: IntoR> IntoR for BTreeMap<CString, D> {
711    fn intor(&self) -> RResult<SEXP> {
712        let size_x = self.len();
713        unsafe {
714            let name = Shield::new(Rf_allocVector(STRSXP, size_x as R_xlen_t));
715            let names = name.s();
716            let rvec = Shield::new(Rf_allocVector(VECSXP, size_x as R_xlen_t));
717            let rvecs = rvec.s();
718            let mut index = 0;
719            for (mname, mitem) in self {
720                SET_STRING_ELT(names, index, Rf_mkChar(mname.as_ptr()));
721                SET_VECTOR_ELT(rvecs, index, try!(mitem.intor()));
722                index = index + 1;
723            }
724            Rf_setAttrib(rvecs, R_NamesSymbol, names);
725            Ok(rvecs)
726        }
727    }
728}
729
730impl<D: RNew> RNew for BTreeMap<CString, D> {
731    fn rnew(x: SEXP) -> RResult<BTreeMap<CString, D>> {
732
733        unsafe {
734            let names = Rf_getAttrib(x, R_NamesSymbol);
735            if Rf_isString(names) != Rboolean::TRUE || RTYPEOF(x) != VECSXP {
736                return rerror(REKind::NotCompatible("expecting a named list".into()));
737            }
738            let lens = Rf_xlength(x);
739            let mut res = BTreeMap::new();
740
741            let selfs = x.s();
742
743            for ii in 0..lens {
744                res.insert(CStr::from_ptr(R_CHAR(STRING_ELT(names, ii as R_xlen_t))).to_owned(),
745                           try!(D::rnew(VECTOR_ELT(selfs, ii as R_xlen_t))));
746            }
747            Ok(res)
748        }
749    }
750}
751
752
753
754
755// ask your self fromr or rnew
756// string ////////////////////////////////
757impl URNew for CString {
758    unsafe fn urnew(x: SEXP) -> CString {
759
760        let rptr: SEXP = STRING_ELT(x, 0);
761        let res = CStr::from_ptr(R_CHAR(rptr));
762        res.to_owned()
763
764    }
765}
766
767impl RNew for CString {
768    fn rnew(x: SEXP) -> RResult<CString> {
769        unsafe {
770            if RTYPEOF(x) != STRSXP || Rf_xlength(x) != 1 {
771                return rerror(REKind::NotCompatible("expecting a string".into()));
772            }
773            Ok(Self::urnew(x))
774        }
775    }
776}
777
778impl RNew for String {
779    fn rnew(x: SEXP) -> RResult<String> {
780        let res = try!(CString::rnew(x));
781        let res_string: String = try!(res.into_string());
782        Ok(res_string)
783    }
784}
785
786
787impl IntoR for CString {
788    fn intor(&self) -> RResult<SEXP> {
789        unsafe { Ok(Self::uintor(self)) }
790    }
791}
792
793impl UIntoR for CString {
794    unsafe fn uintor(&self) -> SEXP {
795
796        let rvec = Shield::new(Rf_allocVector(STRSXP, 1));
797        SET_STRING_ELT(rvec.s(), 0, Rf_mkChar(self.as_ptr()));
798        rvec.s()
799
800    }
801}
802
803impl IntoR for String {
804    fn intor(&self) -> RResult<SEXP> {
805        unsafe {
806            let rvec = Shield::new(Rf_allocVector(STRSXP, 1));
807            let strs: &str = self.as_ref();
808            SET_STRING_ELT(rvec.s(), 0, Rf_mkChar(try!(CString::new(strs)).as_ptr()));
809            Ok(rvec.s())
810        }
811    }
812}
813
814impl<'a> IntoR for &'a str {
815    fn intor(&self) -> RResult<SEXP> {
816        unsafe {
817            let rvec = Shield::new(Rf_allocVector(STRSXP, 1));
818            SET_STRING_ELT(rvec.s(), 0, Rf_mkChar(try!(CString::new(*self)).as_ptr()));
819            Ok(rvec.s())
820        }
821    }
822}
823
824impl<'a> UIntoR for &'a str {
825    unsafe fn uintor(&self) -> SEXP {
826        let rvec = Shield::new(Rf_allocVector(STRSXP, 1));
827        SET_STRING_ELT(rvec.s(), 0, Rf_mkChar(c_str(*self).as_ptr()));
828        rvec.s()
829    }
830}
831
832// string collections macro
833
834macro_rules! gen_string_collections{
835	( $(($x:ident;$push:ident)),*) =>
836	(
837$(
838	
839impl UIntoR for $x<CString> {
840    unsafe fn uintor(&self) -> SEXP {
841    	let lens = self.len();
842            let rvec = Shield::new(Rf_allocVector(STRSXP, lens as R_xlen_t));
843            let mut index = 0;
844            for xs in self{
845            	SET_STRING_ELT( rvec.s(), index, Rf_mkChar(xs.as_ptr()));
846            	index = index + 1;
847            } 
848            rvec.s()
849    }
850}
851
852impl IntoR for $x<CString> {
853    fn intor(&self) -> RResult<SEXP> {
854    	unsafe{Ok(Self::uintor(self))}
855    }
856}
857
858impl IntoR for $x<String> {
859    fn intor(&self) -> RResult<SEXP> {
860    	let lens = self.len();
861        unsafe {
862            let rvec = Shield::new(Rf_allocVector(STRSXP, lens as R_xlen_t));
863            let mut index = 0;
864            for xs in self{
865            	let strs :&str = xs.as_ref();
866            	SET_STRING_ELT( rvec.s(), index, Rf_mkChar(try!(CString::new(strs)).as_ptr()));
867            	index = index + 1;
868            } 
869           Ok(rvec.s())
870        }
871    }
872}
873
874impl<'a> IntoR for $x<&'a str> {
875    fn intor(&self) -> RResult<SEXP> {
876    	let lens = self.len();
877        unsafe {
878            let rvec = Shield::new(Rf_allocVector(STRSXP, lens as R_xlen_t));
879            let mut index = 0;
880            for xs in self{
881            	let strs :&str = xs.as_ref();
882            	SET_STRING_ELT( rvec.s(), index, Rf_mkChar(try!(CString::new(strs)).as_ptr()));
883            	index = index + 1;
884            } 
885           Ok(rvec.s())
886        }
887    }
888}
889
890impl URNew for $x<CString> {
891	unsafe fn urnew(x: SEXP) -> $x<CString> {
892			let lens = Rf_xlength(x);
893			let mut vecs = $x::with_capacity(lens as usize);
894			for ii in 0..lens{
895				let rptr : SEXP = STRING_ELT(x,ii);
896				let res = CStr::from_ptr(R_CHAR(rptr));
897				vecs.$push(res.to_owned());
898			}
899			vecs
900	}
901}
902
903
904impl RNew for $x<CString> {
905	fn rnew(x: SEXP) -> RResult<$x<CString>> {
906		unsafe {
907			if  RTYPEOF(x) != STRSXP {
908    			return rerror(REKind::NotCompatible(
909    					concat!(stringify!($x),"<CString>::rnew expecting a string").into()
910    			));
911			}
912			Ok(Self::urnew(x))
913		}
914	}
915}
916
917impl RNew for $x<String> {
918	fn rnew(x:SEXP) -> RResult<$x<String>> {
919		unsafe {
920			if RTYPEOF(x) != STRSXP  {
921    			return rerror(REKind::NotCompatible(
922    					concat!(stringify!($x),"<String>::rnew expecting a string").into()));
923			}
924			
925			let lens = Rf_xlength(x);
926			let mut vecs = $x::with_capacity(lens as usize);
927			for ii in 0..lens{
928				let rptr : SEXP = STRING_ELT(x,ii);
929				let res = CStr::from_ptr(R_CHAR(rptr));
930				vecs.$push(try!(res.to_owned().into_string()));
931			}
932			Ok(vecs)
933		}
934	}
935})*)
936}
937
938gen_string_collections!((Vec;push),(VecDeque;push_back),(BinaryHeap;push),(HashSet;insert));
939
940
941// borrow
942
943impl<'a> UIntoR for &'a [CString] {
944    unsafe fn uintor(&self) -> SEXP {
945        let lens = self.len();
946        let rvec = Shield::new(Rf_allocVector(STRSXP, lens as R_xlen_t));
947        let mut index = 0;
948        for xs in *self {
949            SET_STRING_ELT(rvec.s(), index, Rf_mkChar(xs.as_ptr()));
950            index = index + 1;
951        }
952        rvec.s()
953    }
954}
955
956impl<'a> IntoR for &'a [CString] {
957    fn intor(&self) -> RResult<SEXP> {
958        unsafe { Ok(Self::uintor(self)) }
959    }
960}
961
962impl<'a> IntoR for &'a [String] {
963    fn intor(&self) -> RResult<SEXP> {
964        let lens = self.len();
965        unsafe {
966            let rvec = Shield::new(Rf_allocVector(STRSXP, lens as R_xlen_t));
967            let mut index = 0;
968            for xs in *self {
969                let strs: &str = xs.as_ref();
970                SET_STRING_ELT(rvec.s(),
971                               index,
972                               Rf_mkChar(try!(CString::new(strs)).as_ptr()));
973                index = index + 1;
974            }
975            Ok(rvec.s())
976        }
977    }
978}
979
980impl<'a, 'b> IntoR for &'a [&'b str] {
981    fn intor(&self) -> RResult<SEXP> {
982        let lens = self.len();
983        unsafe {
984            let rvec = Shield::new(Rf_allocVector(STRSXP, lens as R_xlen_t));
985            let mut index = 0;
986            for xs in *self {
987                SET_STRING_ELT(rvec.s(), index, Rf_mkChar(try!(CString::new(*xs)).as_ptr()));
988                index = index + 1;
989            }
990            Ok(rvec.s())
991        }
992    }
993}
994
995macro_rules! gen_string_collections_linkedlist{
996	( $(($x:ident;$push:ident)),*) =>
997	(
998$(
999
1000impl UIntoR for $x<CString> {
1001    unsafe fn uintor(&self) -> SEXP {
1002    	let lens = self.len();
1003            let rvec = Shield::new(Rf_allocVector(STRSXP, lens as R_xlen_t));
1004            let mut index = 0;
1005            for xs in self{
1006            	SET_STRING_ELT( rvec.s(), index, Rf_mkChar(xs.as_ptr()));
1007            	index = index + 1;
1008            } 
1009            rvec.s()
1010    }
1011}
1012
1013
1014impl IntoR for $x<CString> {
1015    fn intor(&self) -> RResult<SEXP> {
1016    	
1017            unsafe{ Ok(Self::uintor(self))}
1018    }
1019}
1020
1021impl IntoR for $x<String> {
1022    fn intor(&self) -> RResult<SEXP> {
1023    	let lens = self.len();
1024        unsafe {
1025            let rvec = Shield::new(Rf_allocVector(STRSXP, lens as R_xlen_t));
1026            let mut index = 0;
1027            for xs in self{
1028            	let strs :&str = xs.as_ref();
1029            	SET_STRING_ELT( rvec.s(), index, Rf_mkChar(try!(CString::new(strs)).as_ptr()));
1030            	index = index + 1;
1031            } 
1032            Ok(rvec.s())
1033        }
1034    }
1035}
1036
1037impl<'a> IntoR for $x<&'a str> {
1038    fn intor(&self) -> RResult<SEXP> {
1039    	let lens = self.len();
1040        unsafe {
1041            let rvec = Shield::new(Rf_allocVector(STRSXP, lens as R_xlen_t));
1042            let mut index = 0;
1043            for xs in self{
1044            	let strs :&str = xs.as_ref();
1045            	SET_STRING_ELT( rvec.s(), index, Rf_mkChar(try!(CString::new(strs)).as_ptr()));
1046            	index = index + 1;
1047            } 
1048            Ok(rvec.s())
1049        }
1050    }
1051}
1052
1053impl URNew for $x<CString> {
1054	unsafe fn urnew(x: SEXP) -> $x<CString> {
1055
1056			let lens = Rf_xlength(x);
1057			let mut vecs = $x::new();
1058			for ii in 0..lens{
1059				let rptr : SEXP = STRING_ELT(x,ii);
1060				let res = CStr::from_ptr(R_CHAR(rptr));
1061				vecs.$push(res.to_owned());
1062			}
1063			vecs
1064		}
1065}
1066
1067impl RNew for $x<CString> {
1068	fn rnew(x: SEXP) -> RResult<$x<CString>> {
1069		unsafe {
1070			if  RTYPEOF(x) != STRSXP  {
1071    			return rerror(REKind::NotCompatible(
1072    					concat!(stringify!($x),"<CString>::rnew expecting a string").into()
1073    			));
1074			}
1075
1076			Ok(Self::urnew(x))
1077		}
1078	}
1079}
1080
1081impl RNew for $x<String> {
1082	fn rnew(x:SEXP) -> RResult<$x<String>> {
1083		unsafe {
1084			if RTYPEOF(x) != STRSXP {
1085    			return rerror(REKind::NotCompatible(
1086    					concat!(stringify!($x),"<String>::rnew expecting a string").into()));
1087			}
1088			
1089			let lens = Rf_xlength(x);
1090			let mut vecs = $x::new();
1091			for ii in 0..lens{
1092				let rptr : SEXP = STRING_ELT(x,ii);
1093				let res = CStr::from_ptr(R_CHAR(rptr));
1094				vecs.$push(try!(res.to_owned().into_string()));
1095			}
1096			Ok(vecs)
1097		}
1098	}
1099})*)
1100}
1101
1102gen_string_collections_linkedlist!((BTreeSet;insert),(LinkedList;push_front));