Skip to main content

hyperlight_common/flatbuffer_wrappers/
codec.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2026 The Hyperlight Authors.
3
4use alloc::vec::Vec;
5
6use anyhow::Result;
7use bytes::Bytes;
8
9/// Receives external byte values while their FlatBuffer metadata is encoded.
10///
11/// Values are delivered in their logical order without flattening chunked
12/// values. The value lifetime allows a sink to retain references until the
13/// control buffer has been written, without copying payload bytes.
14pub trait ExternalValueSink<'a> {
15    /// Receive one contiguous byte value.
16    fn push_bytes(&mut self, value: &'a [u8]) -> Result<()>;
17
18    /// Receive one logical byte sequence represented as chunks.
19    fn push_chunks(&mut self, value: &'a [Bytes]) -> Result<()>;
20}
21
22/// Supplies complete external byte values while a FlatBuffer is decoded.
23///
24/// [`RecvChain`](crate::virtq::RecvChain) copies values out of shared memory.
25/// [`Segments`](crate::virtq::Segments) retains existing chunk owners.
26///
27/// Implementations must validate `length` against available input and the
28/// resource budget before allocating.
29pub trait ExternalValueSource {
30    /// Take the next external value as contiguous bytes.
31    fn take_bytes(&mut self, length: usize) -> Result<Vec<u8>>;
32
33    /// Take the next external value as owned byte chunks.
34    fn take_chunks(&mut self, length: usize) -> Result<Vec<Bytes>>;
35
36    /// Finish decoding and reject any unused external values.
37    fn finish(&mut self) -> Result<()>;
38}