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();
diff --git a/pvmfw/src/fdt.rs b/pvmfw/src/fdt.rs
index bd47ed7..311f467 100644
--- a/pvmfw/src/fdt.rs
+++ b/pvmfw/src/fdt.rs
@@ -443,7 +443,7 @@
vm_ref_dt: &Fdt,
props_info: &BTreeMap<CString, Vec<u8>>,
) -> libfdt::Result<()> {
- let mut root_vm_dt = vm_dt.root_mut()?;
+ let root_vm_dt = vm_dt.root_mut()?;
let mut avf_vm_dt = root_vm_dt.add_subnode(cstr!("avf"))?;
// TODO(b/318431677): Validate nodes beyond /avf.
let avf_node = vm_ref_dt.node(cstr!("/avf"))?.ok_or(FdtError::NotFound)?;
diff --git a/virtualizationmanager/src/dt_overlay.rs b/virtualizationmanager/src/dt_overlay.rs
index 83f7734..71d3a26 100644
--- a/virtualizationmanager/src/dt_overlay.rs
+++ b/virtualizationmanager/src/dt_overlay.rs
@@ -57,20 +57,19 @@
let fdt =
Fdt::create_empty_tree(buffer).map_err(|e| anyhow!("Failed to create empty Fdt: {e:?}"))?;
- let mut root = fdt.root_mut().map_err(|e| anyhow!("Failed to get root: {e:?}"))?;
+ let root = fdt.root_mut().map_err(|e| anyhow!("Failed to get root: {e:?}"))?;
let mut node =
root.add_subnode(cstr!("fragment@0")).map_err(|e| anyhow!("Failed to fragment: {e:?}"))?;
node.setprop(cstr!("target-path"), b"/\0")
.map_err(|e| anyhow!("Failed to set target-path: {e:?}"))?;
- let mut node = node
+ let node = node
.add_subnode(cstr!("__overlay__"))
.map_err(|e| anyhow!("Failed to __overlay__ node: {e:?}"))?;
if !untrusted_props.is_empty() {
let mut node = node
.add_subnode(AVF_NODE_NAME)
- .map_err(|e| anyhow!("Failed to add avf node: {e:?}"))?;
- let mut node = node
+ .map_err(|e| anyhow!("Failed to add avf node: {e:?}"))?
.add_subnode(UNTRUSTED_NODE_NAME)
.map_err(|e| anyhow!("Failed to add /avf/untrusted node: {e:?}"))?;
for (name, value) in untrusted_props {
diff --git a/vmbase/example/src/main.rs b/vmbase/example/src/main.rs
index 6f513ee..48b24be 100644
--- a/vmbase/example/src/main.rs
+++ b/vmbase/example/src/main.rs
@@ -181,7 +181,7 @@
info!("FDT successfully unpacked.");
let path = cstr!("/memory");
- let mut node = writer.node_mut(path).unwrap().unwrap();
+ let node = writer.node_mut(path).unwrap().unwrap();
let name = cstr!("child");
let mut child = node.add_subnode(name).unwrap();
info!("Created subnode '{}/{}'.", path.to_str().unwrap(), name.to_str().unwrap());