Trait ChunkExt

Source
pub trait ChunkExt: Chunk {
Show 14 methods // Provided methods fn peek_array<const N: usize>(&self) -> [u8; N] { ... } fn peek_array_checked<const N: usize>(&self) -> Option<[u8; N]> { ... } fn try_peek_array<const N: usize>(&self) -> Result<[u8; N], TryPeekError> { ... } fn peek_array_at<const N: usize>(&self, offset: usize) -> [u8; N] { ... } fn peek_array_at_checked<const N: usize>( &self, offset: usize, ) -> Option<[u8; N]> { ... } fn try_peek_array_at<const N: usize>( &self, offset: usize, ) -> Result<[u8; N], TryPeekAtError> { ... } fn read_array<const N: usize>(&mut self) -> [u8; N] { ... } fn read_array_checked<const N: usize>(&mut self) -> Option<[u8; N]> { ... } fn try_read_array<const N: usize>( &mut self, ) -> Result<[u8; N], TryReadError> { ... } fn peek_varint<V: Varint>( &self, ) -> Result<(NonZeroUsize, V), DecodeVarintError> { ... } fn read_varint<V: Varint>( &mut self, ) -> Result<(NonZeroUsize, V), DecodeVarintError> { ... } fn try_scan_varint(&mut self) -> Result<NonZeroUsize, DecodeVarintError> { ... } fn try_scan_varint_at( &mut self, offset: usize, ) -> Result<NonZeroUsize, DecodeVarintAtError> { ... } fn try_consume_varint(&mut self) -> Result<NonZeroUsize, DecodeVarintError> { ... }
}
Expand description

Extension trait for Chunk that provides additional methods

Provided Methods§

Source

fn peek_array<const N: usize>(&self) -> [u8; N]

Peeks a fixed-size array from the beginning of the buffer without advancing the cursor.

This method creates a copy of the first N bytes from the buffer without consuming them. The buffer position remains unchanged after this operation.

§Panics

Panics if the buffer contains fewer than N bytes. Use peek_array_checked or try_peek_array for non-panicking peeks.

§Examples
use bufkit::{Chunk, ChunkExt};

let data = [1, 2, 3, 4, 5];
let buf = &data[..];

let first_three: [u8; 3] = buf.peek_array();
assert_eq!(first_three, [1, 2, 3]);
// Chunkfer unchanged
assert_eq!(buf.remaining(), 5);
Source

fn peek_array_checked<const N: usize>(&self) -> Option<[u8; N]>

Peeks a fixed-size array from the beginning of the buffer without advancing the cursor.

This is the non-panicking version of peek_array. Returns Some(array) if sufficient data is available, otherwise returns None.

§Examples
use bufkit::{Chunk, ChunkExt};

let data = [1, 2, 3];
let buf = &data[..];

assert!(buf.peek_array_checked::<2>().is_some());
assert!(buf.peek_array_checked::<5>().is_none());
Source

fn try_peek_array<const N: usize>(&self) -> Result<[u8; N], TryPeekError>

Peeks a fixed-size array from the beginning of the buffer without advancing the cursor.

This is the non-panicking version of peek_array that returns detailed error information on failure. Returns Ok(array) on success, or Err(TryPeekError) with details about requested vs available bytes.

§Examples
use bufkit::{Chunk, ChunkExt};

let data = [1, 2, 3];
let buf = &data[..];

assert!(buf.try_peek_array::<2>().is_ok());

let err = buf.try_peek_array::<5>().unwrap_err();
// err contains details about requested vs available
Source

fn peek_array_at<const N: usize>(&self, offset: usize) -> [u8; N]

Peeks a fixed-size array from the buffer at the specified offset without advancing the cursor.

This method creates a copy of N bytes from the buffer starting at the given offset without consuming them. The buffer position remains unchanged after this operation.

§Panics

Panics if offset + N > self.remaining(). Use peek_array_at_checked or try_peek_array_at for non-panicking peeks.

§Examples
use bufkit::{Chunk, ChunkExt};

let data = [1, 2, 3, 4, 5, 6, 7, 8];
let buf = &data[..];

let array_at_2: [u8; 3] = buf.peek_array_at(2);
assert_eq!(array_at_2, [3, 4, 5]);
// Chunkfer unchanged
assert_eq!(buf.remaining(), 8);
Source

fn peek_array_at_checked<const N: usize>( &self, offset: usize, ) -> Option<[u8; N]>

Peeks a fixed-size array from the buffer at the specified offset without advancing the cursor.

This is the non-panicking version of peek_array_at. Returns Some(array) if sufficient data is available at the offset, otherwise returns None.

§Examples
use bufkit::{Chunk, ChunkExt};

let data = [1, 2, 3, 4, 5];
let buf = &data[..];

assert!(buf.peek_array_at_checked::<3>(1).is_some());
assert!(buf.peek_array_at_checked::<3>(4).is_none()); // Not enough bytes
Source

fn try_peek_array_at<const N: usize>( &self, offset: usize, ) -> Result<[u8; N], TryPeekAtError>

Peeks a fixed-size array from the buffer at the specified offset without advancing the cursor.

This is the non-panicking version of peek_array_at that returns detailed error information on failure. Returns Ok(array) on success, or Err(TryPeekAtError) with details about the error condition.

§Examples
use bufkit::{Chunk, ChunkExt};

let data = [1, 2, 3, 4, 5];
let buf = &data[..];

assert!(buf.try_peek_array_at::<3>(1).is_ok());

let err = buf.try_peek_array_at::<3>(4).unwrap_err();
let err = buf.try_peek_array_at::<3>(6).unwrap_err();
// err contains details about the error
Source

