courierust 1.0.0

no_std HTTP and gRPC engine with work-stealing, RFC 9218, and JA3/JA4 fingerprinting.
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
//! Cheap immutable byte buffer and a growable write buffer.
//!
//! [`Bytes`] is an immutable, `Arc`-backed slice with O(1) slicing and
//! cheap clone — the currency of frame payloads and header values.
//! [`BytesMut`] is the growable builder used by encoders.

use alloc::boxed::Box;
use alloc::string::String;
use alloc::sync::Arc;
use alloc::vec::Vec;
use core::fmt;
use core::ops::{Deref, Range};

/// Immutable byte buffer with O(1) slicing and cheap clone.
#[derive(Clone, Default)]
pub struct Bytes {
    buf: Arc<[u8]>,
    start: usize,
    len: usize,
}

impl Bytes {
    /// Empty buffer.
    #[inline]
    pub fn new() -> Self {
        Self {
            buf: Arc::from(&[][..]),
            start: 0,
            len: 0,
        }
    }

    /// Buffer from a static slice (no allocation).
    #[inline]
    pub fn from_static(s: &'static [u8]) -> Self {
        Self {
            buf: Arc::from(s),
            start: 0,
            len: s.len(),
        }
    }

    /// Number of bytes.
    #[inline]
    pub fn len(&self) -> usize {
        self.len
    }

    /// Whether the buffer is empty.
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.len == 0
    }

    /// O(1) sub-slice.
    #[inline]
    pub fn slice(&self, range: Range<usize>) -> Self {
        assert!(range.start <= range.end && range.end <= self.len);
        Self {
            buf: self.buf.clone(),
            start: self.start + range.start,
            len: range.end - range.start,
        }
    }

    /// O(1) suffix.
    #[inline]
    pub fn slice_from(&self, from: usize) -> Self {
        assert!(from <= self.len);
        Self {
            buf: self.buf.clone(),
            start: self.start + from,
            len: self.len - from,
        }
    }

    /// O(1) prefix.
    #[inline]
    pub fn slice_to(&self, to: usize) -> Self {
        assert!(to <= self.len);
        Self {
            buf: self.buf.clone(),
            start: self.start,
            len: to,
        }
    }

    /// Split off the first `at` bytes, returning them and keeping the rest.
    #[inline]
    pub fn split_to(&mut self, at: usize) -> Self {
        assert!(at <= self.len);
        let out = Self {
            buf: self.buf.clone(),
            start: self.start,
            len: at,
        };
        self.start += at;
        self.len -= at;
        out
    }

    /// Split off the tail starting at `at`, returning the tail.
    #[inline]
    pub fn split_off(&mut self, at: usize) -> Self {
        assert!(at <= self.len);
        let out = Self {
            buf: self.buf.clone(),
            start: self.start + at,
            len: self.len - at,
        };
        self.len = at;
        out
    }

    /// The underlying contiguous slice.
    #[inline]
    pub fn as_slice(&self) -> &[u8] {
        &self.buf[self.start..self.start + self.len]
    }

    /// Copy into a fresh `Vec`.
    #[inline]
    pub fn to_vec(&self) -> Vec<u8> {
        self.as_slice().to_vec()
    }

    /// Copy into a fresh owned buffer.
    #[inline]
    pub fn into_vec(self) -> Vec<u8> {
        self.to_vec()
    }

    /// View as `str` if valid UTF-8.
    #[inline]
    pub fn to_str(&self) -> Result<&str, core::str::Utf8Error> {
        core::str::from_utf8(self.as_slice())
    }
}

impl Deref for Bytes {
    type Target = [u8];

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

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

impl From<Vec<u8>> for Bytes {
    #[inline]
    fn from(v: Vec<u8>) -> Self {
        let len = v.len();
        Self {
            buf: Arc::from(v.into_boxed_slice()),
            start: 0,
            len,
        }
    }
}

impl From<Box<[u8]>> for Bytes {
    #[inline]
    fn from(b: Box<[u8]>) -> Self {
        let len = b.len();
        Self {
            buf: Arc::from(b),
            start: 0,
            len,
        }
    }
}

impl From<&[u8]> for Bytes {
    #[inline]
    fn from(s: &[u8]) -> Self {
        Self::from(s.to_vec())
    }
}

impl From<&str> for Bytes {
    #[inline]
    fn from(s: &str) -> Self {
        Self::from(s.as_bytes())
    }
}

impl From<String> for Bytes {
    #[inline]
    fn from(s: String) -> Self {
        Self::from(s.into_bytes())
    }
}

impl PartialEq for Bytes {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        self.as_slice() == other.as_slice()
    }
}

impl Eq for Bytes {}

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

impl PartialEq<str> for Bytes {
    #[inline]
    fn eq(&self, other: &str) -> bool {
        self.as_slice() == other.as_bytes()
    }
}

impl PartialOrd for Bytes {
    #[inline]
    fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for Bytes {
    #[inline]
    fn cmp(&self, other: &Self) -> core::cmp::Ordering {
        self.as_slice().cmp(other.as_slice())
    }
}

impl core::hash::Hash for Bytes {
    #[inline]
    fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
        self.as_slice().hash(state)
    }
}

