Trait BufExt

Source
pub trait BufExt: Buf {
    // 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 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<(usize, V), ReadVarintError> { ... }
    fn read_varint<V: Varint>(&mut self) -> Result<(usize, V), ReadVarintError> { ... }
}
Expand description

Extension trait for Buf 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::{Buf, BufExt};

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]);
// Buffer 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::{Buf, BufExt};

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::{Buf, BufExt};

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 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::{Buf, BufExt};

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::{Buf, BufExt};

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::{Buf, BufExt};

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<(usize, V), ReadVarintError>

Available on crate feature varing 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<(usize, V), ReadVarintError>

Available on crate feature varing 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.

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: Buf> BufExt for T