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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#[cfg(not(feature = "compile-with-external-structures"))]
pub(crate) mod rust {
pub type MaybeStringPtr = Option<String>;
use super::MaybeStringPtrAPI;
impl MaybeStringPtrAPI for MaybeStringPtr {
fn some<T>(value: T) -> Self
where
T: Into<String>,
{
Some(value.into())
}
fn none() -> Self {
None
}
}
}
#[cfg(feature = "compile-with-external-structures")]
pub(crate) mod external {
use super::MaybeStringPtrAPI;
use crate::blobs::Blob;
use crate::containers::ExternalStringPtr;
#[repr(C)]
pub struct MaybeStringPtr {
pub(crate) blob: Blob<MaybeStringPtr>,
}
extern "C" {
fn lib_ruby_parser__external__maybe_string_ptr__new_some(
ptr: *const u8,
suze: u64,
) -> Blob<MaybeStringPtr>;
fn lib_ruby_parser__external__maybe_string_ptr__new_none() -> Blob<MaybeStringPtr>;
fn lib_ruby_parser__external__maybe_string_ptr__drop(blob: *mut Blob<MaybeStringPtr>);
fn lib_ruby_parser__external__maybe_string_ptr__is_some(
blob: *const Blob<MaybeStringPtr>,
) -> bool;
fn lib_ruby_parser__external__maybe_string_ptr__is_none(
blob: *const Blob<MaybeStringPtr>,
) -> bool;
fn lib_ruby_parser__external__maybe_string_ptr__get_raw(
blob: *mut Blob<MaybeStringPtr>,
) -> *mut u8;
fn lib_ruby_parser__external__maybe_string_ptr__into_raw(
blob: Blob<MaybeStringPtr>,
) -> *mut u8;
fn lib_ruby_parser__external__maybe_string_ptr__get_len(
blob: *const Blob<MaybeStringPtr>,
) -> u64;
}
impl Drop for MaybeStringPtr {
fn drop(&mut self) {
unsafe { lib_ruby_parser__external__maybe_string_ptr__drop(&mut self.blob) }
}
}
impl std::fmt::Debug for MaybeStringPtr {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Debug::fmt(&self.as_ref(), f)
}
}
impl Clone for MaybeStringPtr {
fn clone(&self) -> Self {
match self.as_ref() {
Some(s) => Self::some(s.to_string()),
None => Self::none(),
}
}
}
impl Default for MaybeStringPtr {
fn default() -> Self {
Self::none()
}
}
impl MaybeStringPtr {
pub fn is_some(&self) -> bool {
unsafe { lib_ruby_parser__external__maybe_string_ptr__is_some(&self.blob) }
}
pub fn is_none(&self) -> bool {
unsafe { lib_ruby_parser__external__maybe_string_ptr__is_none(&self.blob) }
}
pub fn unwrap(self) -> ExternalStringPtr {
if self.is_some() {
let len = unsafe {
lib_ruby_parser__external__maybe_string_ptr__get_len(&self.blob) as usize
};
let ptr =
unsafe { lib_ruby_parser__external__maybe_string_ptr__into_raw(self.blob) };
std::mem::forget(self);
let bytes = unsafe { Vec::from_raw_parts(ptr, len, len) };
let s = String::from_utf8(bytes).unwrap();
ExternalStringPtr::from(s)
} else {
panic!("failed to unwrap null MaybeStringPtr")
}
}
pub fn as_ref(&self) -> Option<&str> {
if self.is_some() {
let len = unsafe {
lib_ruby_parser__external__maybe_string_ptr__get_len(&self.blob) as usize
};
let blob_ptr: *const Blob<MaybeStringPtr> = &self.blob;
let ptr = unsafe {
lib_ruby_parser__external__maybe_string_ptr__get_raw(
blob_ptr as *mut Blob<MaybeStringPtr>,
)
};
let bytes = unsafe { std::slice::from_raw_parts(ptr, len) };
let s = std::str::from_utf8(bytes).unwrap();
Some(s)
} else {
None
}
}
pub fn as_mut(&mut self) -> Option<&mut str> {
if self.is_some() {
let len = unsafe {
lib_ruby_parser__external__maybe_string_ptr__get_len(&self.blob) as usize
};
let ptr =
unsafe { lib_ruby_parser__external__maybe_string_ptr__get_raw(&mut self.blob) };
let bytes = unsafe { std::slice::from_raw_parts_mut(ptr, len) };
let s = std::str::from_utf8_mut(bytes).unwrap();
Some(s)
} else {
None
}
}
}
impl From<Option<String>> for MaybeStringPtr {
fn from(maybe_string: Option<String>) -> Self {
match maybe_string {
Some(string) => Self::some(string),
None => Self::none(),
}
}
}
impl MaybeStringPtrAPI for MaybeStringPtr {
fn some<T>(value: T) -> Self
where
T: Into<String>,
{
let value: String = value.into();
let mut bytes = value.into_bytes();
let ptr = bytes.as_mut_ptr();
let len = bytes.len() as u64;
let blob = unsafe { lib_ruby_parser__external__maybe_string_ptr__new_some(ptr, len) };
Self { blob }
}
fn none() -> Self {
let blob = unsafe { lib_ruby_parser__external__maybe_string_ptr__new_none() };
Self { blob }
}
}
impl PartialEq<MaybeStringPtr> for MaybeStringPtr {
fn eq(&self, other: &MaybeStringPtr) -> bool {
self.as_ref() == other.as_ref()
}
}
#[cfg(test)]
mod tests {
use super::{MaybeStringPtr, MaybeStringPtrAPI};
#[test]
fn test_some() {
let s = MaybeStringPtr::some("foobar");
assert_eq!(s.as_ref(), Some("foobar"))
}
#[test]
fn test_none() {
let s = MaybeStringPtr::none();
assert_eq!(s.as_ref(), None)
}
#[test]
fn test_as_ref() {
let s = MaybeStringPtr::some("foobar");
assert_eq!(s.as_ref(), Some("foobar"))
}
}
}
pub(crate) trait MaybeStringPtrAPI {
fn some<T>(value: T) -> Self
where
T: Into<String>,
Self: Sized;
fn none() -> Self
where
Self: Sized;
}