impl fmt::Debug for Bytes {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Bytes(len={})", self.len)
    }
}

impl fmt::Display for Bytes {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match core::str::from_utf8(self.as_slice()) {
            Ok(s) => f.write_str(s),
            Err(_) => write!(f, "<invalid utf8, {} bytes>", self.len),
        }
    }
}

/// Growable byte builder, the counterpart of [`Bytes`].
#[derive(Clone, Default)]
pub struct BytesMut {
    buf: Vec<u8>,
}

impl BytesMut {
    /// Empty buffer.
    #[inline]
    pub fn new() -> Self {
        Self { buf: Vec::new() }
    }

    /// Empty buffer with capacity.
    #[inline]
    pub fn with_capacity(cap: usize) -> Self {
        Self {
            buf: Vec::with_capacity(cap),
        }
    }

    /// From an existing `Vec` (takes ownership).
    #[inline]
    pub fn from_vec(v: Vec<u8>) -> Self {
        Self { buf: v }
    }

    /// Number of bytes.
    #[inline]
    pub fn len(&self) -> usize {
        self.buf.len()
    }

    /// Whether empty.
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.buf.is_empty()
    }

    /// Contiguous view.
    #[inline]
    pub fn as_slice(&self) -> &[u8] {
        &self.buf
    }

    /// Mutable contiguous view.
    #[inline]
    pub fn as_mut_slice(&mut self) -> &mut [u8] {
        &mut self.buf
    }

    /// Clear without releasing capacity.
    #[inline]
    pub fn clear(&mut self) {
        self.buf.clear();
    }

    /// Reserve capacity.
    #[inline]
    pub fn reserve(&mut self, extra: usize) {
        self.buf.reserve(extra);
    }

    /// Append a slice.
    #[inline]
    pub fn extend_from_slice(&mut self, s: &[u8]) {
        self.buf.extend_from_slice(s);
    }

    /// Append an iterator of slices (avoids re-borrowing).
    #[inline]
    pub fn extend_from_slices<'a>(&mut self, slices: impl IntoIterator<Item = &'a [u8]>) {
        for s in slices {
            self.buf.extend_from_slice(s);
        }
    }

    /// Append a single byte.
    #[inline]
    pub fn put_u8(&mut self, b: u8) {
        self.buf.push(b);
    }

    /// Append a big-endian u16.
    #[inline]
    pub fn put_u16(&mut self, v: u16) {
        self.buf.extend_from_slice(&v.to_be_bytes());
    }

    /// Append a big-endian 24-bit value.
    #[inline]
    pub fn put_u24(&mut self, v: u32) {
        debug_assert!(v <= 0xFF_FFFF);
        self.buf.push((v >> 16) as u8);
        self.buf.push((v >> 8) as u8);
        self.buf.push(v as u8);
    }

    /// Append a big-endian u32.
    #[inline]
    pub fn put_u32(&mut self, v: u32) {
        self.buf.extend_from_slice(&v.to_be_bytes());
    }

    /// Append a big-endian u64.
    #[inline]
    pub fn put_u64(&mut self, v: u64) {
        self.buf.extend_from_slice(&v.to_be_bytes());
    }

    /// Append another buffer.
    #[inline]
    pub fn put_bytes(&mut self, b: &Bytes) {
        self.buf.extend_from_slice(b.as_slice());
    }

    /// Turn into an immutable [`Bytes`] (copies; callers needing O(1)
    /// should keep a [`BytesMut`] and freeze rarely).
    #[inline]
    pub fn freeze(self) -> Bytes {
        Bytes::from(self.buf)
    }

    /// Truncate to `len`.
    #[inline]
    pub fn truncate(&mut self, len: usize) {
        self.buf.truncate(len);
    }

    /// Resize to `new_len`, filling new bytes with `value`.
    #[inline]
    pub fn resize(&mut self, new_len: usize, value: u8) {
        self.buf.resize(new_len, value);
    }

    /// Split off the tail starting at `at`.
    #[inline]
    pub fn split_off(&mut self, at: usize) -> BytesMut {
        BytesMut {
            buf: self.buf.split_off(at),
        }
    }

    /// Split off the first `at` bytes, keeping the rest.
    #[inline]
    pub fn split_to(&mut self, at: usize) -> BytesMut {
        assert!(at <= self.buf.len());
        let head = self.buf[..at].to_vec();
        self.buf.drain(..at);
        BytesMut { buf: head }
    }

    /// Steal the inner `Vec`.
    #[inline]
    pub fn into_vec(self) -> Vec<u8> {
        self.buf
    }
}

impl Deref for BytesMut {
    type Target = [u8];

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

impl core::ops::DerefMut for BytesMut {
    #[inline]
    fn deref_mut(&mut self) -> &mut [u8] {
        &mut self.buf
    }
}

impl AsRef<[u8]> for BytesMut {
    #[inline]
    fn as_ref(&self) -> &[u8] {
        &self.buf
    }
}

impl fmt::Debug for BytesMut {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "BytesMut(len={})", self.buf.len())
    }
}