Skip to content

Commit cba6123

Browse files
committed
Revert "multiboot2: add Tag::cast_tag to prevent code duplication"
This reverts commit bd73c24.
1 parent 7b255de commit cba6123

File tree

2 files changed

+21
-36
lines changed

2 files changed

+21
-36
lines changed

multiboot2/src/lib.rs

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ impl BootInformation {
264264
/// Search for the Memory map tag.
265265
pub fn memory_map_tag(&self) -> Option<&MemoryMapTag> {
266266
self.get_tag(TagType::Mmap)
267-
.map(|tag| tag.cast_tag::<MemoryMapTag>())
267+
.map(|tag| unsafe { &*(tag as *const Tag as *const MemoryMapTag) })
268268
}
269269

270270
/// Search for the Memory map tag, return a mutable reference.
@@ -281,20 +281,20 @@ impl BootInformation {
281281
/// Search for the BootLoader name tag.
282282
pub fn boot_loader_name_tag(&self) -> Option<&BootLoaderNameTag> {
283283
self.get_tag(TagType::BootLoaderName)
284-
.map(|tag| tag.cast_tag::<BootLoaderNameTag>())
284+
.map(|tag| unsafe { &*(tag as *const Tag as *const BootLoaderNameTag) })
285285
}
286286

287287
/// Search for the Command line tag.
288288
pub fn command_line_tag(&self) -> Option<&CommandLineTag> {
289289
self.get_tag(TagType::Cmdline)
290-
.map(|tag| tag.cast_tag::<CommandLineTag>())
290+
.map(|tag| unsafe { &*(tag as *const Tag as *const CommandLineTag) })
291291
}
292292

293293
/// Search for the VBE framebuffer tag. The result is `Some(Err(e))`, if the
294294
/// framebuffer type is unknown, while the framebuffer tag is present.
295295
pub fn framebuffer_tag(&self) -> Option<Result<&FramebufferTag, UnknownFramebufferType>> {
296296
self.get_tag(TagType::Framebuffer)
297-
.map(|tag| tag.cast_tag::<FramebufferTag>())
297+
.map(|tag| unsafe { &*(tag as *const Tag as *const FramebufferTag) })
298298
.map(|tag| match tag.buffer_type() {
299299
Ok(_) => Ok(tag),
300300
Err(e) => Err(e),
@@ -304,25 +304,25 @@ impl BootInformation {
304304
/// Search for the EFI 32-bit SDT tag.
305305
pub fn efi_sdt_32_tag(&self) -> Option<&EFISdt32> {
306306
self.get_tag(TagType::Efi32)
307-
.map(|tag| tag.cast_tag::<EFISdt32>())
307+
.map(|tag| unsafe { &*(tag as *const Tag as *const EFISdt32) })
308308
}
309309

310310
/// Search for the EFI 64-bit SDT tag.
311311
pub fn efi_sdt_64_tag(&self) -> Option<&EFISdt64> {
312312
self.get_tag(TagType::Efi64)
313-
.map(|tag| tag.cast_tag::<EFISdt64>())
313+
.map(|tag| unsafe { &*(tag as *const Tag as *const EFISdt64) })
314314
}
315315

316316
/// Search for the (ACPI 1.0) RSDP tag.
317317
pub fn rsdp_v1_tag(&self) -> Option<&RsdpV1Tag> {
318318
self.get_tag(TagType::AcpiV1)
319-
.map(|tag| tag.cast_tag::<RsdpV1Tag>())
319+
.map(|tag| unsafe { &*(tag as *const Tag as *const RsdpV1Tag) })
320320
}
321321

322322
/// Search for the (ACPI 2.0 or later) RSDP tag.
323323
pub fn rsdp_v2_tag(&self) -> Option<&RsdpV2Tag> {
324324
self.get_tag(TagType::AcpiV2)
325-
.map(|tag| tag.cast_tag::<RsdpV2Tag>())
325+
.map(|tag| unsafe { &*(tag as *const Tag as *const RsdpV2Tag) })
326326
}
327327

328328
/// Search for the EFI Memory map tag, if the boot services were exited.
@@ -336,7 +336,7 @@ impl BootInformation {
336336
Some(_tag) => None,
337337
None => self
338338
.get_tag(TagType::EfiMmap)
339-
.map(|tag| tag.cast_tag::<EFIMemoryMapTag>()),
339+
.map(|tag| unsafe { &*(tag as *const Tag as *const EFIMemoryMapTag) }),
340340
}
341341
}
342342

@@ -349,25 +349,25 @@ impl BootInformation {
349349
/// Search for the EFI 32-bit image handle pointer.
350350
pub fn efi_32_ih(&self) -> Option<&EFIImageHandle32> {
351351
self.get_tag(TagType::Efi32Ih)
352-
.map(|tag| tag.cast_tag::<EFIImageHandle32>())
352+
.map(|tag| unsafe { &*(tag as *const Tag as *const EFIImageHandle32) })
353353
}
354354

355355
/// Search for the EFI 64-bit image handle pointer.
356356
pub fn efi_64_ih(&self) -> Option<&EFIImageHandle64> {
357357
self.get_tag(TagType::Efi64Ih)
358-
.map(|tag| tag.cast_tag::<EFIImageHandle64>())
358+
.map(|tag| unsafe { &*(tag as *const Tag as *const EFIImageHandle64) })
359359
}
360360

361361
/// Search for the Image Load Base Physical Address.
362362
pub fn load_base_addr(&self) -> Option<&ImageLoadPhysAddr> {
363363
self.get_tag(TagType::LoadBaseAddr)
364-
.map(|tag| tag.cast_tag::<ImageLoadPhysAddr>())
364+
.map(|tag| unsafe { &*(tag as *const Tag as *const ImageLoadPhysAddr) })
365365
}
366366

367367
/// Search for the VBE information tag.
368368
pub fn vbe_info_tag(&self) -> Option<&VBEInfoTag> {
369369
self.get_tag(TagType::Vbe)
370-
.map(|tag| tag.cast_tag::<VBEInfoTag>())
370+
.map(|tag| unsafe { &*(tag as *const Tag as *const VBEInfoTag) })
371371
}
372372

373373
fn get(&self) -> &BootInformationInner {
@@ -395,7 +395,7 @@ impl BootInformation {
395395
/// **Belows example needs Rust 1.64 or newer because of std::ffi imports!**
396396
/// ```rust,no_run
397397
/// use std::ffi::{c_char, CStr};
398-
/// use multiboot2::TagTypeId;
398+
/// use multiboot2::{Tag, TagTypeId};
399399
///
400400
/// #[repr(C, align(8))]
401401
/// struct CustomTag {
@@ -410,9 +410,9 @@ impl BootInformation {
410410
///
411411
/// let tag = mbi
412412
/// .get_tag(0x1337)
413-
/// .unwrap()
414-
/// // type definition from end user; must be `Sized`!
415-
/// .cast_tag::<CustomTag>();
413+
/// .unwrap();
414+
/// // type definition from end user; must be `Sized`!
415+
/// let tag = unsafe { &*(tag as *const Tag as *const CustomTag) };
416416
/// let name = &tag.name as *const u8 as *const c_char;
417417
/// let str = unsafe { CStr::from_ptr(name).to_str().unwrap() };
418418
/// assert_eq!(str, "name");
@@ -422,14 +422,9 @@ impl BootInformation {
422422
self.tags().find(|tag| tag.typ == typ)
423423
}
424424

425-
fn get_tag_mut<Tag: ?Sized, TagType: Into<TagTypeId>>(
426-
&mut self,
427-
typ: TagType,
428-
) -> Option<&mut Tag> {
425+
fn get_tag_mut(&mut self, typ: impl Into<TagTypeId>) -> Option<&mut Tag> {
429426
let typ = typ.into();
430-
self.tags_mut()
431-
.find(|tag| tag.typ == typ)
432-
.map(|tag| tag.cast_tag_mut::<Tag>())
427+
self.tags_mut().find(|tag| tag.typ == typ)
433428
}
434429

435430
fn tags(&self) -> TagIter {
@@ -1587,7 +1582,8 @@ mod tests {
15871582
name: u8,
15881583
}
15891584

1590-
let tag = bi.get_tag(CUSTOM_TAG_ID).unwrap().cast_tag::<CustomTag>();
1585+
let tag = bi.get_tag(CUSTOM_TAG_ID).unwrap();
1586+
let tag = unsafe { &*(tag as *const Tag as *const CustomTag) };
15911587

15921588
// strlen without null byte
15931589
let strlen = tag.size as usize - command_line::METADATA_SIZE;

multiboot2/src/tag_type.rs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -301,17 +301,6 @@ pub struct Tag {
301301
content: [u8],
302302
}
303303

304-
impl Tag {
305-
/// Casts the base tag to the specific tag type.
306-
pub fn cast_tag<'a, T: ?Sized>(&self) -> &'a T {
307-
unsafe { &*(self as *const Tag as *const T) }
308-
}
309-
310-
pub(crate) fn cast_tag_mut<'a, T: ?Sized>(&mut self) -> &'a mut T {
311-
unsafe { &mut *(self as *mut Tag as *mut T) }
312-
}
313-
}
314-
315304
impl Debug for Tag {
316305
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
317306
let tag_type = TagType::from(self.typ);

0 commit comments

Comments
 (0)