rustr/traits/
slot.rs

1use ::rdll::*;
2use traits::*;
3use error::{rerror, RResult};
4use error::REKind::{NotS4, NoSuchSlot};
5use symbol::*;
6
7
8pub trait RSlot :ToSEXP{
9    fn get_slot<D: RNew>(&self, slot: &str) -> RResult<D> {
10        unsafe {
11            let name = Symbol::from(slot);
12            if Rf_isS4(self.s()) == Rboolean::FALSE {
13                return rerror(NotS4("fail to get slot".into()));
14            }
15            if R_has_slot(self.s(), name.s()) == 0 {
16                return rerror(NoSuchSlot("fail to get slot".into()));
17            }
18            RNew::rnew(R_do_slot(self.s(), name.s()))
19        }
20    }
21    fn set_slot<T: ToSEXP, D: RNew>(&self, slot: &str, x: T) -> RResult<D> {
22        unsafe {
23            let name = Symbol::from(slot);
24            if Rf_isS4(self.s()) == Rboolean::FALSE {
25                return rerror(NotS4("fail to get slot".into()));
26            }
27            if R_has_slot(self.s(), name.s()) == 0 {
28                return rerror(NoSuchSlot("fail to get slot".into()));
29            }
30            RNew::rnew(R_do_slot_assign(self.s(), name.s(), x.s()))
31        }
32    }
33    fn has_slot(&self, slot: &str) -> RResult<bool> {
34        unsafe {
35            let name = Symbol::from(slot);
36            if Rf_isS4(self.s()) == Rboolean::FALSE {
37                return rerror(NotS4("fail to get slot".into()));
38            }
39            if R_has_slot(self.s(), name.s()) == 0 {
40                return Ok(false);
41            }
42            Ok(true)
43
44        }
45    }
46}