Skip to content

Allow as_gatt() to support owned or caller-provided serialization buffers #375

@sssilver

Description

@sssilver

Currently, the AsGatt trait defines:

fn as_gatt(&self) -> &[u8];

This intends for implementors to return a reference to serialized data. However, it creates challenges:

  • It requires the serialized form to be pre-stored inside the struct
  • It prevents on-demand serialization without unsafe code
  • It makes it harder to implement serialization for dynamic or large payloads efficiently

Because the function must return a reference to a byte array, we can't safely serialize into a temporary buffer inside as_gatt() without storing that buffer somewhere in the struct itself, increasing memory usage even when serialization is infrequent.

This also complicates patterns like serialization depending on runtime field values, serialization into external/caller-provided memory to save RAM, or avoiding stale serialized buffers when fields change.

Would it be possible to provide an alternative trait or extend AsGatt with a variant that returns an owned serialization result? E.g.:

fn to_gatt(&self) -> heapless::Vec<u8, N>;

or something like:

fn to_gatt(&self, buffer: &mut [u8]) -> usize;

This would allow on-demand serialization without extra storage inside the object itself.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions