@@ -12,7 +12,7 @@ use core::fmt::{self, Display, Formatter};
1212
1313use super :: { FDT_TAGSIZE , Fdt , FdtToken } ;
1414use crate :: fdt:: property:: { FdtPropIter , FdtProperty } ;
15- use crate :: standard:: AddressSpaceProperties ;
15+ use crate :: standard:: { AddressSpaceProperties , Node , Property } ;
1616
1717/// A node in a flattened device tree.
1818#[ derive( Debug , Clone , Copy ) ]
@@ -24,14 +24,8 @@ pub struct FdtNode<'a> {
2424 pub ( crate ) parent_address_space : AddressSpaceProperties ,
2525}
2626
27- impl < ' a > FdtNode < ' a > {
28- pub ( crate ) fn new ( fdt : Fdt < ' a > , offset : usize ) -> Self {
29- Self {
30- fdt,
31- offset,
32- parent_address_space : AddressSpaceProperties :: default ( ) ,
33- }
34- }
27+ impl < ' a > Node < ' a > for FdtNode < ' a > {
28+ type Property = FdtProperty < ' a > ;
3529
3630 /// Returns the name of this node.
3731 ///
@@ -44,38 +38,22 @@ impl<'a> FdtNode<'a> {
4438 /// # Examples
4539 ///
4640 /// ```
47- /// # use dtoolkit::fdt::Fdt;
41+ /// use dtoolkit::fdt::Fdt;
42+ /// use dtoolkit::standard::Node;
43+ ///
4844 /// # let dtb = include_bytes!("../../tests/dtb/test_children.dtb");
4945 /// let fdt = Fdt::new(dtb).unwrap();
5046 /// let root = fdt.root();
5147 /// let child = root.child("child1").unwrap();
5248 /// assert_eq!(child.name(), "child1");
5349 /// ```
54- #[ must_use]
55- pub fn name ( & self ) -> & ' a str {
50+ fn name ( & self ) -> & ' a str {
5651 let name_offset = self . offset + FDT_TAGSIZE ;
5752 self . fdt
5853 . string_at_offset ( name_offset, None )
5954 . expect ( "Fdt should be valid" )
6055 }
6156
62- /// Returns the name of this node without the unit address, if any.
63- ///
64- /// # Panics
65- ///
66- /// Panics if the [`Fdt`] structure was constructed using
67- /// [`Fdt::new_unchecked`] or [`Fdt::from_raw_unchecked`] and the FDT is not
68- /// valid.
69- #[ must_use]
70- pub fn name_without_address ( & self ) -> & ' a str {
71- let name = self . name ( ) ;
72- if let Some ( ( name, _) ) = name. split_once ( '@' ) {
73- name
74- } else {
75- name
76- }
77- }
78-
7957 /// Returns a property by its name.
8058 ///
8159 /// # Performance
@@ -85,7 +63,9 @@ impl<'a> FdtNode<'a> {
8563 /// # Examples
8664 ///
8765 /// ```
88- /// # use dtoolkit::fdt::Fdt;
66+ /// use dtoolkit::fdt::Fdt;
67+ /// use dtoolkit::standard::{Node, Property};
68+ ///
8969 /// # let dtb = include_bytes!("../../tests/dtb/test_props.dtb");
9070 /// let fdt = Fdt::new(dtb).unwrap();
9171 /// let node = fdt.find_node("/test-props").unwrap();
@@ -98,8 +78,7 @@ impl<'a> FdtNode<'a> {
9878 /// Panics if the [`Fdt`] structure was constructed using
9979 /// [`Fdt::new_unchecked`] or [`Fdt::from_raw_unchecked`] and the FDT is not
10080 /// valid.
101- #[ must_use]
102- pub fn property ( & self , name : & str ) -> Option < FdtProperty < ' a > > {
81+ fn property ( & self , name : & str ) -> Option < FdtProperty < ' a > > {
10382 self . properties ( ) . find ( |property| property. name ( ) == name)
10483 }
10584
@@ -108,7 +87,9 @@ impl<'a> FdtNode<'a> {
10887 /// # Examples
10988 ///
11089 /// ```
111- /// # use dtoolkit::fdt::Fdt;
90+ /// use dtoolkit::fdt::Fdt;
91+ /// use dtoolkit::standard::{Node, Property};
92+ ///
11293 /// # let dtb = include_bytes!("../../tests/dtb/test_props.dtb");
11394 /// let fdt = Fdt::new(dtb).unwrap();
11495 /// let node = fdt.find_node("/test-props").unwrap();
@@ -117,78 +98,21 @@ impl<'a> FdtNode<'a> {
11798 /// assert_eq!(props.next().unwrap().name(), "u64-prop");
11899 /// assert_eq!(props.next().unwrap().name(), "str-prop");
119100 /// ```
120- pub fn properties ( & self ) -> impl Iterator < Item = FdtProperty < ' a > > + use < ' a > {
101+ fn properties ( & self ) -> impl Iterator < Item = FdtProperty < ' a > > + use < ' a > {
121102 FdtPropIter :: Start {
122103 fdt : self . fdt ,
123104 offset : self . offset ,
124105 }
125106 }
126107
127- /// Returns a child node by its name.
128- ///
129- /// If the given name contains a _unit-address_ (the part after the `@`
130- /// sign) then both the _node-name_ and _unit-address_ must match. If it
131- /// doesn't have a _unit-address_, then nodes with any _unit-address_ or
132- /// none will be allowed.
133- ///
134- /// For example, searching for `memory` as a child of `/` would match either
135- /// `/memory` or `/memory@4000`, while `memory@4000` would match only the
136- /// latter.
137- ///
138- /// # Performance
139- ///
140- /// This method's performance is linear in the number of children of this
141- /// node because it iterates through the children. If you need to call this
142- /// often, consider converting to a
143- /// [`DeviceTreeNode`](crate::model::DeviceTreeNode) first. Child lookup
144- /// on a [`DeviceTreeNode`](crate::model::DeviceTreeNode) is a
145- /// constant-time operation.
146- ///
147- /// # Panics
148- ///
149- /// Panics if the [`Fdt`] structure was constructed using
150- /// [`Fdt::new_unchecked`] or [`Fdt::from_raw_unchecked`] and the FDT is not
151- /// valid.
152- ///
153- /// # Examples
154- ///
155- /// ```
156- /// # use dtoolkit::fdt::Fdt;
157- /// # let dtb = include_bytes!("../../tests/dtb/test_children.dtb");
158- /// let fdt = Fdt::new(dtb).unwrap();
159- /// let root = fdt.root();
160- /// let child = root.child("child1").unwrap();
161- /// assert_eq!(child.name(), "child1");
162- /// ```
163- ///
164- /// ```
165- /// # use dtoolkit::fdt::Fdt;
166- /// # let dtb = include_bytes!("../../tests/dtb/test_children.dtb");
167- /// let fdt = Fdt::new(dtb).unwrap();
168- /// let root = fdt.root();
169- /// let child = root.child("child2").unwrap();
170- /// assert_eq!(child.name(), "child2@42");
171- /// let child = root.child("child2@42").unwrap();
172- /// assert_eq!(child.name(), "child2@42");
173- /// ```
174- #[ must_use]
175- pub fn child ( & self , name : & str ) -> Option < FdtNode < ' a > > {
176- let include_address = name. contains ( '@' ) ;
177- self . children ( ) . find ( |& child| {
178- if include_address {
179- child. name ( ) == name
180- } else {
181- child. name_without_address ( ) == name
182- }
183- } )
184- }
185-
186108 /// Returns an iterator over the children of this node.
187109 ///
188110 /// # Examples
189111 ///
190112 /// ```
191- /// # use dtoolkit::fdt::Fdt;
113+ /// use dtoolkit::fdt::Fdt;
114+ /// use dtoolkit::standard::Node;
115+ ///
192116 /// # let dtb = include_bytes!("../../tests/dtb/test_children.dtb");
193117 /// let fdt = Fdt::new(dtb).unwrap();
194118 /// let root = fdt.root();
@@ -197,9 +121,19 @@ impl<'a> FdtNode<'a> {
197121 /// assert_eq!(children.next().unwrap().name(), "child2@42");
198122 /// assert!(children.next().is_none());
199123 /// ```
200- pub fn children ( & self ) -> impl Iterator < Item = FdtNode < ' a > > + use < ' a > {
124+ fn children ( & self ) -> impl Iterator < Item = FdtNode < ' a > > + use < ' a > {
201125 FdtChildIter :: Start { node : * self }
202126 }
127+ }
128+
129+ impl < ' a > FdtNode < ' a > {
130+ pub ( crate ) fn new ( fdt : Fdt < ' a > , offset : usize ) -> Self {
131+ Self {
132+ fdt,
133+ offset,
134+ parent_address_space : AddressSpaceProperties :: default ( ) ,
135+ }
136+ }
203137
204138 pub ( crate ) fn fmt_recursive ( & self , f : & mut Formatter , indent : usize ) -> fmt:: Result {
205139 let name = self . name ( ) ;
0 commit comments