libfdt: Refactor to keep iterators in one place
This explicitly encapsulates FFI calls in lib.rs.
Bug: 277993056
Test: atest liblibfdt.integration_test
Change-Id: I31610911e63aae543686c1b3484cc6c1e60480aa
diff --git a/libs/libfdt/src/iterators.rs b/libs/libfdt/src/iterators.rs
index 280257b..13d262e 100644
--- a/libs/libfdt/src/iterators.rs
+++ b/libs/libfdt/src/iterators.rs
@@ -14,11 +14,41 @@
//! Iterators over cells, and various layers on top of them.
+use crate::Fdt;
use crate::FdtError;
+use crate::FdtNode;
use crate::{AddrCells, SizeCells};
+use core::ffi::CStr;
use core::marker::PhantomData;
use core::{mem::size_of, ops::Range, slice::ChunksExact};
+/// Iterator over nodes sharing a same compatible string.
+pub struct CompatibleIterator<'a> {
+ node: FdtNode<'a>,
+ compatible: &'a CStr,
+}
+
+impl<'a> CompatibleIterator<'a> {
+ pub(crate) fn new(fdt: &'a Fdt, compatible: &'a CStr) -> Result<Self, FdtError> {
+ let node = fdt.root()?;
+ Ok(Self { node, compatible })
+ }
+}
+
+impl<'a> Iterator for CompatibleIterator<'a> {
+ type Item = FdtNode<'a>;
+
+ fn next(&mut self) -> Option<Self::Item> {
+ let next = self.node.next_compatible(self.compatible).ok()?;
+
+ if let Some(node) = next {
+ self.node = node;
+ }
+
+ next
+ }
+}
+
/// Iterator over cells of a DT property.
#[derive(Debug)]
pub struct CellIterator<'a> {
diff --git a/libs/libfdt/src/lib.rs b/libs/libfdt/src/lib.rs
index 19ce0f7..b573697 100644
--- a/libs/libfdt/src/lib.rs
+++ b/libs/libfdt/src/lib.rs
@@ -19,7 +19,10 @@
mod iterators;
-pub use iterators::{AddressRange, CellIterator, MemRegIterator, RangesIterator, Reg, RegIterator};
+pub use iterators::{
+ AddressRange, CellIterator, CompatibleIterator, MemRegIterator, RangesIterator, Reg,
+ RegIterator,
+};
use core::cmp::max;
use core::ffi::{c_int, c_void, CStr};
@@ -586,33 +589,6 @@
}
}
-/// Iterator over nodes sharing a same compatible string.
-pub struct CompatibleIterator<'a> {
- node: FdtNode<'a>,
- compatible: &'a CStr,
-}
-
-impl<'a> CompatibleIterator<'a> {
- fn new(fdt: &'a Fdt, compatible: &'a CStr) -> Result<Self> {
- let node = fdt.root()?;
- Ok(Self { node, compatible })
- }
-}
-
-impl<'a> Iterator for CompatibleIterator<'a> {
- type Item = FdtNode<'a>;
-
- fn next(&mut self) -> Option<Self::Item> {
- let next = self.node.next_compatible(self.compatible).ok()?;
-
- if let Some(node) = next {
- self.node = node;
- }
-
- next
- }
-}
-
/// Wrapper around low-level libfdt functions.
#[derive(Debug)]
#[repr(transparent)]