aorist_extendr_api/wrapper/primitive.rs
1use super::*;
2
3/// Wrapper for creating primitive objects.
4///
5/// Make a primitive object, or NULL if not available.
6/// ```
7/// use extendr_api::prelude::*;
8/// test! {
9/// let builtin = r!(Primitive::from_string("+"));
10/// let special = r!(Primitive::from_string("if"));
11/// }
12/// ```
13///
14#[derive(Debug, PartialEq, Clone)]
15pub struct Primitive {
16 pub(crate) robj: Robj,
17}
18
19impl Primitive {
20 /// Make a Primitive object from a string.
21 /// ```
22 /// use extendr_api::prelude::*;
23 /// test! {
24 /// let builtin = r!(Primitive::from_string("+")?);
25 /// let special = r!(Primitive::from_string("if")?);
26 /// assert_eq!(builtin.rtype(), RType::Builtin);
27 /// assert_eq!(special.rtype(), RType::Special);
28 /// }
29 /// ```
30 pub fn from_string(val: &str) -> Result<Self> {
31 single_threaded(|| unsafe {
32 // Primitives have a special "SYMVALUE" entry in their symbol.
33 let sym = Symbol::from_string(val);
34 let symvalue = new_owned(SYMVALUE(sym.get()));
35 if symvalue.is_primitive() {
36 Ok(Primitive { robj: symvalue })
37 } else {
38 Err(Error::ExpectedPrimitive(sym.into()))
39 }
40 })
41 }
42
43 // There is currently no way to convert a primitive to a string.
44}