hyperlight_common/flatbuffer_wrappers/
host_function_details.rs

1/*
2Copyright 2025 The Hyperlight Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8    http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17use alloc::vec::Vec;
18
19use anyhow::{Error, Result};
20use flatbuffers::{WIPOffset, size_prefixed_root};
21#[cfg(feature = "tracing")]
22use tracing::{Span, instrument};
23
24use super::host_function_definition::HostFunctionDefinition;
25use crate::flatbuffers::hyperlight::generated::{
26    HostFunctionDefinition as FbHostFunctionDefinition,
27    HostFunctionDetails as FbHostFunctionDetails,
28    HostFunctionDetailsArgs as FbHostFunctionDetailsArgs,
29};
30
31/// `HostFunctionDetails` represents the set of functions that the host exposes to the guest.
32#[derive(Debug, Default, Clone)]
33pub struct HostFunctionDetails {
34    /// The host functions.
35    pub host_functions: Option<Vec<HostFunctionDefinition>>,
36}
37
38impl TryFrom<&[u8]> for HostFunctionDetails {
39    type Error = Error;
40    #[cfg_attr(feature = "tracing", instrument(err(Debug), skip_all, parent = Span::current(), level= "Trace"))]
41    fn try_from(value: &[u8]) -> Result<Self> {
42        let host_function_details_fb = size_prefixed_root::<FbHostFunctionDetails>(value)
43            .map_err(|e| anyhow::anyhow!("Error while reading HostFunctionDetails: {:?}", e))?;
44
45        let host_function_definitions = match host_function_details_fb.functions() {
46            Some(hfd) => {
47                let len = hfd.len();
48                let mut vec_hfd: Vec<HostFunctionDefinition> = Vec::with_capacity(len);
49                for i in 0..len {
50                    let fb_host_function_definition = hfd.get(i);
51                    let hfdef = HostFunctionDefinition::try_from(&fb_host_function_definition)?;
52                    vec_hfd.push(hfdef);
53                }
54
55                Some(vec_hfd)
56            }
57
58            None => None,
59        };
60
61        Ok(Self {
62            host_functions: host_function_definitions,
63        })
64    }
65}
66
67impl TryFrom<&HostFunctionDetails> for Vec<u8> {
68    type Error = Error;
69    #[cfg_attr(feature = "tracing", instrument(err(Debug), skip_all, parent = Span::current(), level= "Trace"))]
70    fn try_from(value: &HostFunctionDetails) -> Result<Vec<u8>> {
71        let mut builder = flatbuffers::FlatBufferBuilder::new();
72        let vec_host_function_definitions = match &value.host_functions {
73            Some(vec_hfd) => {
74                let num_items = vec_hfd.len();
75                let mut host_function_definitions: Vec<WIPOffset<FbHostFunctionDefinition>> =
76                    Vec::with_capacity(num_items);
77
78                for hfd in vec_hfd {
79                    let host_function_definition = hfd.convert_to_flatbuffer_def(&mut builder)?;
80                    host_function_definitions.push(host_function_definition);
81                }
82
83                Some(host_function_definitions)
84            }
85            None => None,
86        };
87
88        let fb_host_function_definitions =
89            vec_host_function_definitions.map(|v| builder.create_vector(&v));
90
91        let host_function_details = FbHostFunctionDetails::create(
92            &mut builder,
93            &FbHostFunctionDetailsArgs {
94                functions: fb_host_function_definitions,
95            },
96        );
97        builder.finish_size_prefixed(host_function_details, None);
98        let res = builder.finished_data().to_vec();
99
100        Ok(res)
101    }
102}