Skip to main content

hyperlight_common/flatbuffer_wrappers/
host_function_details.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2025 The Hyperlight Authors.
3
4use alloc::vec::Vec;
5
6use anyhow::{Error, Result};
7use flatbuffers::{WIPOffset, size_prefixed_root};
8#[cfg(feature = "tracing")]
9use tracing::{Span, instrument};
10
11use super::host_function_definition::HostFunctionDefinition;
12use crate::flatbuffers::hyperlight::generated::{
13    HostFunctionDefinition as FbHostFunctionDefinition,
14    HostFunctionDetails as FbHostFunctionDetails,
15    HostFunctionDetailsArgs as FbHostFunctionDetailsArgs,
16};
17
18/// `HostFunctionDetails` represents the set of functions that the host exposes to the guest.
19#[derive(Debug, Default, Clone)]
20pub struct HostFunctionDetails {
21    /// The host functions.
22    pub host_functions: Option<Vec<HostFunctionDefinition>>,
23}
24
25impl TryFrom<&[u8]> for HostFunctionDetails {
26    type Error = Error;
27    #[cfg_attr(feature = "tracing", instrument(err(Debug), skip_all, parent = Span::current(), level= "Trace"))]
28    fn try_from(value: &[u8]) -> Result<Self> {
29        let host_function_details_fb = size_prefixed_root::<FbHostFunctionDetails>(value)
30            .map_err(|e| anyhow::anyhow!("Error while reading HostFunctionDetails: {:?}", e))?;
31
32        let host_function_definitions = match host_function_details_fb.functions() {
33            Some(hfd) => {
34                let len = hfd.len();
35                let mut vec_hfd: Vec<HostFunctionDefinition> = Vec::with_capacity(len);
36                for i in 0..len {
37                    let fb_host_function_definition = hfd.get(i);
38                    let hfdef = HostFunctionDefinition::try_from(&fb_host_function_definition)?;
39                    vec_hfd.push(hfdef);
40                }
41
42                Some(vec_hfd)
43            }
44
45            None => None,
46        };
47
48        Ok(Self {
49            host_functions: host_function_definitions,
50        })
51    }
52}
53
54impl TryFrom<&HostFunctionDetails> for Vec<u8> {
55    type Error = Error;
56    #[cfg_attr(feature = "tracing", instrument(err(Debug), skip_all, parent = Span::current(), level= "Trace"))]
57    fn try_from(value: &HostFunctionDetails) -> Result<Vec<u8>> {
58        let mut builder = flatbuffers::FlatBufferBuilder::new();
59        let vec_host_function_definitions = match &value.host_functions {
60            Some(vec_hfd) => {
61                let num_items = vec_hfd.len();
62                let mut host_function_definitions: Vec<WIPOffset<FbHostFunctionDefinition>> =
63                    Vec::with_capacity(num_items);
64
65                for hfd in vec_hfd {
66                    let host_function_definition = hfd.convert_to_flatbuffer_def(&mut builder)?;
67                    host_function_definitions.push(host_function_definition);
68                }
69
70                Some(host_function_definitions)
71            }
72            None => None,
73        };
74
75        let fb_host_function_definitions =
76            vec_host_function_definitions.map(|v| builder.create_vector(&v));
77
78        let host_function_details = FbHostFunctionDetails::create(
79            &mut builder,
80            &FbHostFunctionDetailsArgs {
81                functions: fb_host_function_definitions,
82            },
83        );
84        builder.finish_size_prefixed(host_function_details, None);
85        let res = builder.finished_data().to_vec();
86
87        Ok(res)
88    }
89}