tcio 0.1.4

Collection of utility types.
Documentation
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
//! raw bytes utilities.
use bytes::{Bytes, BytesMut};

/// Returns the pointer range of a buffer.
///
/// This is intended to be used with [`slice_of_bytes`] to keep a slice of [`BytesMut`] and
/// freezing it while keeping the slice without copying.
///
/// # Examples
///
/// ```
/// # use bytes::{BytesMut, Bytes};
/// use tcio::slice::{range_of, slice_of_bytes};
///
/// let mut bytesm = BytesMut::from(&b"Content-Type: text/html"[..]);
/// let range = range_of(&bytesm[14..]);
///
/// // `.freeze()` require mutation on `bytesm`,
/// // so any slice reference cannot pass this point;
/// //
/// // because `.split_off()` guarantees that address
/// // does not change, its possible to store pointer as index,
/// // therefore, no lifetime restriction
/// let bytes = bytesm.split_off(12).freeze();
///
/// // Shared `Bytes`, no copy
/// let content: Bytes = slice_of_bytes(range, &bytes);
///
/// assert_eq!(content, &b"text/html"[..]);
/// ```
///
/// [`BytesMut`]: bytes::BytesMut
#[inline]
pub fn range_of(buf: &[u8]) -> std::ops::Range<usize> {
    let ptr = buf.as_ptr() as usize;
    ptr..ptr + buf.len()
}

/// Returns the shared [`Bytes`] by given pointer range.
///
/// This is intented to be used with [`range_of`] to keep a slice of [`BytesMut`] and freezing it
/// while keeping the slice without copying.
///
/// # Examples
///
/// ```
/// # use bytes::{BytesMut, Bytes};
/// use tcio::slice::{range_of, slice_of_bytes};
///
/// let mut bytesm = BytesMut::from(&b"Content-Type: text/html"[..]);
/// let range = range_of(&bytesm[14..]);
///
/// // `.freeze()` require mutation on `bytesm`,
/// // so any slice reference cannot pass this point;
/// //
/// // because `.split_off()` guarantees that address
/// // does not change, its possible to store pointer as index,
/// // therefore, no lifetime restriction
/// let bytes = bytesm.split_off(12).freeze();
///
/// // Shared `Bytes`, no copy
/// let content: Bytes = slice_of_bytes(range, &bytes);
///
/// assert_eq!(content, &b"text/html"[..]);
/// ```
///
/// # Panics
///
/// Requires that the given pointer range is in fact contained within the `bytes`, otherwise this
/// function will panic.
///
/// ```should_panic
/// # use bytes::{BytesMut, Bytes};
/// use tcio::slice::{range_of, slice_of_bytes};
///
/// let mut bytesm = BytesMut::from(&b"Content-Type: text/html"[..]);
/// let range = range_of(&bytesm[14..]);
///
/// // only contains "Content-Type"
/// let bytes = bytesm.split_to(12).freeze();
///
/// // therefore it panic because pointer range is out of bounds
/// let content: Bytes = slice_of_bytes(range, &bytes);
/// ```
///
/// [`BytesMut`]: bytes::BytesMut
pub fn slice_of_bytes(range: std::ops::Range<usize>, bytes: &Bytes) -> Bytes {
    let bytes_p = bytes.as_ptr() as usize;
    let bytes_len = bytes.as_ptr() as usize;
    let sub_len = range.end.saturating_sub(range.start);
    let sub_p = range.start;

    if sub_len == 0 {
        return Bytes::new()
    }

    let Some(offset) = sub_p.checked_sub(bytes_p) else {
        // assert failed: sub_p >= bytes_p
        panic!(
            "range pointer ({:p}) is smaller than `bytes` pointer ({:p})",
            sub_p as *const u8,
            bytes.as_ptr(),
        );
    };

    assert!(
        sub_p + sub_len <= bytes_p + bytes_len,
        "subset is out of bounds: self = ({:p}, {}), subset = ({:p}, {})",
        bytes.as_ptr(),
        bytes_len,
        sub_p as *const u8,
        sub_len,
    );

    bytes.slice(offset..offset + sub_len)
}

