libfdt: Add FdtNodeMut::as_node()
This helps to call FdtNode APIs without searching the same node again
with Fdt::node().
Bug: 277993056
Test: atest libpvmfw.device_assignment.test, launch protected VM
Change-Id: I9c148947f91e12bd96c976cd882267fa6a8b0a99
diff --git a/libs/libfdt/src/lib.rs b/libs/libfdt/src/lib.rs
index b187c33..b811730 100644
--- a/libs/libfdt/src/lib.rs
+++ b/libs/libfdt/src/lib.rs
@@ -696,6 +696,11 @@
self.fdt
}
+ /// Returns immutable FdtNode of this node.
+ pub fn as_node(&self) -> FdtNode {
+ FdtNode { fdt: self.fdt, offset: self.offset }
+ }
+
/// Adds a new subnode to the given node and return it as a FdtNodeMut on success.
pub fn add_subnode(&'a mut self, name: &CStr) -> Result<Self> {
let offset = self.add_subnode_offset(name.to_bytes())?;
diff --git a/libs/libfdt/tests/api_test.rs b/libs/libfdt/tests/api_test.rs
index f4a4af5..343552d 100644
--- a/libs/libfdt/tests/api_test.rs
+++ b/libs/libfdt/tests/api_test.rs
@@ -259,3 +259,18 @@
// Validates type.
let _symbols: FdtNodeMut = fdt.symbols_mut().unwrap().unwrap();
}
+
+#[test]
+fn node_mut_as_node() {
+ let mut data = fs::read(TEST_TREE_WITH_ONE_MEMORY_RANGE_PATH).unwrap();
+ let fdt = Fdt::from_mut_slice(&mut data).unwrap();
+
+ let mut memory = fdt.node_mut(cstr!("/memory")).unwrap().unwrap();
+ {
+ let memory = memory.as_node();
+ assert_eq!(memory.name(), Ok(cstr!("memory")));
+ }
+
+ // Just check whether borrow checker doesn't complain this.
+ memory.setprop_inplace(cstr!("device_type"), b"MEMORY\0").unwrap();
+}