Skip to main content

fdt_edit/node/view/
generic.rs

1//! Generic node view specialization.
2
3use alloc::{string::String, vec::Vec};
4use fdt_raw::{Phandle, RegInfo};
5
6use super::NodeView;
7use crate::{Node, NodeId, RegFixed, ViewMutOp, ViewOp};
8
9// ---------------------------------------------------------------------------
10// GenericNodeView
11// ---------------------------------------------------------------------------
12
13/// A generic node view with no extra specialization.
14#[derive(Clone, Copy)]
15pub struct NodeGeneric<'a> {
16    pub(super) inner: NodeView<'a>,
17}
18
19impl<'a> NodeGeneric<'a> {
20    pub fn id(&self) -> NodeId {
21        self.inner.id()
22    }
23
24    pub fn path(&self) -> String {
25        self.inner.path()
26    }
27
28    pub fn regs(&self) -> Vec<RegFixed> {
29        self.inner.regs()
30    }
31
32    /// Returns the effective `interrupt-parent`, inheriting from ancestors.
33    pub fn interrupt_parent(&self) -> Option<Phandle> {
34        self.inner.interrupt_parent()
35    }
36}
37
38impl<'a> ViewOp<'a> for NodeGeneric<'a> {
39    fn as_view(&self) -> NodeView<'a> {
40        self.inner
41    }
42}
43
44// ---------------------------------------------------------------------------
45// GenericNodeViewMut
46// ---------------------------------------------------------------------------
47
48/// Mutable view for generic nodes.
49pub struct NodeGenericMut<'a> {
50    pub(super) inner: NodeView<'a>,
51}
52
53impl<'a> ViewOp<'a> for NodeGenericMut<'a> {
54    fn as_view(&self) -> NodeView<'a> {
55        self.inner
56    }
57}
58
59impl<'a> ViewMutOp<'a> for NodeGenericMut<'a> {
60    fn new(node: NodeGenericMut<'a>) -> Self {
61        Self { inner: node.inner }
62    }
63}
64
65impl<'a> NodeGenericMut<'a> {
66    pub fn id(&self) -> NodeId {
67        self.inner.id()
68    }
69
70    pub fn path(&self) -> String {
71        self.inner.path()
72    }
73
74    pub fn set_regs(&mut self, regs: &[RegInfo]) {
75        self.inner.set_regs(regs);
76    }
77
78    pub fn add_child_generic(&mut self, name: &str) -> NodeGenericMut<'a> {
79        let node = Node::new(name);
80        let new_id = self.inner.fdt_mut().add_node(self.inner.id(), node);
81        let new_view = NodeView::new(self.inner.fdt(), new_id);
82        NodeGenericMut { inner: new_view }
83    }
84
85    pub(crate) fn add_child<T: ViewMutOp<'a>>(&mut self, name: &str) -> T {
86        let generic_child = self.add_child_generic(name);
87        T::new(generic_child)
88    }
89}