/// Returns the splitted [`BytesMut`] by given pointer range.
///
/// Afterwards `bytes` contains elements `[range.end, len)`, and the returned [`BytesMut`] contains
/// elements `[range.start, range.end)`.
///
/// If the given pointer range is not exactly in the front of `bytes`, the leading bytes will be
/// dropped.
///
/// # Examples
///
/// ```
/// # use bytes::{BytesMut, Bytes};
/// use tcio::slice::{range_of, slice_of_bytes_mut};
///
/// let mut bytesm = BytesMut::from(&b"Content-Type: text/html"[..]);
/// let range = range_of(&bytesm[14..18]);
///
/// let mut split = bytesm.split_off(12);
///
/// let content: BytesMut = slice_of_bytes_mut(range, &mut split);
///
/// // note that `: ` is dropped
/// assert_eq!(content, &b"text"[..]);
/// assert_eq!(split, &b"/html"[..]);
/// ```
///
/// # Panics
///
/// Requires that the given pointer range is in fact contained within the `bytes`, otherwise this
/// function will panic.
pub fn slice_of_bytes_mut(range: std::ops::Range<usize>, bytes: &mut BytesMut) -> BytesMut {
    let bytes_p = bytes.as_ptr() as usize;
    let bytes_len = bytes.as_ptr() as usize;
    let sub_len = range.end.saturating_sub(range.start);
    let sub_p = range.start;

    let Some(leading_len) = sub_p.checked_sub(bytes_p) else {
        // assert failed: sub_p >= bytes_p
        panic!(
            "range pointer ({:p}) is smaller than `bytes` pointer ({:p})",
            sub_p as *const u8,
            bytes.as_ptr()
        )
    };

    assert!(
        sub_p + sub_len <= bytes_p + bytes_len,
        "subset is out of bounds: self = ({:p}, {}), subset = ({:p}, {})",
        bytes.as_ptr(),
        bytes_len,
        sub_p as *const u8,
        sub_len,
    );

    // `BytesMut::advance` have early returns if offset 0
    bytes::Buf::advance(bytes, leading_len);

    bytes.split_to(sub_len)
}

/// Returns the subset value in `buf` with returned range from [`range_of`].
///
/// # Examples
///
/// ```
/// use tcio::slice::{range_of, slice_of};
///
/// let mut bytes = b"Content-Type: text/html";
/// let range = range_of(&bytes[14..]);
///
/// let content = slice_of(range, bytes);
///
/// assert_eq!(content, &b"text/html"[..]);
/// ```
pub fn slice_of(range: std::ops::Range<usize>, buf: &[u8]) -> &[u8] {
    let buf_p = buf.as_ptr() as usize;
    let buf_len = buf.as_ptr() as usize;
    let sub_p = range.start;
    let sub_len = range.end.saturating_sub(range.start);

    if sub_len == 0 {
        return &[]
    }

    let Some(offset) = sub_p.checked_sub(buf_p) else {
        // assert failed: sub_p >= bytes_p
        panic!(
            "range pointer ({:p}) is smaller than `bytes` pointer ({:p})",
            sub_p as *const u8,
            buf.as_ptr(),
        );
    };
    assert!(
        sub_p + sub_len <= buf_p + buf_len,
        "subset is out of bounds: self = ({:p}, {}), subset = ({:p}, {})",
        buf.as_ptr(),
        buf_len,
        sub_p as *const u8,
        sub_len,
    );

    // SAFETY:
    // - sub_p >= buf_p
    // - sub_p + sub_len <= buf_p + buf_len
    unsafe { buf.get_unchecked(offset..offset + sub_len) }
}

// ===== Cursor =====

/// Raw bytes cursor.
///
/// Provides an API for bytes reading, with unsafe methods that skip bounds checking.
#[derive(Debug)]
pub struct Cursor<'a> {
    start: *const u8,
    cursor: *const u8,
    end: usize,
    _p: std::marker::PhantomData<&'a ()>,
}

