extendr_api/robj/into_robj/repeat_into_robj.rs
1use super::*;
2use extendr_ffi::Rcomplex;
3//TODO: It could be an idea to make this a attribute macro for users,
4// if they wish to specialise their own types as representable in R.
5
6/// Generates a [`ToVectorValue`] for a type, by inheriting the properties
7/// of another type's [`ToVectorValue`]'s implementation.
8///
9/// This is meant to be used for wrappers of types that may be represented
10/// in R. The marshalling rules for this is represented in `ToVectorValue`,
11/// and this macro merely co-opts
12///
13/// Arguments:
14///
15/// * `$type` - Target type
16/// * `$synonym_type` - A type that has a `ToVectorValue`-impl, and an `inner`-method.
17///
18/// Requirements: `$type` must have an `inner`-method to extract the
19/// wrapped value. It suffices that `$type` implements `Scalar<T>`.
20///
21/// Example usage:
22///
23/// ```ignore
24/// impl_synonym_type(Rint, i32);
25/// ```
26///
27/// The example here implements
28///
29/// `impl ToVectorValue for Rint`,
30///
31/// this entails that `Rint` would be stored in `R` exactly
32/// as `i32`.
33///
34macro_rules! impl_synonym_type {
35 ($type: ty, $synonym_type: ty) => {
36 impl ToVectorValue for $type {
37 fn sexptype() -> SEXPTYPE {
38 <$synonym_type as ToVectorValue>::sexptype()
39 }
40
41 fn to_real(&self) -> f64
42 where
43 Self: Sized,
44 {
45 <$synonym_type as ToVectorValue>::to_real(&self.inner())
46 }
47
48 fn to_complex(&self) -> Rcomplex
49 where
50 Self: Sized,
51 {
52 <$synonym_type as ToVectorValue>::to_complex(&self.inner())
53 }
54
55 fn to_integer(&self) -> i32
56 where
57 Self: Sized,
58 {
59 <$synonym_type as ToVectorValue>::to_integer(&self.inner())
60 }
61
62 fn to_logical(&self) -> i32
63 where
64 Self: Sized,
65 {
66 <$synonym_type as ToVectorValue>::to_logical(&self.inner())
67 }
68
69 fn to_raw(&self) -> u8
70 where
71 Self: Sized,
72 {
73 <$synonym_type as ToVectorValue>::to_raw(&self.inner())
74 }
75
76 fn to_sexp(&self) -> SEXP
77 where
78 Self: Sized,
79 {
80 <$synonym_type as ToVectorValue>::to_sexp(&self.inner())
81 }
82 }
83 };
84}
85impl_synonym_type!(Rfloat, f64);
86impl_synonym_type!(&Rfloat, f64);
87impl_synonym_type!(Rint, i32);
88impl_synonym_type!(&Rint, i32);