Skip to content

Commit af45ab2

Browse files
committed
multiboot2: use TagIter from common package
1 parent 4e85cdc commit af45ab2

File tree

2 files changed

+12
-55
lines changed

2 files changed

+12
-55
lines changed

multiboot2/src/boot_information.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ impl<'a> BootInformation<'a> {
410410

411411
/// Returns an iterator over all tags.
412412
fn tags(&self) -> TagIter {
413-
TagIter::new(&self.0.payload())
413+
TagIter::new(self.0.payload())
414414
}
415415
}
416416

multiboot2/src/tag.rs

Lines changed: 11 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::{TagTrait, TagType, TagTypeId};
1313
use core::fmt::{Debug, Formatter};
1414
use core::mem;
1515
use core::ptr;
16-
use multiboot2_common::increase_to_alignment;
16+
use multiboot2_common::iter::TagBytesIter;
1717
use multiboot2_common::{BytesRef, DynSizedStructure, Header};
1818

1919
/// The common header that all tags have in common. This type is ABI compatible.
@@ -65,10 +65,8 @@ impl GenericTag {
6565
pub fn ref_from<'a>(bytes: &'a [u8]) -> &'a Self {
6666
let bytes = BytesRef::<TagHeader>::try_from(bytes).unwrap();
6767
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) }
7069
}
71-
7270
/// Returns the underlying [`TagHeader`].
7371
pub const fn header(&self) -> &TagHeader {
7472
self.0.header()
@@ -83,7 +81,7 @@ impl GenericTag {
8381
/// may be a ZST or DST typed tag.
8482
pub fn cast<T: TagTrait + ?Sized>(&self) -> &T {
8583
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());
8785
let t_ptr = ptr_meta::from_raw_parts(base_ptr.cast(), t_dst_size);
8886
let t_ref = unsafe { &*t_ptr };
8987
assert_eq!(mem::size_of_val(self), mem::size_of_val(t_ref));
@@ -94,7 +92,7 @@ impl GenericTag {
9492
impl Debug for GenericTag {
9593
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
9694
f.debug_struct("GenericTag")
97-
.field("header", &self.0.header())
95+
.field("header", self.0.header())
9896
.field("payload", &"<bytes>")
9997
.finish()
10098
}
@@ -109,65 +107,24 @@ impl TagTrait for GenericTag {
109107
}
110108
}
111109

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.
114112
#[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>);
121114

122115
impl<'a> TagIter<'a> {
123-
/// Creates a new iterator
116+
/// Creates a new iterator.
124117
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))
135119
}
136120
}
137121

138122
impl<'a> Iterator for TagIter<'a> {
139123
type Item = &'a GenericTag;
140124

141125
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()))
171128
}
172129
}
173130

0 commit comments

Comments
 (0)