1
// Copyright (C) 2018-2019, Cloudflare, Inc.
4
// Redistribution and use in source and binary forms, with or without
5
// modification, are permitted provided that the following conditions are
8
// * Redistributions of source code must retain the above copyright notice,
9
// this list of conditions and the following disclaimer.
11
// * Redistributions in binary form must reproduce the above copyright
12
// notice, this list of conditions and the following disclaimer in the
13
// documentation and/or other materials provided with the distribution.
15
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
16
// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
17
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
19
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
/// Zero-copy abstraction for parsing and constructing network packets.
31
/// A specialized [`Result`] type for [`OctetsMut`] operations.
33
/// [`Result`]: https://doc.rust-lang.org/std/result/enum.Result.html
34
/// [`OctetsMut`]: struct.OctetsMut.html
35
pub type Result<T> = std::result::Result<T, BufferTooShortError>;
37
/// An error indicating that the provided [`OctetsMut`] is not big enough.
39
/// [`OctetsMut`]: struct.OctetsMut.html
40
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
41
pub struct BufferTooShortError;
43
impl std::fmt::Display for BufferTooShortError {
44
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
45
write!(f, "BufferTooShortError")
49
impl std::error::Error for BufferTooShortError {
50
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
56
($b:expr, $ty:ty, $len:expr) => {{
58
let src = &$b.buf[$b.off..];
61
return Err(BufferTooShortError);
66
let dst = &mut out as *mut $ty as *mut u8;
67
let off = (mem::size_of::<$ty>() - len) as isize;
69
ptr::copy_nonoverlapping(src.as_ptr(), dst.offset(off), len);
72
Ok(<$ty>::from_be(out))
77
($b:expr, $ty:ty, $len:expr) => {{
78
let out = peek_u!($b, $ty, $len);
87
($b:expr, $ty:ty, $v:expr, $len:expr) => {{
90
if $b.buf.len() < $b.off + len {
91
return Err(BufferTooShortError);
96
let dst = &mut $b.buf[$b.off..($b.off + len)];
99
let src = &<$ty>::to_be(v) as *const $ty as *const u8;
100
let off = (mem::size_of::<$ty>() - len) as isize;
102
ptr::copy_nonoverlapping(src.offset(off), dst.as_mut_ptr(), len);
111
/// A zero-copy immutable byte buffer.
113
/// `Octets` wraps an in-memory buffer of bytes and provides utility functions
114
/// for manipulating it. The underlying buffer is provided by the user and is
115
/// not copied when creating an `Octets`. Operations are panic-free and will
116
/// avoid indexing the buffer past its end.
118
/// Additionally, an offset (initially set to the start of the buffer) is
119
/// incremented as bytes are read from / written to the buffer, to allow for
120
/// sequential operations.
121
#[derive(Debug, PartialEq, Eq)]
122
pub struct Octets<'a> {
128
/// Creates an `Octets` from the given slice, without copying.
130
/// Since the `Octets` is immutable, the input slice needs to be
132
pub fn with_slice(buf: &'a [u8]) -> Self {
133
Octets { buf, off: 0 }
136
/// Reads an unsigned 8-bit integer from the current offset and advances
138
pub fn get_u8(&mut self) -> Result<u8> {
142
/// Reads an unsigned 8-bit integer from the current offset without
143
/// advancing the buffer.
144
pub fn peek_u8(&mut self) -> Result<u8> {
148
/// Reads an unsigned 16-bit integer in network byte-order from the current
149
/// offset and advances the buffer.
150
pub fn get_u16(&mut self) -> Result<u16> {
154
/// Reads an unsigned 24-bit integer in network byte-order from the current
155
/// offset and advances the buffer.
156
pub fn get_u24(&mut self) -> Result<u32> {
160
/// Reads an unsigned 32-bit integer in network byte-order from the current
161
/// offset and advances the buffer.
162
pub fn get_u32(&mut self) -> Result<u32> {
166
/// Reads an unsigned 64-bit integer in network byte-order from the current
167
/// offset and advances the buffer.
168
pub fn get_u64(&mut self) -> Result<u64> {
172
/// Reads an unsigned variable-length integer in network byte-order from
173
/// the current offset and advances the buffer.
174
pub fn get_varint(&mut self) -> Result<u64> {
175
let first = self.peek_u8()?;
177
let len = varint_parse_len(first);
179
if len > self.cap() {
180
return Err(BufferTooShortError);
183
let out = match len {
184
1 => u64::from(self.get_u8()?),
186
2 => u64::from(self.get_u16()? & 0x3fff),
188
4 => u64::from(self.get_u32()? & 0x3fffffff),
190
8 => self.get_u64()? & 0x3fffffffffffffff,
198
/// Reads `len` bytes from the current offset without copying and advances
200
pub fn get_bytes(&mut self, len: usize) -> Result<Octets<'a>> {
201
if self.cap() < len {
202
return Err(BufferTooShortError);
206
buf: &self.buf[self.off..self.off + len],
215
/// Reads `len` bytes from the current offset without copying and advances
216
/// the buffer, where `len` is an unsigned 8-bit integer prefix.
217
pub fn get_bytes_with_u8_length(&mut self) -> Result<Octets<'a>> {
218
let len = self.get_u8()?;
219
self.get_bytes(len as usize)
222
/// Reads `len` bytes from the current offset without copying and advances
223
/// the buffer, where `len` is an unsigned 16-bit integer prefix in network
225
pub fn get_bytes_with_u16_length(&mut self) -> Result<Octets<'a>> {
226
let len = self.get_u16()?;
227
self.get_bytes(len as usize)
230
/// Reads `len` bytes from the current offset without copying and advances
231
/// the buffer, where `len` is an unsigned variable-length integer prefix
232
/// in network byte-order.
233
pub fn get_bytes_with_varint_length(&mut self) -> Result<Octets<'a>> {
234
let len = self.get_varint()?;
235
self.get_bytes(len as usize)
238
/// Reads `len` bytes from the current offset without copying and without
239
/// advancing the buffer.
240
pub fn peek_bytes(&self, len: usize) -> Result<Octets<'a>> {
241
if self.cap() < len {
242
return Err(BufferTooShortError);
246
buf: &self.buf[self.off..self.off + len],
253
/// Returns a slice of `len` elements from the current offset.
254
pub fn slice(&self, len: usize) -> Result<&'a [u8]> {
255
if len > self.cap() {
256
return Err(BufferTooShortError);
259
Ok(&self.buf[self.off..self.off + len])
262
/// Returns a slice of `len` elements from the end of the buffer.
263
pub fn slice_last(&self, len: usize) -> Result<&'a [u8]> {
264
if len > self.cap() {
265
return Err(BufferTooShortError);
268
let cap = self.cap();
269
Ok(&self.buf[cap - len..])
272
/// Advances the buffer's offset.
273
pub fn skip(&mut self, skip: usize) -> Result<()> {
274
if skip > self.cap() {
275
return Err(BufferTooShortError);
283
/// Returns the remaining capacity in the buffer.
284
pub fn cap(&self) -> usize {
285
self.buf.len() - self.off
288
/// Returns the total length of the buffer.
289
pub fn len(&self) -> usize {
293
/// Returns `true` if the buffer is empty.
294
pub fn is_empty(&self) -> bool {
298
/// Returns the current offset of the buffer.
299
pub fn off(&self) -> usize {
303
/// Returns a reference to the internal buffer.
304
pub fn buf(&self) -> &'a [u8] {
308
/// Copies the buffer from the current offset into a new `Vec<u8>`.
309
pub fn to_vec(&self) -> Vec<u8> {
310
self.as_ref().to_vec()
314
impl<'a> AsRef<[u8]> for Octets<'a> {
315
fn as_ref(&self) -> &[u8] {
316
&self.buf[self.off..]
320
/// A zero-copy mutable byte buffer.
322
/// Like `Octets` but mutable.
323
#[derive(Debug, PartialEq, Eq)]
324
pub struct OctetsMut<'a> {
329
impl<'a> OctetsMut<'a> {
330
/// Creates an `OctetsMut` from the given slice, without copying.
332
/// Since there's no copy, the input slice needs to be mutable to allow
334
pub fn with_slice(buf: &'a mut [u8]) -> Self {
335
OctetsMut { buf, off: 0 }
338
/// Reads an unsigned 8-bit integer from the current offset and advances
340
pub fn get_u8(&mut self) -> Result<u8> {
344
/// Reads an unsigned 8-bit integer from the current offset without
345
/// advancing the buffer.
346
pub fn peek_u8(&mut self) -> Result<u8> {
350
/// Writes an unsigned 8-bit integer at the current offset and advances
352
pub fn put_u8(&mut self, v: u8) -> Result<&mut [u8]> {
353
put_u!(self, u8, v, 1)
356
/// Reads an unsigned 16-bit integer in network byte-order from the current
357
/// offset and advances the buffer.
358
pub fn get_u16(&mut self) -> Result<u16> {
362
/// Writes an unsigned 16-bit integer in network byte-order at the current
363
/// offset and advances the buffer.
364
pub fn put_u16(&mut self, v: u16) -> Result<&mut [u8]> {
365
put_u!(self, u16, v, 2)
368
/// Reads an unsigned 24-bit integer in network byte-order from the current
369
/// offset and advances the buffer.
370
pub fn get_u24(&mut self) -> Result<u32> {
374
/// Writes an unsigned 24-bit integer in network byte-order at the current
375
/// offset and advances the buffer.
376
pub fn put_u24(&mut self, v: u32) -> Result<&mut [u8]> {
377
put_u!(self, u32, v, 3)
380
/// Reads an unsigned 32-bit integer in network byte-order from the current
381
/// offset and advances the buffer.
382
pub fn get_u32(&mut self) -> Result<u32> {
386
/// Writes an unsigned 32-bit integer in network byte-order at the current
387
/// offset and advances the buffer.
388
pub fn put_u32(&mut self, v: u32) -> Result<&mut [u8]> {
389
put_u!(self, u32, v, 4)
392
/// Reads an unsigned 64-bit integer in network byte-order from the current
393
/// offset and advances the buffer.
394
pub fn get_u64(&mut self) -> Result<u64> {
398
/// Writes an unsigned 64-bit integer in network byte-order at the current
399
/// offset and advances the buffer.
400
pub fn put_u64(&mut self, v: u64) -> Result<&mut [u8]> {
401
put_u!(self, u64, v, 8)
404
/// Reads an unsigned variable-length integer in network byte-order from
405
/// the current offset and advances the buffer.
406
pub fn get_varint(&mut self) -> Result<u64> {
407
let first = self.peek_u8()?;
409
let len = varint_parse_len(first);
411
if len > self.cap() {
412
return Err(BufferTooShortError);
415
let out = match len {
416
1 => u64::from(self.get_u8()?),
418
2 => u64::from(self.get_u16()? & 0x3fff),
420
4 => u64::from(self.get_u32()? & 0x3fffffff),
422
8 => self.get_u64()? & 0x3fffffffffffffff,
430
/// Writes an unsigned variable-length integer in network byte-order at the
431
/// current offset and advances the buffer.
432
pub fn put_varint(&mut self, v: u64) -> Result<&mut [u8]> {
433
self.put_varint_with_len(v, varint_len(v))
436
/// Writes an unsigned variable-length integer of the specified length, in
437
/// network byte-order at the current offset and advances the buffer.
438
pub fn put_varint_with_len(
439
&mut self, v: u64, len: usize,
440
) -> Result<&mut [u8]> {
441
if self.cap() < len {
442
return Err(BufferTooShortError);
445
let buf = match len {
446
1 => self.put_u8(v as u8)?,
449
let buf = self.put_u16(v as u16)?;
455
let buf = self.put_u32(v as u32)?;
461
let buf = self.put_u64(v)?;
466
_ => panic!("value is too large for varint"),
472
/// Reads `len` bytes from the current offset without copying and advances
474
pub fn get_bytes(&mut self, len: usize) -> Result<Octets> {
475
if self.cap() < len {
476
return Err(BufferTooShortError);
480
buf: &self.buf[self.off..self.off + len],
489
/// Reads `len` bytes from the current offset without copying and advances
491
pub fn get_bytes_mut(&mut self, len: usize) -> Result<OctetsMut> {
492
if self.cap() < len {
493
return Err(BufferTooShortError);
496
let out = OctetsMut {
497
buf: &mut self.buf[self.off..self.off + len],
506
/// Reads `len` bytes from the current offset without copying and advances
507
/// the buffer, where `len` is an unsigned 8-bit integer prefix.
508
pub fn get_bytes_with_u8_length(&mut self) -> Result<Octets> {
509
let len = self.get_u8()?;
510
self.get_bytes(len as usize)
513
/// Reads `len` bytes from the current offset without copying and advances
514
/// the buffer, where `len` is an unsigned 16-bit integer prefix in network
516
pub fn get_bytes_with_u16_length(&mut self) -> Result<Octets> {
517
let len = self.get_u16()?;
518
self.get_bytes(len as usize)
521
/// Reads `len` bytes from the current offset without copying and advances
522
/// the buffer, where `len` is an unsigned variable-length integer prefix
523
/// in network byte-order.
524
pub fn get_bytes_with_varint_length(&mut self) -> Result<Octets> {
525
let len = self.get_varint()?;
526
self.get_bytes(len as usize)
529
/// Reads `len` bytes from the current offset without copying and without
530
/// advancing the buffer.
531
pub fn peek_bytes(&mut self, len: usize) -> Result<Octets> {
532
if self.cap() < len {
533
return Err(BufferTooShortError);
537
buf: &self.buf[self.off..self.off + len],
544
/// Reads `len` bytes from the current offset without copying and without
545
/// advancing the buffer.
546
pub fn peek_bytes_mut(&mut self, len: usize) -> Result<OctetsMut> {
547
if self.cap() < len {
548
return Err(BufferTooShortError);
551
let out = OctetsMut {
552
buf: &mut self.buf[self.off..self.off + len],
559
/// Writes `len` bytes from the current offset without copying and advances
561
pub fn put_bytes(&mut self, v: &[u8]) -> Result<()> {
564
if self.cap() < len {
565
return Err(BufferTooShortError);
572
self.as_mut()[..len].copy_from_slice(v);
579
/// Splits the buffer in two at the given absolute offset.
580
pub fn split_at(&mut self, off: usize) -> Result<(OctetsMut, OctetsMut)> {
581
if self.len() < off {
582
return Err(BufferTooShortError);
585
let (left, right) = self.buf.split_at_mut(off);
587
let first = OctetsMut { buf: left, off: 0 };
589
let last = OctetsMut { buf: right, off: 0 };
594
/// Returns a slice of `len` elements from the current offset.
595
pub fn slice(&'a mut self, len: usize) -> Result<&'a mut [u8]> {
596
if len > self.cap() {
597
return Err(BufferTooShortError);
600
Ok(&mut self.buf[self.off..self.off + len])
603
/// Returns a slice of `len` elements from the end of the buffer.
604
pub fn slice_last(&'a mut self, len: usize) -> Result<&'a mut [u8]> {
605
if len > self.cap() {
606
return Err(BufferTooShortError);
609
let cap = self.cap();
610
Ok(&mut self.buf[cap - len..])
613
/// Advances the buffer's offset.
614
pub fn skip(&mut self, skip: usize) -> Result<()> {
615
if skip > self.cap() {
616
return Err(BufferTooShortError);
624
/// Returns the remaining capacity in the buffer.
625
pub fn cap(&self) -> usize {
626
self.buf.len() - self.off
629
/// Returns the total length of the buffer.
630
pub fn len(&self) -> usize {
634
/// Returns `true` if the buffer is empty.
635
pub fn is_empty(&self) -> bool {
639
/// Returns the current offset of the buffer.
640
pub fn off(&self) -> usize {
644
/// Returns a reference to the internal buffer.
645
pub fn buf(&self) -> &[u8] {
649
/// Copies the buffer from the current offset into a new `Vec<u8>`.
650
pub fn to_vec(&self) -> Vec<u8> {
651
self.as_ref().to_vec()
655
impl<'a> AsRef<[u8]> for OctetsMut<'a> {
656
fn as_ref(&self) -> &[u8] {
657
&self.buf[self.off..]
661
impl<'a> AsMut<[u8]> for OctetsMut<'a> {
662
fn as_mut(&mut self) -> &mut [u8] {
663
&mut self.buf[self.off..]
667
/// Returns how many bytes it would take to encode `v` as a variable-length
669
pub const fn varint_len(v: u64) -> usize {
672
} else if v <= 16383 {
674
} else if v <= 1_073_741_823 {
676
} else if v <= 4_611_686_018_427_387_903 {
683
/// Returns how long the variable-length integer is, given its first byte.
684
pub const fn varint_parse_len(first: u8) -> usize {
701
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
704
let mut b = Octets::with_slice(&d);
705
assert_eq!(b.cap(), 18);
706
assert_eq!(b.off(), 0);
708
assert_eq!(b.get_u8().unwrap(), 1);
709
assert_eq!(b.cap(), 17);
710
assert_eq!(b.off(), 1);
712
assert_eq!(b.get_u16().unwrap(), 0x203);
713
assert_eq!(b.cap(), 15);
714
assert_eq!(b.off(), 3);
716
assert_eq!(b.get_u24().unwrap(), 0x40506);
717
assert_eq!(b.cap(), 12);
718
assert_eq!(b.off(), 6);
720
assert_eq!(b.get_u32().unwrap(), 0x0708090a);
721
assert_eq!(b.cap(), 8);
722
assert_eq!(b.off(), 10);
724
assert_eq!(b.get_u64().unwrap(), 0x0b0c0d0e0f101112);
725
assert_eq!(b.cap(), 0);
726
assert_eq!(b.off(), 18);
728
assert!(b.get_u8().is_err());
729
assert!(b.get_u16().is_err());
730
assert!(b.get_u24().is_err());
731
assert!(b.get_u32().is_err());
732
assert!(b.get_u64().is_err());
738
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
741
let mut b = OctetsMut::with_slice(&mut d);
742
assert_eq!(b.cap(), 18);
743
assert_eq!(b.off(), 0);
745
assert_eq!(b.get_u8().unwrap(), 1);
746
assert_eq!(b.cap(), 17);
747
assert_eq!(b.off(), 1);
749
assert_eq!(b.get_u16().unwrap(), 0x203);
750
assert_eq!(b.cap(), 15);
751
assert_eq!(b.off(), 3);
753
assert_eq!(b.get_u24().unwrap(), 0x40506);
754
assert_eq!(b.cap(), 12);
755
assert_eq!(b.off(), 6);
757
assert_eq!(b.get_u32().unwrap(), 0x0708090a);
758
assert_eq!(b.cap(), 8);
759
assert_eq!(b.off(), 10);
761
assert_eq!(b.get_u64().unwrap(), 0x0b0c0d0e0f101112);
762
assert_eq!(b.cap(), 0);
763
assert_eq!(b.off(), 18);
765
assert!(b.get_u8().is_err());
766
assert!(b.get_u16().is_err());
767
assert!(b.get_u24().is_err());
768
assert!(b.get_u32().is_err());
769
assert!(b.get_u64().is_err());
776
let mut b = Octets::with_slice(&d);
777
assert_eq!(b.cap(), 2);
778
assert_eq!(b.off(), 0);
780
assert_eq!(b.peek_u8().unwrap(), 1);
781
assert_eq!(b.cap(), 2);
782
assert_eq!(b.off(), 0);
784
assert_eq!(b.peek_u8().unwrap(), 1);
785
assert_eq!(b.cap(), 2);
786
assert_eq!(b.off(), 0);
788
b.get_u16().unwrap();
790
assert!(b.peek_u8().is_err());
797
let mut b = OctetsMut::with_slice(&mut d);
798
assert_eq!(b.cap(), 2);
799
assert_eq!(b.off(), 0);
801
assert_eq!(b.peek_u8().unwrap(), 1);
802
assert_eq!(b.cap(), 2);
803
assert_eq!(b.off(), 0);
805
assert_eq!(b.peek_u8().unwrap(), 1);
806
assert_eq!(b.cap(), 2);
807
assert_eq!(b.off(), 0);
809
b.get_u16().unwrap();
811
assert!(b.peek_u8().is_err());
816
let d = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
817
let mut b = Octets::with_slice(&d);
818
assert_eq!(b.cap(), 10);
819
assert_eq!(b.off(), 0);
821
assert_eq!(b.get_bytes(5).unwrap().as_ref(), [1, 2, 3, 4, 5]);
822
assert_eq!(b.cap(), 5);
823
assert_eq!(b.off(), 5);
825
assert_eq!(b.get_bytes(3).unwrap().as_ref(), [6, 7, 8]);
826
assert_eq!(b.cap(), 2);
827
assert_eq!(b.off(), 8);
829
assert!(b.get_bytes(3).is_err());
830
assert_eq!(b.cap(), 2);
831
assert_eq!(b.off(), 8);
833
assert_eq!(b.get_bytes(2).unwrap().as_ref(), [9, 10]);
834
assert_eq!(b.cap(), 0);
835
assert_eq!(b.off(), 10);
837
assert!(b.get_bytes(2).is_err());
842
let mut d = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
843
let mut b = OctetsMut::with_slice(&mut d);
844
assert_eq!(b.cap(), 10);
845
assert_eq!(b.off(), 0);
847
assert_eq!(b.get_bytes(5).unwrap().as_ref(), [1, 2, 3, 4, 5]);
848
assert_eq!(b.cap(), 5);
849
assert_eq!(b.off(), 5);
851
assert_eq!(b.get_bytes(3).unwrap().as_ref(), [6, 7, 8]);
852
assert_eq!(b.cap(), 2);
853
assert_eq!(b.off(), 8);
855
assert!(b.get_bytes(3).is_err());
856
assert_eq!(b.cap(), 2);
857
assert_eq!(b.off(), 8);
859
assert_eq!(b.get_bytes(2).unwrap().as_ref(), [9, 10]);
860
assert_eq!(b.cap(), 0);
861
assert_eq!(b.off(), 10);
863
assert!(b.get_bytes(2).is_err());
868
let d = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
869
let mut b = Octets::with_slice(&d);
870
assert_eq!(b.cap(), 10);
871
assert_eq!(b.off(), 0);
873
assert_eq!(b.peek_bytes(5).unwrap().as_ref(), [1, 2, 3, 4, 5]);
874
assert_eq!(b.cap(), 10);
875
assert_eq!(b.off(), 0);
877
assert_eq!(b.peek_bytes(5).unwrap().as_ref(), [1, 2, 3, 4, 5]);
878
assert_eq!(b.cap(), 10);
879
assert_eq!(b.off(), 0);
881
b.get_bytes(5).unwrap();
885
fn peek_bytes_mut() {
886
let mut d = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
887
let mut b = OctetsMut::with_slice(&mut d);
888
assert_eq!(b.cap(), 10);
889
assert_eq!(b.off(), 0);
891
assert_eq!(b.peek_bytes(5).unwrap().as_ref(), [1, 2, 3, 4, 5]);
892
assert_eq!(b.cap(), 10);
893
assert_eq!(b.off(), 0);
895
assert_eq!(b.peek_bytes(5).unwrap().as_ref(), [1, 2, 3, 4, 5]);
896
assert_eq!(b.cap(), 10);
897
assert_eq!(b.off(), 0);
899
b.get_bytes(5).unwrap();
904
let d = [0xc2, 0x19, 0x7c, 0x5e, 0xff, 0x14, 0xe8, 0x8c];
905
let mut b = Octets::with_slice(&d);
906
assert_eq!(b.get_varint().unwrap(), 151288809941952652);
907
assert_eq!(b.cap(), 0);
908
assert_eq!(b.off(), 8);
910
let d = [0x9d, 0x7f, 0x3e, 0x7d];
911
let mut b = Octets::with_slice(&d);
912
assert_eq!(b.get_varint().unwrap(), 494878333);
913
assert_eq!(b.cap(), 0);
914
assert_eq!(b.off(), 4);
916
let d = [0x7b, 0xbd];
917
let mut b = Octets::with_slice(&d);
918
assert_eq!(b.get_varint().unwrap(), 15293);
919
assert_eq!(b.cap(), 0);
920
assert_eq!(b.off(), 2);
922
let d = [0x40, 0x25];
923
let mut b = Octets::with_slice(&d);
924
assert_eq!(b.get_varint().unwrap(), 37);
925
assert_eq!(b.cap(), 0);
926
assert_eq!(b.off(), 2);
929
let mut b = Octets::with_slice(&d);
930
assert_eq!(b.get_varint().unwrap(), 37);
931
assert_eq!(b.cap(), 0);
932
assert_eq!(b.off(), 1);
936
fn get_varint_mut() {
937
let mut d = [0xc2, 0x19, 0x7c, 0x5e, 0xff, 0x14, 0xe8, 0x8c];
938
let mut b = OctetsMut::with_slice(&mut d);
939
assert_eq!(b.get_varint().unwrap(), 151288809941952652);
940
assert_eq!(b.cap(), 0);
941
assert_eq!(b.off(), 8);
943
let mut d = [0x9d, 0x7f, 0x3e, 0x7d];
944
let mut b = OctetsMut::with_slice(&mut d);
945
assert_eq!(b.get_varint().unwrap(), 494878333);
946
assert_eq!(b.cap(), 0);
947
assert_eq!(b.off(), 4);
949
let mut d = [0x7b, 0xbd];
950
let mut b = OctetsMut::with_slice(&mut d);
951
assert_eq!(b.get_varint().unwrap(), 15293);
952
assert_eq!(b.cap(), 0);
953
assert_eq!(b.off(), 2);
955
let mut d = [0x40, 0x25];
956
let mut b = OctetsMut::with_slice(&mut d);
957
assert_eq!(b.get_varint().unwrap(), 37);
958
assert_eq!(b.cap(), 0);
959
assert_eq!(b.off(), 2);
962
let mut b = OctetsMut::with_slice(&mut d);
963
assert_eq!(b.get_varint().unwrap(), 37);
964
assert_eq!(b.cap(), 0);
965
assert_eq!(b.off(), 1);
972
let mut b = OctetsMut::with_slice(&mut d);
973
assert!(b.put_varint(151288809941952652).is_ok());
974
assert_eq!(b.cap(), 0);
975
assert_eq!(b.off(), 8);
977
let exp = [0xc2, 0x19, 0x7c, 0x5e, 0xff, 0x14, 0xe8, 0x8c];
978
assert_eq!(&d, &exp);
982
let mut b = OctetsMut::with_slice(&mut d);
983
assert!(b.put_varint(494878333).is_ok());
984
assert_eq!(b.cap(), 0);
985
assert_eq!(b.off(), 4);
987
let exp = [0x9d, 0x7f, 0x3e, 0x7d];
988
assert_eq!(&d, &exp);
992
let mut b = OctetsMut::with_slice(&mut d);
993
assert!(b.put_varint(15293).is_ok());
994
assert_eq!(b.cap(), 0);
995
assert_eq!(b.off(), 2);
997
let exp = [0x7b, 0xbd];
998
assert_eq!(&d, &exp);
1002
let mut b = OctetsMut::with_slice(&mut d);
1003
assert!(b.put_varint(37).is_ok());
1004
assert_eq!(b.cap(), 0);
1005
assert_eq!(b.off(), 1);
1008
assert_eq!(&d, &exp);
1012
let mut b = OctetsMut::with_slice(&mut d);
1013
assert!(b.put_varint(151288809941952652).is_err());
1014
assert_eq!(b.cap(), 3);
1015
assert_eq!(b.off(), 0);
1018
assert_eq!(&d, &exp);
1023
fn varint_too_large() {
1025
let mut b = OctetsMut::with_slice(&mut d);
1026
assert!(b.put_varint(u64::MAX).is_err());
1031
let mut d = [0; 18];
1034
let mut b = OctetsMut::with_slice(&mut d);
1035
assert_eq!(b.cap(), 18);
1036
assert_eq!(b.off(), 0);
1038
assert!(b.put_u8(1).is_ok());
1039
assert_eq!(b.cap(), 17);
1040
assert_eq!(b.off(), 1);
1042
assert!(b.put_u16(0x203).is_ok());
1043
assert_eq!(b.cap(), 15);
1044
assert_eq!(b.off(), 3);
1046
assert!(b.put_u24(0x40506).is_ok());
1047
assert_eq!(b.cap(), 12);
1048
assert_eq!(b.off(), 6);
1050
assert!(b.put_u32(0x0708090a).is_ok());
1051
assert_eq!(b.cap(), 8);
1052
assert_eq!(b.off(), 10);
1054
assert!(b.put_u64(0x0b0c0d0e0f101112).is_ok());
1055
assert_eq!(b.cap(), 0);
1056
assert_eq!(b.off(), 18);
1058
assert!(b.put_u8(1).is_err());
1062
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
1064
assert_eq!(&d, &exp);
1072
let mut b = OctetsMut::with_slice(&mut d);
1073
assert_eq!(b.cap(), 5);
1074
assert_eq!(b.off(), 0);
1076
let p = [0x0a, 0x0b, 0x0c, 0x0d, 0x0e];
1077
assert!(b.put_bytes(&p).is_ok());
1078
assert_eq!(b.cap(), 0);
1079
assert_eq!(b.off(), 5);
1081
assert!(b.put_u8(1).is_err());
1084
let exp = [0xa, 0xb, 0xc, 0xd, 0xe];
1085
assert_eq!(&d, &exp);
1090
let mut d = b"helloworld".to_vec();
1092
let mut b = OctetsMut::with_slice(&mut d);
1093
assert_eq!(b.cap(), 10);
1094
assert_eq!(b.off(), 0);
1095
assert_eq!(b.as_ref(), b"helloworld");
1097
assert!(b.get_bytes(5).is_ok());
1098
assert_eq!(b.cap(), 5);
1099
assert_eq!(b.off(), 5);
1100
assert_eq!(b.as_ref(), b"world");
1104
let (first, last) = b.split_at(off).unwrap();
1105
assert_eq!(first.cap(), 5);
1106
assert_eq!(first.off(), 0);
1107
assert_eq!(first.as_ref(), b"hello");
1109
assert_eq!(last.cap(), 5);
1110
assert_eq!(last.off(), 0);
1111
assert_eq!(last.as_ref(), b"world");
1116
let mut d = b"helloworld".to_vec();
1119
let mut b = OctetsMut::with_slice(&mut d);
1120
let (first, second) = b.split_at(5).unwrap();
1122
let mut exp1 = b"hello".to_vec();
1123
assert_eq!(first.as_ref(), &mut exp1[..]);
1125
let mut exp2 = b"world".to_vec();
1126
assert_eq!(second.as_ref(), &mut exp2[..]);
1130
let mut b = OctetsMut::with_slice(&mut d);
1131
let (first, second) = b.split_at(10).unwrap();
1133
let mut exp1 = b"helloworld".to_vec();
1134
assert_eq!(first.as_ref(), &mut exp1[..]);
1136
let mut exp2 = b"".to_vec();
1137
assert_eq!(second.as_ref(), &mut exp2[..]);
1141
let mut b = OctetsMut::with_slice(&mut d);
1142
let (first, second) = b.split_at(9).unwrap();
1144
let mut exp1 = b"helloworl".to_vec();
1145
assert_eq!(first.as_ref(), &mut exp1[..]);
1147
let mut exp2 = b"d".to_vec();
1148
assert_eq!(second.as_ref(), &mut exp2[..]);
1152
let mut b = OctetsMut::with_slice(&mut d);
1153
assert!(b.split_at(11).is_err());
1159
let d = b"helloworld".to_vec();
1162
let b = Octets::with_slice(&d);
1163
let exp = b"hello".to_vec();
1164
assert_eq!(b.slice(5), Ok(&exp[..]));
1168
let b = Octets::with_slice(&d);
1169
let exp = b"".to_vec();
1170
assert_eq!(b.slice(0), Ok(&exp[..]));
1174
let mut b = Octets::with_slice(&d);
1175
b.get_bytes(5).unwrap();
1177
let exp = b"world".to_vec();
1178
assert_eq!(b.slice(5), Ok(&exp[..]));
1182
let b = Octets::with_slice(&d);
1183
assert!(b.slice(11).is_err());
1189
let mut d = b"helloworld".to_vec();
1192
let mut b = OctetsMut::with_slice(&mut d);
1193
let mut exp = b"hello".to_vec();
1194
assert_eq!(b.slice(5), Ok(&mut exp[..]));
1198
let mut b = OctetsMut::with_slice(&mut d);
1199
let mut exp = b"".to_vec();
1200
assert_eq!(b.slice(0), Ok(&mut exp[..]));
1204
let mut b = OctetsMut::with_slice(&mut d);
1205
b.get_bytes(5).unwrap();
1207
let mut exp = b"world".to_vec();
1208
assert_eq!(b.slice(5), Ok(&mut exp[..]));
1212
let mut b = OctetsMut::with_slice(&mut d);
1213
assert!(b.slice(11).is_err());
1219
let d = b"helloworld".to_vec();
1222
let b = Octets::with_slice(&d);
1223
let exp = b"orld".to_vec();
1224
assert_eq!(b.slice_last(4), Ok(&exp[..]));
1228
let b = Octets::with_slice(&d);
1229
let exp = b"d".to_vec();
1230
assert_eq!(b.slice_last(1), Ok(&exp[..]));
1234
let b = Octets::with_slice(&d);
1235
let exp = b"".to_vec();
1236
assert_eq!(b.slice_last(0), Ok(&exp[..]));
1240
let b = Octets::with_slice(&d);
1241
let exp = b"helloworld".to_vec();
1242
assert_eq!(b.slice_last(10), Ok(&exp[..]));
1246
let b = Octets::with_slice(&d);
1247
assert!(b.slice_last(11).is_err());
1252
fn slice_last_mut() {
1253
let mut d = b"helloworld".to_vec();
1256
let mut b = OctetsMut::with_slice(&mut d);
1257
let mut exp = b"orld".to_vec();
1258
assert_eq!(b.slice_last(4), Ok(&mut exp[..]));
1262
let mut b = OctetsMut::with_slice(&mut d);
1263
let mut exp = b"d".to_vec();
1264
assert_eq!(b.slice_last(1), Ok(&mut exp[..]));
1268
let mut b = OctetsMut::with_slice(&mut d);
1269
let mut exp = b"".to_vec();
1270
assert_eq!(b.slice_last(0), Ok(&mut exp[..]));
1274
let mut b = OctetsMut::with_slice(&mut d);
1275
let mut exp = b"helloworld".to_vec();
1276
assert_eq!(b.slice_last(10), Ok(&mut exp[..]));
1280
let mut b = OctetsMut::with_slice(&mut d);
1281
assert!(b.slice_last(11).is_err());