Skip to main content

jeff/reader/value/
function_io.rs

1//! Input/output types and metadata for functions.
2
3use crate::capnp::jeff_capnp;
4use crate::reader::metadata::sealed::HasMetadataSealed;
5use crate::reader::string_table::StringTable;
6
7use crate::types::Type;
8
9use super::WireValue;
10
11/// Input/output type for a function, with associated metadata.
12///
13/// This corresponds to a jeff format `Value`.
14#[derive(Clone, Copy, Debug)]
15pub struct FunctionIOValue<'a> {
16    /// Type of the hyperedge.
17    value_type: Type,
18    /// Metadata associated with the value.
19    metadata: capnp::struct_list::Reader<'a, jeff_capnp::meta::Owned>,
20    /// Module-level register of reused strings.
21    strings: StringTable<'a>,
22}
23
24impl<'a> FunctionIOValue<'a> {
25    /// Create a new function view from a capnp reader.
26    pub(crate) fn read_capnp(
27        value: jeff_capnp::value::Reader<'a>,
28        strings: StringTable<'a>,
29    ) -> Self {
30        let value_type = value
31            .get_type()
32            .map(Type::read_capnp)
33            .expect("Type should be present");
34        let metadata = value.get_metadata().expect("Metadata should be present");
35        Self {
36            value_type,
37            metadata,
38            strings,
39        }
40    }
41
42    /// Returns the type of this value.
43    pub fn ty(&self) -> Type {
44        self.value_type
45    }
46}
47
48impl<'a> HasMetadataSealed for FunctionIOValue<'a> {
49    fn strings(&self) -> StringTable<'a> {
50        self.strings
51    }
52
53    fn metadata_reader(&self) -> capnp::struct_list::Reader<'a, jeff_capnp::meta::Owned> {
54        self.metadata
55    }
56}
57
58impl<'a> From<WireValue<'a>> for FunctionIOValue<'a> {
59    fn from(wire_value: WireValue<'a>) -> Self {
60        Self {
61            value_type: wire_value.value_type,
62            metadata: wire_value.metadata,
63            strings: wire_value.strings,
64        }
65    }
66}