algorithm/buf/
binary_ref.rs

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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
// Copyright 2022 - 2023 Wenmeng See the COPYRIGHT
// file at the top-level directory of this distribution.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//
// Author: tickbh
// -----
// Created Date: 2023/09/14 12:21:02

use std::fmt::Debug;
use std::io::{self, Error};
use std::marker::PhantomData;
use std::ops::Deref;
use std::{borrow::Borrow, cmp, hash, io::Read, io::Result, slice};

use super::Binary;

use super::Bt;

static EMPTY_ARRAY: &[u8] = &[];

/// 二进制引用的封装, 只针对引用
pub struct BinaryRef<'a> {
    ptr: *const u8,
    // 游标值, 可以得出当前指向的位置
    cursor: usize,
    // 标记值, 从上一次标记到现在的游标值, 可以得出偏移的对象
    mark: usize,
    // 长度值, 还剩下多少的长度
    len: usize,

    data: PhantomData<&'a ()>,
}

impl<'a> BinaryRef<'a> {
    pub fn new() -> BinaryRef<'a> {
        BinaryRef::from(EMPTY_ARRAY)
    }

    /// # Examples
    ///
    /// ```
    /// use algorithm::buf::Binary;
    ///
    /// let b = Binary::from(&b"hello"[..]);
    /// assert_eq!(b.len(), 5);
    /// ```
    ///
    pub fn len(&self) -> usize {
        self.len
    }

    /// Returns true if the `Binary` has a length of 0.
    ///
    /// # Examples
    ///
    /// ```
    /// use algorithm::buf::Binary;
    ///
    /// let b = Binary::new();
    /// assert!(b.is_empty());
    /// ```
    #[inline]
    pub const fn is_empty(&self) -> bool {
        self.len == 0
    }

    pub fn to_vec(&self) -> Vec<u8> {
        unsafe { slice::from_raw_parts(self.ptr, self.len).to_vec() }
    }

    #[inline]
    fn as_slice(&self) -> &[u8] {
        unsafe { slice::from_raw_parts(self.ptr, self.len) }
    }

    #[inline]
    unsafe fn inc_start(&mut self, by: usize) {
        if by == 0 {
            return;
        }
        // should already be asserted, but debug assert for tests
        debug_assert!(self.len >= by, "internal: inc_start out of bounds");
        self.len -= by;
        self.ptr = self.ptr.add(by);
        self.cursor += by;
    }

    // #[inline]
    // unsafe fn sub_start(&mut self, by: usize) {
    //     // should already be asserted, but debug assert for tests
    //     debug_assert!(self.cursor >= by, "internal: inc_start out of bounds");
    //     self.len += by;
    //     self.ptr = self.ptr.sub(by);
    //     self.cursor -= by;
    //     self.mark = std::cmp::min(self.mark, self.cursor);
    // }

    pub fn copy_from_slice(data: &'a [u8]) -> Self {
        data.into()
    }

    #[inline]
    pub fn into_slice_all(&self) -> Vec<u8> {
        self.to_vec()
    }
}

impl<'a> Clone for BinaryRef<'a> {
    fn clone(&self) -> Self {
        BinaryRef {
            ptr: self.ptr,
            cursor: self.cursor,
            mark: self.mark,
            len: self.len,
            data: self.data.clone(),
        }
    }
}

impl<'a> Drop for BinaryRef<'a> {
    fn drop(&mut self) {}
}

impl<'a> From<&'a str> for BinaryRef<'a> {
    fn from(value: &'a str) -> Self {
        BinaryRef::from(value.as_bytes())
    }
}

impl<'a> From<&'a [u8]> for BinaryRef<'a> {
    fn from(value: &'a [u8]) -> Self {
        let len = value.len();
        BinaryRef {
            ptr: value.as_ptr(),
            len,
            mark: 0,
            cursor: 0,
            data: PhantomData,
        }
    }
}

impl<'a> Bt for BinaryRef<'a> {
    fn remaining(&self) -> usize {
        self.len
    }

    fn chunk(&self) -> &[u8] {
        self.as_slice()
    }