fn read_array<const N: usize>(&mut self) -> [u8; N]

Reads a fixed-size array from the buffer and advances the internal cursor.

This method creates a copy of the first N bytes from the buffer and advances the cursor by N bytes, consuming the data.

§Panics

Panics if the buffer contains fewer than N bytes. Use read_array_checked or try_read_array for non-panicking reads.

§Examples
use bufkit::{Chunk, ChunkExt};

let data = [1, 2, 3, 4, 5];
let mut buf = &data[..];

let first_three: [u8; 3] = buf.read_array();
assert_eq!(first_three, [1, 2, 3]);
assert_eq!(buf.remaining(), 2); // Cursor advanced
Source

fn read_array_checked<const N: usize>(&mut self) -> Option<[u8; N]>

Reads a fixed-size array from the buffer and advances the internal cursor.

This is the non-panicking version of read_array. Returns Some(array) and advances the cursor on success, or None if insufficient data.

§Examples
use bufkit::{Chunk, ChunkExt};

let data = [1, 2, 3];
let mut buf = &data[..];

assert!(buf.read_array_checked::<2>().is_some());
assert_eq!(buf.remaining(), 1);

assert!(buf.read_array_checked::<2>().is_none());
assert_eq!(buf.remaining(), 1); // Cursor not advanced on failure
Source

fn try_read_array<const N: usize>(&mut self) -> Result<[u8; N], TryReadError>

Reads a fixed-size array from the buffer and advances the internal cursor.

This is the non-panicking version of read_array that returns detailed error information on failure. Returns Ok(array) and advances the cursor on success, or Err(TryReadError) with details about requested vs available bytes.

§Examples
use bufkit::{Chunk, ChunkExt};

let data = [1, 2, 3];
let mut buf = &data[..];

assert!(buf.try_read_array::<2>().is_ok());
assert_eq!(buf.remaining(), 1);

let err = buf.try_read_array::<2>().unwrap_err();
// err contains details about requested vs available
Source

fn peek_varint<V: Varint>(&self) -> Result<(NonZeroUsize, V), DecodeVarintError>

Available on crate feature varint only.

Peeks a variable-length encoded type from the buffer without advancing the internal cursor.

Returns the length of the value and the value itself.

Source

fn read_varint<V: Varint>( &mut self, ) -> Result<(NonZeroUsize, V), DecodeVarintError>

Available on crate feature varint only.

Reads a variable-length encoded type from the buffer and advances the internal cursor.

Returns the length of the value read and the value itself.

Source

fn try_scan_varint(&mut self) -> Result<NonZeroUsize, DecodeVarintError>

Available on crate feature varint only.

Skips a variable-length encoded type in the buffer without advancing the internal cursor.

In varint encoding, each byte uses 7 bits for the value and the highest bit (MSB) as a continuation flag. A set MSB (1) indicates more bytes follow, while an unset MSB (0) marks the last byte of the varint.

§Examples
use bufkit::{ChunkExt, Chunk};

let buf = [0x96, 0x01]; // Varint encoding of 150
let mut chunk = &buf[..];
assert_eq!(chunk.try_scan_varint().map(|val| val.get()), Ok(2));
assert_eq!(chunk.remaining(), 2); // Cursor not advanced

let buf = [0x7F]; // Varint encoding of 127
let mut chunk = &buf[..];
assert_eq!(chunk.try_scan_varint().map(|val| val.get()), Ok(1));
assert_eq!(chunk.remaining(), 1); // Cursor not advanced
Source

fn try_scan_varint_at( &mut self, offset: usize, ) -> Result<NonZeroUsize, DecodeVarintAtError>

Available on crate feature varint only.

Skips a variable-length encoded type in the buffer at the specified offset without advancing the cursor.

In varint encoding, each byte uses 7 bits for the value and the highest bit (MSB) as a continuation flag. A set MSB (1) indicates more bytes follow, while an unset MSB (0) marks the last byte of the varint.

§Examples
use bufkit::{ChunkExt, Chunk};

let buf = [0, 0x96, 0x01]; // Varint encoding of 150
let mut chunk = &buf[..];
assert_eq!(chunk.try_scan_varint_at(1).map(|val| val.get()), Ok(2));
assert_eq!(chunk.remaining(), 3); // Cursor not advanced

let buf = [0, 0x7F]; // Varint encoding of 127
let mut chunk = &buf[..];
assert_eq!(chunk.try_scan_varint_at(1).map(|val| val.get()), Ok(1));
assert_eq!(chunk.remaining(), 2); // Cursor not advanced

// Out of bounds example
let err = chunk.try_scan_varint_at(10).unwrap_err();
Source

fn try_consume_varint(&mut self) -> Result<NonZeroUsize, DecodeVarintError>

Available on crate feature varint only.

Skips a variable-length encoded type in the buffer and advances the internal cursor.

In varint encoding, each byte uses 7 bits for the value and the highest bit (MSB) as a continuation flag. A set MSB (1) indicates more bytes follow, while an unset MSB (0) marks the last byte of the varint.

§Examples
use bufkit::{ChunkExt, Chunk};

let buf = [0x96, 0x01]; // Varint encoding of 150
let mut chunk = &buf[..];
assert_eq!(chunk.try_consume_varint().map(|val| val.get()), Ok(2));
assert_eq!(chunk.remaining(), 0); // Cursor advanced

let buf = [0x7F]; // Varint encoding of 127
let mut chunk = &buf[..];
assert_eq!(chunk.try_consume_varint().map(|val| val.get()), Ok(1));
assert_eq!(chunk.remaining(), 0); // Cursor advanced

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<T: Chunk> ChunkExt for T