impl<'a> Cursor<'a> {
    /// Create new [`Cursor`] from an initialized buffer.
    #[inline]
    pub fn new(buf: &'a [u8]) -> Self {
        Self {
            start: buf.as_ptr(),
            cursor: buf.as_ptr(),
            end: buf.as_ptr() as usize + buf.len(),
            _p: std::marker::PhantomData,
        }
    }

    /// Returns how many [`Cursor`] has step forward.
    #[inline]
    pub fn step(&self) -> usize {
        (self.cursor as usize) - (self.start as usize)
    }

    /// Returns the remaining bytes length.
    #[inline]
    pub fn remaining(&self) -> usize {
        self.end - self.cursor as usize
    }

    /// Returns `true` if there is more bytes left.
    #[inline]
    pub fn has_remaining(&self) -> bool {
        self.remaining() != 0
    }

    /// Returns the original bytes.
    #[inline]
    pub fn original(&self) -> &'a [u8] {
        unsafe { std::slice::from_raw_parts(self.start, self.end - self.start as usize) }
    }

    /// Returns the remaining bytes.
    #[inline]
    pub fn as_bytes(&self) -> &'a [u8] {
        unsafe { std::slice::from_raw_parts(self.cursor, self.end - self.cursor as usize) }
    }

    /// Try get the first byte.
    #[inline]
    pub fn first(&self) -> Option<u8> {
        if (self.cursor as usize) < self.end {
            // SAFETY: start is still in bounds
            Some(unsafe { *self.cursor })
        } else {
            None
        }
    }

    /// Try get the first `N`-th bytes.
    #[inline]
    pub fn first_chunk<const N: usize>(&self) -> Option<&[u8; N]> {
        if (self.cursor as usize) + N <= self.end {
            // SAFETY: start + N is still in bounds
            Some(unsafe { &*self.cursor.cast() })
        } else {
            None
        }
    }

    /// Try get the first byte, and advance the cursor by `1`.
    #[inline]
    pub fn pop_front(&mut self) -> Option<u8> {
        if (self.cursor as usize) < self.end {
            // SAFETY: start is still in bounds
            unsafe {
                let val = *self.cursor;
                self.advance(1);
                Some(val)
            }
        } else {
            None
        }
    }

    /// Try get the first `N`-th bytes, and advance the cursor by `N`.
    #[inline]
    pub fn pop_chunk_front<const N: usize>(&mut self) -> Option<&[u8; N]> {
        if (self.cursor as usize) + N <= self.end {
            // SAFETY: start + N is still in bounds
            unsafe {
                let val = &*self.cursor.cast();
                self.advance(N);
                Some(val)
            }
        } else {
            None
        }
    }

    /// Advance cursor, discarding the first `n`-th bytes.
    ///
    /// # Safety
    ///
    /// Must not advance pass slice length.
    #[inline]
    pub unsafe fn advance(&mut self, n: usize) {
        debug_assert!(
            (self.cursor as usize) + n <= self.end,
            "`Cursor::advance` safety violated, advancing `n` is out of bounds"
        );
        unsafe { self.cursor = self.cursor.add(n) };
    }

    /// Move cursor backwards cursor.
    ///
    /// # Safety
    ///
    /// Must not step back pass the first slice element.
    #[inline]
    pub unsafe fn step_back(&mut self, n: usize) {
        debug_assert!(
            (self.cursor as usize) - n >= self.start as usize,
            "`Cursor::step_back` safety violated, stepping back `n` is out of bounds"
        );
        unsafe { self.cursor = self.cursor.sub(n) };
    }
}

impl Iterator for Cursor<'_> {
    type Item = u8;

    fn next(&mut self) -> Option<Self::Item> {
        self.pop_front()
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        let len = self.remaining();
        (len, Some(len))
    }
}

impl ExactSizeIterator for Cursor<'_> {
    fn len(&self) -> usize {
        self.remaining()
    }
}

