1+ //! A simple package for DTB depth-first walking.
2+ //!
3+ //! # Example
4+ //!
5+ //! ```cmd
6+ //! cargo run --release --example qemu-virt
7+ //! ```
8+ //!
9+ //! # Usage
10+ //!
11+ //! ```rust,no_run
12+ //! Dtb::from_raw_parts(dtb).unwrap()
13+ //! ```
14+
115#![ no_std]
2- #![ deny( warnings) ] // cancel this line during developing
16+ #![ deny( warnings, unstable_features , missing_docs ) ] // cancel this line during developing
317
418mod header;
519mod indent;
@@ -9,8 +23,10 @@ mod structure_block;
923mod walker;
1024
1125pub use path:: Path ;
12- pub use property:: { PHandle , Property , Reg , Str , StrList } ;
26+ pub use property:: { PHandle , Property , Reg , StrList } ;
1327pub mod utils {
28+ //! 用于设备树解析、格式化的工具集。
29+
1430 pub use crate :: indent:: indent;
1531}
1632pub use header:: HeaderError ;
@@ -64,9 +80,12 @@ impl Dtb<'static> {
6480 }
6581}
6682
83+ /// 从内存切片构造设备树二进制对象失败。
6784pub enum ConvertError {
68- Truncated ,
85+ /// 首部检查未通过。
6986 Header ( HeaderError ) ,
87+ /// 切片未能容纳整个设备树。
88+ Truncated ,
7089}
7190
7291impl < ' a > Dtb < ' a > {
@@ -127,24 +146,61 @@ impl Dtb<'_> {
127146
128147/// 设备树二进制小对象。
129148pub enum DtbObj < ' a > {
130- /// 子节点
131- SubNode { name : & ' a [ u8 ] } ,
132- /// 一般属性
149+ /// 子节点。
150+ SubNode {
151+ /// 节点名。
152+ name : Str < ' a > ,
153+ } ,
154+ /// 一般属性。
133155 Property ( Property < ' a > ) ,
134156}
135157
136158/// 遍历操作。
137159pub enum WalkOperation {
138- /// 进入子节点
160+ /// 进入子节点。
139161 StepInto ,
140- /// 跳过子节点
162+ /// 跳过子节点。
141163 StepOver ,
142- /// 跳过当前子树
164+ /// 跳过当前子树。
143165 StepOut ,
144- /// 结束遍历
166+ /// 结束遍历。
145167 Terminate ,
146168}
147169
170+ /// 地址空间上的一个字符串,但未检查是否符合 utf-8 编码。
171+ #[ derive( Clone , Copy ) ]
172+ pub struct Str < ' a > ( & ' a [ u8 ] ) ;
173+
174+ impl Str < ' _ > {
175+ /// Converts to `&[u8]`.
176+ #[ inline]
177+ pub fn as_bytes ( & self ) -> & [ u8 ] {
178+ self . 0
179+ }
180+
181+ /// Converts to [`str`].
182+ #[ inline]
183+ pub fn as_str ( & self ) -> Result < & str , core:: str:: Utf8Error > {
184+ core:: str:: from_utf8 ( self . 0 )
185+ }
186+
187+ /// Converts to [`str`] without checking utf-8 validity.
188+ ///
189+ /// # Safety
190+ ///
191+ /// see [`core::str::from_utf8_unchecked`].
192+ #[ inline]
193+ pub unsafe fn as_str_unchecked ( & self ) -> & str {
194+ core:: str:: from_utf8_unchecked ( self . 0 )
195+ }
196+ }
197+
198+ impl fmt:: Display for Str < ' _ > {
199+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
200+ unsafe { self . as_str_unchecked ( ) } . fmt ( f)
201+ }
202+ }
203+
148204#[ repr( transparent) ]
149205#[ derive( Clone , Copy , PartialEq , Eq ) ]
150206struct U32BigEndian ( u32 ) ;
0 commit comments