Skip to main content

read_varbinary

Function read_varbinary 

Source
pub fn read_varbinary(buf: &[u8]) -> Result<&[u8], CopyReadError>
Expand description

Reads a variable-length binary value from HyperBinary format.

Returns the data slice (without the length prefix).

§Errors

§Example

use hyperdb_api_core::protocol::copy::{read_varbinary, CopyReadError};

// "hello" with 4-byte length prefix
let buf = [0x05, 0x00, 0x00, 0x00, b'h', b'e', b'l', b'l', b'o'];
assert_eq!(read_varbinary(&buf).unwrap(), b"hello");

// Too short for length field
let short_buf = [0x05, 0x00, 0x00];
assert!(matches!(read_varbinary(&short_buf), Err(CopyReadError::BufferTooShort { .. })));

// Declared length exceeds buffer
let bad_len = [0x10, 0x00, 0x00, 0x00, b'h', b'i']; // declares 16 bytes, has 2
assert!(matches!(read_varbinary(&bad_len), Err(CopyReadError::LengthExceedsBuffer { .. })));