Skip to content

Commit 7b255de

Browse files
committed
Revert "multiboot2: simplify usage of cast_tag::<T>()"
This reverts commit cdfc1c7.
1 parent 2d9ee1f commit 7b255de

File tree

1 file changed

+41
-32
lines changed

1 file changed

+41
-32
lines changed

multiboot2/src/lib.rs

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -257,17 +257,14 @@ impl BootInformation {
257257

258258
/// Search for the ELF Sections tag.
259259
pub fn elf_sections_tag(&self) -> Option<&ElfSectionsTag> {
260-
self.get_tag::<Tag, _>(TagType::ElfSections)
261-
.map(|tag| unsafe {
262-
let tag = &*(tag as *const Tag as *const ElfSectionsTag);
263-
assert!((tag.entry_size * tag.shndx) as u32 <= tag.size);
264-
tag
265-
})
260+
self.get_tag(TagType::ElfSections)
261+
.map(|tag| unsafe { &*(tag as *const Tag as *const ElfSectionsTag) })
266262
}
267263

268264
/// Search for the Memory map tag.
269265
pub fn memory_map_tag(&self) -> Option<&MemoryMapTag> {
270-
self.get_tag::<MemoryMapTag, _>(TagType::Mmap)
266+
self.get_tag(TagType::Mmap)
267+
.map(|tag| tag.cast_tag::<MemoryMapTag>())
271268
}
272269

273270
/// Search for the Memory map tag, return a mutable reference.
@@ -283,18 +280,21 @@ impl BootInformation {
283280

284281
/// Search for the BootLoader name tag.
285282
pub fn boot_loader_name_tag(&self) -> Option<&BootLoaderNameTag> {
286-
self.get_tag::<BootLoaderNameTag, _>(TagType::BootLoaderName)
283+
self.get_tag(TagType::BootLoaderName)
284+
.map(|tag| tag.cast_tag::<BootLoaderNameTag>())
287285
}
288286

289287
/// Search for the Command line tag.
290288
pub fn command_line_tag(&self) -> Option<&CommandLineTag> {
291-
self.get_tag::<CommandLineTag, _>(TagType::Cmdline)
289+
self.get_tag(TagType::Cmdline)
290+
.map(|tag| tag.cast_tag::<CommandLineTag>())
292291
}
293292

294293
/// Search for the VBE framebuffer tag. The result is `Some(Err(e))`, if the
295294
/// framebuffer type is unknown, while the framebuffer tag is present.
296295
pub fn framebuffer_tag(&self) -> Option<Result<&FramebufferTag, UnknownFramebufferType>> {
297-
self.get_tag::<FramebufferTag, _>(TagType::Framebuffer)
296+
self.get_tag(TagType::Framebuffer)
297+
.map(|tag| tag.cast_tag::<FramebufferTag>())
298298
.map(|tag| match tag.buffer_type() {
299299
Ok(_) => Ok(tag),
300300
Err(e) => Err(e),
@@ -303,22 +303,26 @@ impl BootInformation {
303303

304304
/// Search for the EFI 32-bit SDT tag.
305305
pub fn efi_sdt_32_tag(&self) -> Option<&EFISdt32> {
306-
self.get_tag::<EFISdt32, _>(TagType::Efi32)
306+
self.get_tag(TagType::Efi32)
307+
.map(|tag| tag.cast_tag::<EFISdt32>())
307308
}
308309

309310
/// Search for the EFI 64-bit SDT tag.
310311
pub fn efi_sdt_64_tag(&self) -> Option<&EFISdt64> {
311-
self.get_tag::<EFISdt64, _>(TagType::Efi64)
312+
self.get_tag(TagType::Efi64)
313+
.map(|tag| tag.cast_tag::<EFISdt64>())
312314
}
313315

314316
/// Search for the (ACPI 1.0) RSDP tag.
315317
pub fn rsdp_v1_tag(&self) -> Option<&RsdpV1Tag> {
316-
self.get_tag::<RsdpV1Tag, _>(TagType::AcpiV1)
318+
self.get_tag(TagType::AcpiV1)
319+
.map(|tag| tag.cast_tag::<RsdpV1Tag>())
317320
}
318321

319322
/// Search for the (ACPI 2.0 or later) RSDP tag.
320323
pub fn rsdp_v2_tag(&self) -> Option<&RsdpV2Tag> {
321-
self.get_tag::<RsdpV2Tag, _>(TagType::AcpiV2)
324+
self.get_tag(TagType::AcpiV2)
325+
.map(|tag| tag.cast_tag::<RsdpV2Tag>())
322326
}
323327

324328
/// Search for the EFI Memory map tag, if the boot services were exited.
@@ -328,9 +332,11 @@ impl BootInformation {
328332
pub fn efi_memory_map_tag(&self) -> Option<&EFIMemoryMapTag> {
329333
// If the EFIBootServicesNotExited is present, then we should not use
330334
// the memory map, as it could still be in use.
331-
match self.get_tag::<Tag, _>(TagType::EfiBs) {
335+
match self.get_tag(TagType::EfiBs) {
332336
Some(_tag) => None,
333-
None => self.get_tag::<EFIMemoryMapTag, _>(TagType::EfiMmap),
337+
None => self
338+
.get_tag(TagType::EfiMmap)
339+
.map(|tag| tag.cast_tag::<EFIMemoryMapTag>()),
334340
}
335341
}
336342

@@ -342,22 +348,26 @@ impl BootInformation {
342348

343349
/// Search for the EFI 32-bit image handle pointer.
344350
pub fn efi_32_ih(&self) -> Option<&EFIImageHandle32> {
345-
self.get_tag::<EFIImageHandle32, _>(TagType::Efi32Ih)
351+
self.get_tag(TagType::Efi32Ih)
352+
.map(|tag| tag.cast_tag::<EFIImageHandle32>())
346353
}
347354

348355
/// Search for the EFI 64-bit image handle pointer.
349356
pub fn efi_64_ih(&self) -> Option<&EFIImageHandle64> {
350-
self.get_tag::<EFIImageHandle64, _>(TagType::Efi64Ih)
357+
self.get_tag(TagType::Efi64Ih)
358+
.map(|tag| tag.cast_tag::<EFIImageHandle64>())
351359
}
352360

353361
/// Search for the Image Load Base Physical Address.
354362
pub fn load_base_addr(&self) -> Option<&ImageLoadPhysAddr> {
355-
self.get_tag::<ImageLoadPhysAddr, _>(TagType::LoadBaseAddr)
363+
self.get_tag(TagType::LoadBaseAddr)
364+
.map(|tag| tag.cast_tag::<ImageLoadPhysAddr>())
356365
}
357366

358367
/// Search for the VBE information tag.
359368
pub fn vbe_info_tag(&self) -> Option<&VBEInfoTag> {
360-
self.get_tag::<VBEInfoTag, _>(TagType::Vbe)
369+
self.get_tag(TagType::Vbe)
370+
.map(|tag| tag.cast_tag::<VBEInfoTag>())
361371
}
362372

363373
fn get(&self) -> &BootInformationInner {
@@ -381,8 +391,9 @@ impl BootInformation {
381391
/// the MBI. Custom tags must be `Sized`. Hence, they are not allowed to contain a field such
382392
/// as `name: [u8]`.
383393
///
394+
///
384395
/// **Belows example needs Rust 1.64 or newer because of std::ffi imports!**
385-
/// ```ignore
396+
/// ```rust,no_run
386397
/// use std::ffi::{c_char, CStr};
387398
/// use multiboot2::TagTypeId;
388399
///
@@ -398,18 +409,17 @@ impl BootInformation {
398409
/// let mbi = unsafe { multiboot2::load(0xdeadbeef).unwrap() };
399410
///
400411
/// let tag = mbi
412+
/// .get_tag(0x1337)
413+
/// .unwrap()
401414
/// // type definition from end user; must be `Sized`!
402-
/// .get_tag::<CustomTag, _>(0x1337)
403-
/// .unwrap();
415+
/// .cast_tag::<CustomTag>();
404416
/// let name = &tag.name as *const u8 as *const c_char;
405417
/// let str = unsafe { CStr::from_ptr(name).to_str().unwrap() };
406418
/// assert_eq!(str, "name");
407419
/// ```
408-
pub fn get_tag<Tag: ?Sized, TagType: Into<TagTypeId>>(&self, typ: TagType) -> Option<&Tag> {
420+
pub fn get_tag(&self, typ: impl Into<TagTypeId>) -> Option<&Tag> {
409421
let typ = typ.into();
410-
self.tags()
411-
.find(|tag| tag.typ == typ)
412-
.map(|tag| tag.cast_tag::<Tag>())
422+
self.tags().find(|tag| tag.typ == typ)
413423
}
414424

415425
fn get_tag_mut<Tag: ?Sized, TagType: Into<TagTypeId>>(
@@ -1523,7 +1533,6 @@ mod tests {
15231533
consumer(MbiLoadError::IllegalAddress)
15241534
}
15251535

1526-
#[test]
15271536
fn custom_tag() {
15281537
const CUSTOM_TAG_ID: u32 = 0x1337;
15291538

@@ -1578,7 +1587,7 @@ mod tests {
15781587
name: u8,
15791588
}
15801589

1581-
let tag = bi.get_tag::<CustomTag, _>(CUSTOM_TAG_ID).unwrap();
1590+
let tag = bi.get_tag(CUSTOM_TAG_ID).unwrap().cast_tag::<CustomTag>();
15821591

15831592
// strlen without null byte
15841593
let strlen = tag.size as usize - command_line::METADATA_SIZE;
@@ -1631,10 +1640,10 @@ mod tests {
16311640
let bi = unsafe { load(addr) };
16321641
let bi = bi.unwrap();
16331642

1634-
let _tag = bi.get_tag::<CommandLineTag, _>(TagType::Cmdline).unwrap();
1643+
let _tag = bi.get_tag(TagType::Cmdline).unwrap();
16351644

1636-
let _tag = bi.get_tag::<CommandLineTag, _>(1).unwrap();
1645+
let _tag = bi.get_tag(1).unwrap();
16371646

1638-
let _tag = bi.get_tag::<CommandLineTag, _>(TagTypeId::new(1)).unwrap();
1647+
let _tag = bi.get_tag(TagTypeId::new(1)).unwrap();
16391648
}
16401649
}

0 commit comments

Comments
 (0)