Skip to main content

ploidy_core/ir/views/
primitive.rs

1//! Primitives: scalar types.
2//!
3//! Primitives are leaf nodes that don't reference other types in the graph.
4//! Codegen maps each [`PrimitiveType`] variant to a language-specific type:
5//! for example, [`PrimitiveType::String`] becomes a [`String`] in Rust.
6//! See [`PrimitiveType`] for the full list of variants.
7
8use petgraph::graph::NodeIndex;
9
10use crate::ir::{CookedGraph, PrimitiveType};
11
12use super::ViewNode;
13
14/// A graph-aware view of a [primitive type][PrimitiveType].
15#[derive(Debug)]
16pub struct PrimitiveView<'a> {
17    cooked: &'a CookedGraph<'a>,
18    index: NodeIndex<usize>,
19    ty: PrimitiveType,
20}
21
22impl<'a> PrimitiveView<'a> {
23    #[inline]
24    pub(in crate::ir) fn new(
25        cooked: &'a CookedGraph<'a>,
26        index: NodeIndex<usize>,
27        ty: PrimitiveType,
28    ) -> Self {
29        Self { cooked, index, ty }
30    }
31
32    /// Returns the primitive type.
33    #[inline]
34    pub fn ty(&self) -> PrimitiveType {
35        self.ty
36    }
37}
38
39impl<'a> ViewNode<'a> for PrimitiveView<'a> {
40    #[inline]
41    fn cooked(&self) -> &'a CookedGraph<'a> {
42        self.cooked
43    }
44
45    #[inline]
46    fn index(&self) -> NodeIndex<usize> {
47        self.index
48    }
49}