@@ -257,17 +257,14 @@ impl BootInformation {
257
257
258
258
/// Search for the ELF Sections tag.
259
259
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 ) } )
266
262
}
267
263
268
264
/// Search for the Memory map tag.
269
265
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 > ( ) )
271
268
}
272
269
273
270
/// Search for the Memory map tag, return a mutable reference.
@@ -283,18 +280,21 @@ impl BootInformation {
283
280
284
281
/// Search for the BootLoader name tag.
285
282
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 > ( ) )
287
285
}
288
286
289
287
/// Search for the Command line tag.
290
288
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 > ( ) )
292
291
}
293
292
294
293
/// Search for the VBE framebuffer tag. The result is `Some(Err(e))`, if the
295
294
/// framebuffer type is unknown, while the framebuffer tag is present.
296
295
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 > ( ) )
298
298
. map ( |tag| match tag. buffer_type ( ) {
299
299
Ok ( _) => Ok ( tag) ,
300
300
Err ( e) => Err ( e) ,
@@ -303,22 +303,26 @@ impl BootInformation {
303
303
304
304
/// Search for the EFI 32-bit SDT tag.
305
305
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 > ( ) )
307
308
}
308
309
309
310
/// Search for the EFI 64-bit SDT tag.
310
311
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 > ( ) )
312
314
}
313
315
314
316
/// Search for the (ACPI 1.0) RSDP tag.
315
317
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 > ( ) )
317
320
}
318
321
319
322
/// Search for the (ACPI 2.0 or later) RSDP tag.
320
323
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 > ( ) )
322
326
}
323
327
324
328
/// Search for the EFI Memory map tag, if the boot services were exited.
@@ -328,9 +332,11 @@ impl BootInformation {
328
332
pub fn efi_memory_map_tag ( & self ) -> Option < & EFIMemoryMapTag > {
329
333
// If the EFIBootServicesNotExited is present, then we should not use
330
334
// 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 ) {
332
336
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 > ( ) ) ,
334
340
}
335
341
}
336
342
@@ -342,22 +348,26 @@ impl BootInformation {
342
348
343
349
/// Search for the EFI 32-bit image handle pointer.
344
350
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 > ( ) )
346
353
}
347
354
348
355
/// Search for the EFI 64-bit image handle pointer.
349
356
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 > ( ) )
351
359
}
352
360
353
361
/// Search for the Image Load Base Physical Address.
354
362
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 > ( ) )
356
365
}
357
366
358
367
/// Search for the VBE information tag.
359
368
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 > ( ) )
361
371
}
362
372
363
373
fn get ( & self ) -> & BootInformationInner {
@@ -381,8 +391,9 @@ impl BootInformation {
381
391
/// the MBI. Custom tags must be `Sized`. Hence, they are not allowed to contain a field such
382
392
/// as `name: [u8]`.
383
393
///
394
+ ///
384
395
/// **Belows example needs Rust 1.64 or newer because of std::ffi imports!**
385
- /// ```ignore
396
+ /// ```rust,no_run
386
397
/// use std::ffi::{c_char, CStr};
387
398
/// use multiboot2::TagTypeId;
388
399
///
@@ -398,18 +409,17 @@ impl BootInformation {
398
409
/// let mbi = unsafe { multiboot2::load(0xdeadbeef).unwrap() };
399
410
///
400
411
/// let tag = mbi
412
+ /// .get_tag(0x1337)
413
+ /// .unwrap()
401
414
/// // type definition from end user; must be `Sized`!
402
- /// .get_tag::<CustomTag, _>(0x1337)
403
- /// .unwrap();
415
+ /// .cast_tag::<CustomTag>();
404
416
/// let name = &tag.name as *const u8 as *const c_char;
405
417
/// let str = unsafe { CStr::from_ptr(name).to_str().unwrap() };
406
418
/// assert_eq!(str, "name");
407
419
/// ```
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 > {
409
421
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)
413
423
}
414
424
415
425
fn get_tag_mut < Tag : ?Sized , TagType : Into < TagTypeId > > (
@@ -1523,7 +1533,6 @@ mod tests {
1523
1533
consumer ( MbiLoadError :: IllegalAddress )
1524
1534
}
1525
1535
1526
- #[ test]
1527
1536
fn custom_tag ( ) {
1528
1537
const CUSTOM_TAG_ID : u32 = 0x1337 ;
1529
1538
@@ -1578,7 +1587,7 @@ mod tests {
1578
1587
name : u8 ,
1579
1588
}
1580
1589
1581
- let tag = bi. get_tag :: < CustomTag , _ > ( CUSTOM_TAG_ID ) . unwrap ( ) ;
1590
+ let tag = bi. get_tag ( CUSTOM_TAG_ID ) . unwrap ( ) . cast_tag :: < CustomTag > ( ) ;
1582
1591
1583
1592
// strlen without null byte
1584
1593
let strlen = tag. size as usize - command_line:: METADATA_SIZE ;
@@ -1631,10 +1640,10 @@ mod tests {
1631
1640
let bi = unsafe { load ( addr) } ;
1632
1641
let bi = bi. unwrap ( ) ;
1633
1642
1634
- let _tag = bi. get_tag :: < CommandLineTag , _ > ( TagType :: Cmdline ) . unwrap ( ) ;
1643
+ let _tag = bi. get_tag ( TagType :: Cmdline ) . unwrap ( ) ;
1635
1644
1636
- let _tag = bi. get_tag :: < CommandLineTag , _ > ( 1 ) . unwrap ( ) ;
1645
+ let _tag = bi. get_tag ( 1 ) . unwrap ( ) ;
1637
1646
1638
- let _tag = bi. get_tag :: < CommandLineTag , _ > ( TagTypeId :: new ( 1 ) ) . unwrap ( ) ;
1647
+ let _tag = bi. get_tag ( TagTypeId :: new ( 1 ) ) . unwrap ( ) ;
1639
1648
}
1640
1649
}
0 commit comments