Bytes

Derive Macro Bytes 

Source
#[derive(Bytes)]
Expand description

Derives the Bytes trait for single-field tuple structs.

This macro enables custom wrapper types to work with BytesVec, LZ4Vec, ZstdVec, and other vecdb vector types that require the Bytes trait.

§Requirements

  • Must be a tuple struct with exactly one field
  • The inner type must implement Bytes
  • Supports generic type parameters

§Generated Implementation

The derive generates a Bytes implementation that delegates to the inner type:

impl Bytes for Wrapper<T> where T: Bytes {
    type Array = <T as Bytes>::Array;

    fn to_bytes(&self) -> Self::Array {
        self.0.to_bytes()
    }
    fn from_bytes(bytes: &[u8]) -> Result<Self> {
        Ok(Self(<T>::from_bytes(bytes)?))
    }
}

§Example

use vecdb::{Bytes, BytesVec};

#[derive(Bytes)]
struct UserId(u64);

#[derive(Bytes)]
struct Timestamp<T>(T); // Generic types supported