1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
use super::*;
#[derive(Clone)]
pub struct Rstr {
pub(crate) robj: Robj,
}
pub(crate) unsafe fn sexp_to_str(sexp: SEXP) -> &'static str {
if sexp == R_NaString {
<&str>::na()
} else {
std::mem::transmute(to_str(R_CHAR(sexp) as *const u8))
}
}
impl Rstr {
pub fn from_string(val: &str) -> Self {
Rstr {
robj: Robj::from_sexp(str_to_character(val)),
}
}
pub fn as_str(&self) -> &str {
unsafe { sexp_to_str(self.robj.get()) }
}
}
impl AsRef<str> for Rstr {
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl From<String> for Rstr {
fn from(s: String) -> Self {
Rstr::from_string(&s)
}
}
impl From<&str> for Rstr {
fn from(s: &str) -> Self {
Rstr::from_string(s)
}
}
impl Deref for Rstr {
type Target = str;
fn deref(&self) -> &Self::Target {
self.as_str()
}
}
impl<T> PartialEq<T> for Rstr
where
T: AsRef<str>,
{
fn eq(&self, other: &T) -> bool {
self.as_str() == other.as_ref()
}
}
impl PartialEq<str> for Rstr {
fn eq(&self, other: &str) -> bool {
self.as_str() == other
}
}
impl std::fmt::Debug for Rstr {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let s = self.as_str();
write!(f, "{:?}", s)
}
}
impl std::fmt::Display for Rstr {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let s = self.as_str();
write!(f, "{}", s)
}
}
impl CanBeNA for Rstr {
fn is_na(&self) -> bool {
unsafe { self.robj.get() == R_NaString }
}
fn na() -> Self {
unsafe {
Self {
robj: Robj::from_sexp(R_NaString),
}
}
}
}