#[test]
fn test_cursor_advance() {
    const BUF: [u8; 12] = *b"Content-Type";
    const BUF_LEN: usize = BUF.len();

    let mut cursor = Cursor::new(&BUF[..]);

    assert_eq!(cursor.step(), 0);
    assert_eq!(cursor.remaining(), BUF.len());
    assert_eq!(cursor.as_bytes(), BUF);

    assert_eq!(cursor.first(), Some(b'C'));
    assert_eq!(cursor.first_chunk::<0>(), Some(b""));
    assert_eq!(cursor.first_chunk::<2>(), Some(b"Co"));
    assert_eq!(cursor.first_chunk::<13>(), None);

    // SAFETY: checked with `first_chunk::<2>`
    unsafe { cursor.advance(2) };

    const REST: [u8; 10] = *b"ntent-Type";
    const REST_LEN: usize = REST.len();

    assert_eq!(cursor.step(), 2);
    assert_eq!(cursor.remaining(), REST_LEN);
    assert_eq!(cursor.as_bytes(), REST);

    assert_eq!(cursor.first(), Some(b'n'));
    assert_eq!(cursor.first_chunk::<0>(), Some(b""));
    assert_eq!(cursor.first_chunk::<REST_LEN>(), Some(&REST));
    assert_eq!(cursor.first_chunk::<BUF_LEN>(), None);

    // SAFETY: checked with `first_chunk::<REST_LEN>`
    unsafe { cursor.advance(REST_LEN) };

    assert_eq!(cursor.step(), BUF_LEN);
    assert!(!cursor.has_remaining());
    assert!(cursor.first().is_none());
    assert!(cursor.first_chunk::<5>().is_none());
    assert_eq!(cursor.as_bytes(), b"");

    // empty buffer
    let cursor = Cursor::new(b"");
    assert!(!cursor.has_remaining());
    assert!(cursor.first().is_none());
    assert!(cursor.first_chunk::<2>().is_none());
}

#[test]
fn test_cursor_pop_front() {
    const BUF: [u8; 12] = *b"Content-Type";
    const BUF_LEN: usize = BUF.len();

    let bytes = &BUF[..];
    let mut cursor = Cursor::new(bytes);

    assert_eq!(cursor.step(), 0);
    assert_eq!(cursor.remaining(), BUF_LEN);
    assert_eq!(cursor.as_bytes(), BUF);

    assert_eq!(cursor.first(), Some(b'C'));
    assert_eq!(cursor.first_chunk::<0>(), Some(b""));
    assert_eq!(cursor.first_chunk::<2>(), Some(b"Co"));
    assert_eq!(cursor.first_chunk::<13>(), None);

    assert_eq!(cursor.pop_front(), Some(b'C'));
    assert_eq!(cursor.pop_front(), Some(b'o'));

    const REST: [u8; 10] = *b"ntent-Type";
    const REST_LEN: usize = REST.len();

    assert_eq!(cursor.step(), 2);
    assert_eq!(cursor.remaining(), REST_LEN);
    assert_eq!(cursor.as_bytes(), REST);

    assert_eq!(cursor.first(), Some(b'n'));
    assert_eq!(cursor.first_chunk::<0>(), Some(b""));
    assert_eq!(cursor.first_chunk::<REST_LEN>(), Some(&REST));
    assert_eq!(cursor.first_chunk::<BUF_LEN>(), None);

    assert_eq!(cursor.pop_chunk_front::<REST_LEN>(), Some(&REST));

    assert!(!cursor.has_remaining());
    assert!(cursor.first().is_none());
    assert!(cursor.first_chunk::<5>().is_none());
    assert_eq!(cursor.step(), BUF_LEN);
    assert_eq!(cursor.as_bytes(), b"");

    // empty buffer
    let mut cursor = Cursor::new(b"");
    assert!(!cursor.has_remaining());
    assert!(cursor.pop_front().is_none());
    assert!(cursor.pop_chunk_front::<2>().is_none());
    assert_eq!(cursor.as_bytes(), b"");
}