    fn advance_chunk(&mut self, n: usize) -> &[u8] {
        let ret = &unsafe { slice::from_raw_parts(self.ptr, self.len) }[..n];
        self.advance(n);
        ret
    }

    fn advance(&mut self, n: usize) {
        unsafe {
            self.inc_start(n);
        }
    }

    fn into_binary(self) -> Binary {
        Binary::from(self.chunk().to_vec())
    }
}

impl<'a> Read for BinaryRef<'a> {
    #[inline(always)]
    fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
        let left = self.remaining();
        if left == 0 || buf.len() == 0 {
            return Err(Error::new(io::ErrorKind::WouldBlock, ""));
        }
        let read = std::cmp::min(left, buf.len());
        unsafe {
            std::ptr::copy(&self.chunk()[0], &mut buf[0], read);
        }
        self.advance(read);
        Ok(read)
    }
}

impl<'a> Iterator for BinaryRef<'a> {
    type Item = u8;
    #[inline]
    fn next(&mut self) -> Option<u8> {
        self.get_next()
    }
}

impl<'a> Deref for BinaryRef<'a> {
    type Target = [u8];

    #[inline]
    fn deref(&self) -> &[u8] {
        self.as_slice()
    }
}

impl<'a> Debug for BinaryRef<'a> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Binary")
            .field("ptr", &self.ptr)
            .field("cursor", &self.cursor)
            .field("mark", &self.mark)
            .field("len", &self.len)
            .finish()
    }
}

impl<'a> AsRef<[u8]> for BinaryRef<'a> {
    #[inline]
    fn as_ref(&self) -> &[u8] {
        self.as_slice()
    }
}

impl<'a> hash::Hash for BinaryRef<'a> {
    fn hash<H>(&self, state: &mut H)
    where
        H: hash::Hasher,
    {
        self.as_slice().hash(state);
    }
}

impl<'a> Borrow<[u8]> for BinaryRef<'a> {
    fn borrow(&self) -> &[u8] {
        self.as_slice()
    }
}

impl<'a> PartialEq for BinaryRef<'a> {
    fn eq(&self, other: &BinaryRef) -> bool {
        self.as_slice() == other.as_slice()
    }
}

impl<'a> PartialOrd for BinaryRef<'a> {
    fn partial_cmp(&self, other: &BinaryRef) -> Option<cmp::Ordering> {
        self.as_slice().partial_cmp(other.as_slice())
    }
}

impl<'a> Ord for BinaryRef<'a> {
    fn cmp(&self, other: &BinaryRef) -> cmp::Ordering {
        self.as_slice().cmp(other.as_slice())
    }
}

impl<'a> Eq for BinaryRef<'a> {}

impl<'a> PartialEq<[u8]> for BinaryRef<'a> {
    fn eq(&self, other: &[u8]) -> bool {
        self.as_slice() == other
    }
}

impl<'a> PartialOrd<[u8]> for BinaryRef<'a> {
    fn partial_cmp(&self, other: &[u8]) -> Option<cmp::Ordering> {
        self.as_slice().partial_cmp(other)
    }
}

impl<'a> PartialEq<BinaryRef<'a>> for [u8] {
    fn eq(&self, other: &BinaryRef) -> bool {
        *other == *self
    }
}

impl<'a> PartialOrd<BinaryRef<'a>> for [u8] {
    fn partial_cmp(&self, other: &BinaryRef) -> Option<cmp::Ordering> {
        <[u8] as PartialOrd<[u8]>>::partial_cmp(self, other)
    }
}

impl<'a> PartialEq<str> for BinaryRef<'a> {
    fn eq(&self, other: &str) -> bool {
        self.as_slice() == other.as_bytes()
    }
}

impl<'a> PartialOrd<str> for BinaryRef<'a> {
    fn partial_cmp(&self, other: &str) -> Option<cmp::Ordering> {
        self.as_slice().partial_cmp(other.as_bytes())
    }
}

impl<'a> PartialEq<BinaryRef<'a>> for str {
    fn eq(&self, other: &BinaryRef) -> bool {
        *other == *self
    }
}

