libfdt: Change FdtNodeMut::add_subnode*(self, ...)
As the borrow checker forbids having more than one mutable reference at
the same time and as FdtNodeMut is a wrapper around &mut Fdt, take the
ownership of the node when obtaining its first_subnode, allowing less
convoluted code when walking a mutable DT.
Bug: 324046698
Test: m pvmfw virtmgr fsfdt librialto libfdtpci
Test: m liblibfdt.integration_test libpvmfw.device_assignment.test
Change-Id: I1bbbebe263c21f95b3637bb6266b4308079ada8f
diff --git a/libs/libfdt/src/lib.rs b/libs/libfdt/src/lib.rs
index d1ab24e..d254af1 100644
--- a/libs/libfdt/src/lib.rs
+++ b/libs/libfdt/src/lib.rs
@@ -434,7 +434,7 @@
}
/// Adds new subnodes to the given node.
- pub fn add_subnodes(&mut self, names: &[&CStr]) -> Result<()> {
+ pub fn add_subnodes(self, names: &[&CStr]) -> Result<()> {
for name in names {
self.fdt.add_subnode_namelen(self.offset, name.to_bytes())?;
}
@@ -442,7 +442,7 @@
}
/// 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> {
+ pub fn add_subnode(self, name: &CStr) -> Result<Self> {
let name = name.to_bytes();
let offset = self.fdt.add_subnode_namelen(self.offset, name)?;
@@ -451,7 +451,7 @@
/// Adds a new subnode to the given node with name and namelen, and returns it as a FdtNodeMut
/// on success.
- pub fn add_subnode_with_namelen(&'a mut self, name: &CStr, namelen: usize) -> Result<Self> {
+ pub fn add_subnode_with_namelen(self, name: &CStr, namelen: usize) -> Result<Self> {
let name = &name.to_bytes()[..namelen];
let offset = self.fdt.add_subnode_namelen(self.offset, name)?;
diff --git a/libs/libfdt/tests/api_test.rs b/libs/libfdt/tests/api_test.rs
index ddc4538..c2ddda0 100644
--- a/libs/libfdt/tests/api_test.rs
+++ b/libs/libfdt/tests/api_test.rs
@@ -267,8 +267,8 @@
let node = fdt.node(node_path).unwrap().unwrap();
assert_eq!(Ok(None), node.subnode_with_name_bytes(name));
- let mut node = fdt.node_mut(node_path).unwrap().unwrap();
- node.add_subnode_with_namelen(subnode_name, len).unwrap();
+ let node = fdt.node_mut(node_path).unwrap().unwrap();
+ let _ = node.add_subnode_with_namelen(subnode_name, len).unwrap();
let node = fdt.node(node_path).unwrap().unwrap();
assert_ne!(Ok(None), node.subnode_with_name_bytes(name));
@@ -456,7 +456,7 @@
let mut data = vec![0_u8; 1000];
let fdt = Fdt::create_empty_tree(&mut data).unwrap();
- let mut root = fdt.root_mut().unwrap();
+ let root = fdt.root_mut().unwrap();
let names = [cstr!("a"), cstr!("b")];
root.add_subnodes(&names).unwrap();