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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#[cfg(feature = "compile-with-external-structures")]
use crate::containers::ExternalList;
#[cfg(feature = "compile-with-external-structures")]
type List<T> = ExternalList<T>;
#[cfg(not(feature = "compile-with-external-structures"))]
type List<T> = Vec<T>;

/// Trait with common methods of Bytes (Rust- or external-based)
pub trait BytesTrait:
    std::ops::Index<usize> + Default + std::fmt::Debug + Clone + PartialEq + Eq
{
    /// Constructs Bytes based on a given vector
    fn new(raw: Vec<u8>) -> Self;

    /// Returns a reference to inner data
    fn as_raw(&self) -> &[u8];
    /// "Unwraps" self and returns inner data
    fn into_raw(self) -> List<u8>
    where
        Self: Sized;
    /// Replaces inner data with given list
    fn set_raw(&mut self, raw: List<u8>);

    /// Constructs an empty instance of `Bytes`
    fn empty() -> Self
    where
        Self: Sized,
    {
        Self::new(vec![])
    }

    /// Converts byte sequence to a string slice, returns error if there are invalid UTF-8 chars
    fn as_str_lossy(&self) -> Result<&str, std::str::Utf8Error> {
        std::str::from_utf8(self.as_raw())
    }

    /// Converts byte sequnce to a string, all invalid UTF-8 chars are converted into "replacement char"
    fn to_string_lossy(&self) -> String {
        String::from_utf8_lossy(self.as_raw()).into_owned()
    }

    /// Converts byte sequence to a String, returns error if there are invalid UTF-8 chars
    fn to_string(&self) -> Result<String, std::string::FromUtf8Error> {
        String::from_utf8(self.as_raw().to_vec())
    }

    /// Consumes itself and convrters it into a string, returns error if there are invalid UTF-8 chars
    fn into_string(self) -> Result<String, std::string::FromUtf8Error>
    where
        Self: Sized,
    {
        String::from_utf8(self.into_raw().into())
    }

    /// Returns `true` if `self` represents a valid UTF-8 string
    fn is_valid_utf8(&self) -> bool {
        std::str::from_utf8(self.as_raw()).is_ok()
    }

    /// Returns `true` if byte sequence is empty
    fn is_empty(&self) -> bool {
        self.as_raw().is_empty()
    }

    /// Returns length of the byte sequence
    fn len(&self) -> usize {
        self.as_raw().len()
    }

    /// Clears inner data
    fn clear(&mut self) {
        self.set_raw(List::<u8>::new())
    }

    /// Appends a byte
    fn push(&mut self, byte: u8);
}

#[cfg(not(feature = "compile-with-external-structures"))]
mod bytes {
    use super::{BytesTrait, List};

    /// Representation of a byte sequence, see [`BytesTrait`] for available methods
    #[derive(Debug, Clone, PartialEq, Eq)]
    #[repr(C)]
    pub struct Bytes {
        /// Raw vector of bytes
        pub raw: List<u8>,
    }

    impl Default for Bytes {
        fn default() -> Self {
            Self::new(vec![])
        }
    }

    impl BytesTrait for Bytes {
        fn new(raw: Vec<u8>) -> Self {
            Self { raw: raw.into() }
        }

        fn as_raw(&self) -> &[u8] {
            &self.raw
        }

        fn into_raw(self) -> List<u8> {
            self.raw
        }

        fn set_raw(&mut self, raw: List<u8>) {
            self.raw = raw
        }

        fn push(&mut self, item: u8) {
            self.raw.push(item);
        }
    }

    impl std::ops::Index<usize> for Bytes {
        type Output = u8;

        fn index(&self, index: usize) -> &Self::Output {
            self.raw.index(index)
        }
    }
}

#[cfg(feature = "compile-with-external-structures")]
pub(crate) mod bytes {
    use super::BytesTrait;
    use crate::containers::size::BYTES_SIZE;

    #[repr(C)]
    #[derive(Clone, Copy)]
    pub(crate) struct BytesBlob {
        blob: [u8; BYTES_SIZE],
    }

    /// Byte sequence based on external implementation
    #[repr(C)]
    pub struct Bytes {
        pub(crate) blob: BytesBlob,
    }

    use crate::containers::list::external::{List, ListBlob};

    extern "C" {
        fn lib_ruby_parser_bytes_blob_from_list_blob(list_blob: ListBlob) -> BytesBlob;
        fn lib_ruby_parser_bytes_blob_free(bytes_blob: BytesBlob);
        fn lib_ruby_parser_bytes_blob_new() -> BytesBlob;
        fn lib_ruby_parser_list_blob_from_bytes_blob(bytes_blob: BytesBlob) -> ListBlob;
    }

