@@ -13,7 +13,7 @@ use crate::{TagTrait, TagType, TagTypeId};
13
13
use core:: fmt:: { Debug , Formatter } ;
14
14
use core:: mem;
15
15
use core:: ptr;
16
- use multiboot2_common:: increase_to_alignment ;
16
+ use multiboot2_common:: iter :: TagBytesIter ;
17
17
use multiboot2_common:: { BytesRef , DynSizedStructure , Header } ;
18
18
19
19
/// The common header that all tags have in common. This type is ABI compatible.
@@ -65,10 +65,8 @@ impl GenericTag {
65
65
pub fn ref_from < ' a > ( bytes : & ' a [ u8 ] ) -> & ' a Self {
66
66
let bytes = BytesRef :: < TagHeader > :: try_from ( bytes) . unwrap ( ) ;
67
67
let inner = DynSizedStructure :: ref_from ( bytes) . unwrap ( ) ;
68
- let tag = unsafe { mem:: transmute :: < _ , & ' a Self > ( inner) } ;
69
- tag
68
+ unsafe { mem:: transmute :: < _ , & ' a Self > ( inner) }
70
69
}
71
-
72
70
/// Returns the underlying [`TagHeader`].
73
71
pub const fn header ( & self ) -> & TagHeader {
74
72
self . 0 . header ( )
@@ -83,7 +81,7 @@ impl GenericTag {
83
81
/// may be a ZST or DST typed tag.
84
82
pub fn cast < T : TagTrait + ?Sized > ( & self ) -> & T {
85
83
let base_ptr = ptr:: addr_of!( * self ) ;
86
- let t_dst_size = T :: dst_len ( & self . 0 . header ( ) ) ;
84
+ let t_dst_size = T :: dst_len ( self . 0 . header ( ) ) ;
87
85
let t_ptr = ptr_meta:: from_raw_parts ( base_ptr. cast ( ) , t_dst_size) ;
88
86
let t_ref = unsafe { & * t_ptr } ;
89
87
assert_eq ! ( mem:: size_of_val( self ) , mem:: size_of_val( t_ref) ) ;
@@ -94,7 +92,7 @@ impl GenericTag {
94
92
impl Debug for GenericTag {
95
93
fn fmt ( & self , f : & mut Formatter < ' _ > ) -> core:: fmt:: Result {
96
94
f. debug_struct ( "GenericTag" )
97
- . field ( "header" , & self . 0 . header ( ) )
95
+ . field ( "header" , self . 0 . header ( ) )
98
96
. field ( "payload" , & "<bytes>" )
99
97
. finish ( )
100
98
}
@@ -109,65 +107,24 @@ impl TagTrait for GenericTag {
109
107
}
110
108
}
111
109
112
- /// Iterates the tags of the MBI from the first tag to the end tag. THe end tag
113
- /// included.
110
+ /// Iterates the tags of the MBI from the first tag to the end tag. The end tag
111
+ /// is included.
114
112
#[ derive( Clone , Debug ) ]
115
- pub struct TagIter < ' a > {
116
- /// Absolute offset to next tag and updated in each iteration.
117
- next_tag_offset : usize ,
118
- exclusive_end : * const u8 ,
119
- buffer : & ' a [ u8 ] ,
120
- }
113
+ pub struct TagIter < ' a > ( TagBytesIter < ' a , TagHeader > ) ;
121
114
122
115
impl < ' a > TagIter < ' a > {
123
- /// Creates a new iterator
116
+ /// Creates a new iterator.
124
117
pub fn new ( mem : & ' a [ u8 ] ) -> Self {
125
- // Assert alignment.
126
- assert_eq ! ( mem. as_ptr( ) . align_offset( 8 ) , 0 ) ;
127
-
128
- let exclusive_end = unsafe { mem. as_ptr ( ) . add ( mem. len ( ) ) } ;
129
-
130
- TagIter {
131
- next_tag_offset : 0 ,
132
- buffer : mem,
133
- exclusive_end,
134
- }
118
+ Self ( TagBytesIter :: new ( mem) )
135
119
}
136
120
}
137
121
138
122
impl < ' a > Iterator for TagIter < ' a > {
139
123
type Item = & ' a GenericTag ;
140
124
141
125
fn next ( & mut self ) -> Option < Self :: Item > {
142
- let next_ptr = unsafe { self . buffer . as_ptr ( ) . add ( self . next_tag_offset ) } ;
143
-
144
- if next_ptr == self . exclusive_end {
145
- return None ;
146
- }
147
- assert ! ( next_ptr < self . exclusive_end) ;
148
-
149
- let next_tag_ptr = next_ptr. cast :: < TagHeader > ( ) ;
150
-
151
- let tag_hdr = unsafe { & * next_tag_ptr } ;
152
-
153
- // Get relevant byte portion for the next tag. This includes padding
154
- // bytes to fulfill Rust memory guarantees. Otherwise, Miri complains.
155
- // See <https://doc.rust-lang.org/reference/type-layout.html>.
156
- let bytes = {
157
- let from = self . next_tag_offset ;
158
- let to = from + tag_hdr. size as usize ;
159
- // The size of [the allocation for] a value is always a multiple of its
160
- // alignment.
161
- // https://doc.rust-lang.org/reference/type-layout.html
162
- let to = increase_to_alignment ( to) ;
163
-
164
- // Update ptr for next iteration.
165
- self . next_tag_offset += to - from;
166
-
167
- & self . buffer [ from..to]
168
- } ;
169
-
170
- Some ( GenericTag :: ref_from ( bytes) )
126
+ let bytes = self . 0 . next ( ) ?;
127
+ Some ( GenericTag :: ref_from ( bytes. as_ref ( ) ) )
171
128
}
172
129
}
173
130
0 commit comments