Skip to main content

fdt_edit/node/view/
intc.rs

1//! Interrupt controller node view specialization.
2
3use core::ops::Deref;
4
5use alloc::vec::Vec;
6
7use super::NodeView;
8use crate::{NodeGeneric, NodeGenericMut, Property, ViewMutOp, ViewOp};
9
10// ---------------------------------------------------------------------------
11// IntcNodeView
12// ---------------------------------------------------------------------------
13
14/// Specialized view for interrupt controller nodes.
15#[derive(Clone, Copy)]
16pub struct IntcNodeView<'a> {
17    pub(super) inner: NodeGeneric<'a>,
18}
19
20impl<'a> ViewOp<'a> for IntcNodeView<'a> {
21    fn as_view(&self) -> NodeView<'a> {
22        self.inner.as_view()
23    }
24}
25
26impl<'a> Deref for IntcNodeView<'a> {
27    type Target = NodeGeneric<'a>;
28    fn deref(&self) -> &Self::Target {
29        &self.inner
30    }
31}
32
33impl<'a> IntcNodeView<'a> {
34    pub(crate) fn try_from_view(view: NodeView<'a>) -> Option<Self> {
35        if view.as_node().is_interrupt_controller() {
36            Some(Self {
37                inner: NodeGeneric { inner: view },
38            })
39        } else {
40            None
41        }
42    }
43
44    /// Returns the `#interrupt-cells` property value.
45    pub fn interrupt_cells(&self) -> Option<u32> {
46        self.as_view().as_node().interrupt_cells()
47    }
48
49    /// This is always `true` for `IntcNodeView` (type-level guarantee).
50    pub fn is_interrupt_controller(&self) -> bool {
51        true
52    }
53}
54
55// ---------------------------------------------------------------------------
56// IntcNodeViewMut
57// ---------------------------------------------------------------------------
58
59/// Mutable view for interrupt controller nodes.
60pub struct IntcNodeViewMut<'a> {
61    pub(super) inner: NodeGenericMut<'a>,
62}
63
64impl<'a> ViewOp<'a> for IntcNodeViewMut<'a> {
65    fn as_view(&self) -> NodeView<'a> {
66        self.inner.as_view()
67    }
68}
69
70impl<'a> ViewMutOp<'a> for IntcNodeViewMut<'a> {
71    fn new(node: NodeGenericMut<'a>) -> Self {
72        let mut s = Self { inner: node };
73        let n = s.inner.inner.as_node_mut();
74        n.set_property(Property::new("interrupt-controller", Vec::new()));
75        s
76    }
77}
78
79impl<'a> Deref for IntcNodeViewMut<'a> {
80    type Target = NodeGenericMut<'a>;
81    fn deref(&self) -> &Self::Target {
82        &self.inner
83    }
84}
85
86impl<'a> IntcNodeViewMut<'a> {
87    pub(crate) fn try_from_view(view: NodeView<'a>) -> Option<Self> {
88        if view.as_node().is_interrupt_controller() {
89            Some(Self {
90                inner: NodeGenericMut { inner: view },
91            })
92        } else {
93            None
94        }
95    }
96}