    impl Drop for Bytes {
        fn drop(&mut self) {
            unsafe { lib_ruby_parser_bytes_blob_free(self.blob) }
            self.blob = unsafe { lib_ruby_parser_bytes_blob_new() };
        }
    }

    impl Default for Bytes {
        fn default() -> Self {
            Self::new(vec![])
        }
    }

    impl Eq for Bytes {}

    impl PartialEq for Bytes {
        fn eq(&self, other: &Self) -> bool {
            self.as_raw() == other.as_raw()
        }
    }

    impl std::fmt::Debug for Bytes {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            f.debug_struct("Bytes")
                .field("raw", &self.as_raw())
                .finish()
        }
    }

    impl Clone for Bytes {
        fn clone(&self) -> Self {
            Self::new(self.as_raw().to_vec())
        }
    }

    impl BytesTrait for Bytes {
        fn new(raw: Vec<u8>) -> Self {
            let list: List<u8> = raw.into();
            let list_blob: ListBlob = list.into();
            let bytes_blob = unsafe { lib_ruby_parser_bytes_blob_from_list_blob(list_blob) };
            Self { blob: bytes_blob }
        }

        fn as_raw(&self) -> &[u8] {
            let list_blob = unsafe { lib_ruby_parser_list_blob_from_bytes_blob(self.blob) };
            let list: List<u8> = list_blob.into();
            let slice = unsafe { std::slice::from_raw_parts(list.as_ptr(), list.len()) };
            std::mem::forget(list);
            slice
        }

        fn into_raw(mut self) -> List<u8> {
            let list_blob = unsafe { lib_ruby_parser_list_blob_from_bytes_blob(self.blob) };
            self.blob = unsafe { lib_ruby_parser_bytes_blob_new() };
            list_blob.into()
        }

        fn set_raw(&mut self, raw: List<u8>) {
            let list_blob = unsafe { lib_ruby_parser_list_blob_from_bytes_blob(self.blob) };
            let list: List<u8> = list_blob.into();
            drop(list);

            let list_blob: ListBlob = raw.into();
            self.blob = unsafe { lib_ruby_parser_bytes_blob_from_list_blob(list_blob) };
        }

        fn push(&mut self, byte: u8) {
            let bytes_blob = self.blob;
            let list_blob = unsafe { lib_ruby_parser_list_blob_from_bytes_blob(bytes_blob) };
            let mut list: List<u8> = list_blob.into();
            list.push(byte);
            let list_blob: ListBlob = list.into();
            let bytes_blob = unsafe { lib_ruby_parser_bytes_blob_from_list_blob(list_blob) };
            self.blob = bytes_blob;
        }
    }

    impl Bytes {
        pub(crate) fn into_blob(mut self) -> BytesBlob {
            let result = self.blob;
            self.blob = unsafe { lib_ruby_parser_bytes_blob_new() };
            result
        }
    }

    impl std::ops::Index<usize> for Bytes {
        type Output = u8;

        fn index(&self, index: usize) -> &Self::Output {
            self.as_raw().index(index)
        }
    }

    #[cfg(test)]
    mod tests {
        use super::{Bytes, BytesTrait, List, BYTES_SIZE};

        #[test]
        fn test_size() {
            assert_eq!(std::mem::size_of::<Bytes>(), BYTES_SIZE);
        }

        #[test]
        fn test_new() {
            let bytes = Bytes::new(vec![1, 2, 3]);
            drop(bytes);
        }

        #[test]
        fn test_as_raw() {
            let bytes = Bytes::new(vec![1, 2, 3]);

            assert_eq!(bytes.as_raw(), &[1, 2, 3])
        }

        #[test]
        fn test_into_raw() {
            let bytes = Bytes::new(vec![1, 2, 3]);

            assert_eq!(bytes.into_raw(), List::<u8>::from(vec![1, 2, 3]))
        }

        #[test]
        fn test_set_raw() {
            let mut bytes = Bytes::new(vec![1, 2, 3]);
            bytes.set_raw(vec![4, 5, 6].into());

            assert_eq!(bytes.as_raw(), &[4, 5, 6])
        }

        #[test]
        fn test_push() {
            let mut bytes = Bytes::default();
            for i in 0..10 {
                bytes.push(i);
            }
            assert_eq!(bytes.as_raw(), &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
        }
    }
}

pub use bytes::Bytes;