impl<'a> PartialOrd<BinaryRef<'a>> for str {
    fn partial_cmp(&self, other: &BinaryRef) -> Option<cmp::Ordering> {
        <[u8] as PartialOrd<[u8]>>::partial_cmp(self.as_bytes(), other)
    }
}

impl<'a> PartialEq<Vec<u8>> for BinaryRef<'a> {
    fn eq(&self, other: &Vec<u8>) -> bool {
        *self == other[..]
    }
}

impl<'a> PartialOrd<Vec<u8>> for BinaryRef<'a> {
    fn partial_cmp(&self, other: &Vec<u8>) -> Option<cmp::Ordering> {
        self.as_slice().partial_cmp(&other[..])
    }
}

impl<'a> PartialEq<BinaryRef<'a>> for Vec<u8> {
    fn eq(&self, other: &BinaryRef) -> bool {
        *other == *self
    }
}

impl<'a> PartialOrd<BinaryRef<'a>> for Vec<u8> {
    fn partial_cmp(&self, other: &BinaryRef) -> Option<cmp::Ordering> {
        <[u8] as PartialOrd<[u8]>>::partial_cmp(self, other)
    }
}

impl<'a> PartialEq<String> for BinaryRef<'a> {
    fn eq(&self, other: &String) -> bool {
        *self == other[..]
    }
}

impl<'a> PartialOrd<String> for BinaryRef<'a> {
    fn partial_cmp(&self, other: &String) -> Option<cmp::Ordering> {
        self.as_slice().partial_cmp(other.as_bytes())
    }
}

impl<'a> PartialEq<BinaryRef<'a>> for String {
    fn eq(&self, other: &BinaryRef) -> bool {
        *other == *self
    }
}

impl<'a> PartialOrd<BinaryRef<'a>> for String {
    fn partial_cmp(&self, other: &BinaryRef) -> Option<cmp::Ordering> {
        <[u8] as PartialOrd<[u8]>>::partial_cmp(self.as_bytes(), other)
    }
}

impl<'a> PartialEq<BinaryRef<'a>> for &[u8] {
    fn eq(&self, other: &BinaryRef) -> bool {
        *other == *self
    }
}

impl<'a> PartialOrd<BinaryRef<'a>> for &[u8] {
    fn partial_cmp(&self, other: &BinaryRef) -> Option<cmp::Ordering> {
        <[u8] as PartialOrd<[u8]>>::partial_cmp(self, other)
    }
}

impl<'a> PartialEq<BinaryRef<'a>> for &str {
    fn eq(&self, other: &BinaryRef) -> bool {
        *other == *self
    }
}

impl<'a> PartialOrd<BinaryRef<'a>> for &str {
    fn partial_cmp(&self, other: &BinaryRef) -> Option<cmp::Ordering> {
        <[u8] as PartialOrd<[u8]>>::partial_cmp(self.as_bytes(), other)
    }
}

impl<'a, T: ?Sized> PartialEq<&'a T> for BinaryRef<'a>
where
    BinaryRef<'a>: PartialEq<T>,
{
    fn eq(&self, other: &&'a T) -> bool {
        *self == **other
    }
}

impl<'a, T: ?Sized> PartialOrd<&'a T> for BinaryRef<'a>
where
    BinaryRef<'a>: PartialOrd<T>,
{
    fn partial_cmp(&self, other: &&'a T) -> Option<cmp::Ordering> {
        self.partial_cmp(&**other)
    }
}

// impl From

impl<'a> Default for BinaryRef<'a> {
    #[inline]
    fn default() -> BinaryRef<'a> {
        BinaryRef::new()
    }
}

#[cfg(test)]
mod tests {
    use super::{BinaryRef, Bt};

    #[test]
    fn binary_refs() {
        {
            let s = BinaryRef::from("aaaa");
            let s1 = s.clone();
            drop(s1);
        }
        {
            let v = vec![1, 2];
            let mut b = BinaryRef::from(&v[..]);
            let x = b.get_u8();
            assert!(x == 1);
            let b1 = b.clone();
            drop(b1);
        }
    }
}