@@ -311,6 +311,8 @@ impl BootInformation {
311
311
/// Public getter to find any Multiboot tag by its type, including
312
312
/// specified and custom ones.
313
313
///
314
+ /// The parameter can be of type `u32`, [`TagType`], or [`TagTypeId`].
315
+ ///
314
316
/// # Specified or Custom Tags
315
317
/// The Multiboot2 specification specifies a list of tags, see [`TagType`].
316
318
/// However, it doesn't forbid to use custom tags. Because of this, there
@@ -334,16 +336,16 @@ impl BootInformation {
334
336
/// }
335
337
///
336
338
/// let tag = bi
337
- /// // this function is now public!
338
- /// .get_tag(0x1337.into())
339
+ /// .get_tag(0x1337)
339
340
/// .unwrap()
340
341
/// // type definition from end user; must be `Sized`!
341
342
/// .cast_tag::<CustomTag>();
342
343
/// let name = &tag.name as *const u8 as *const c_char;
343
344
/// let str = unsafe { CStr::from_ptr(name).to_str().unwrap() };
344
345
/// assert_eq!(str, "name");
345
346
/// ```
346
- pub fn get_tag ( & self , typ : TagType ) -> Option < & Tag > {
347
+ pub fn get_tag ( & self , typ : impl Into < TagTypeId > ) -> Option < & Tag > {
348
+ let typ = typ. into ( ) ;
347
349
self . tags ( ) . find ( |tag| tag. typ == typ)
348
350
}
349
351
@@ -1516,15 +1518,64 @@ mod tests {
1516
1518
name : u8 ,
1517
1519
}
1518
1520
1519
- let tag = bi
1520
- . get_tag ( CUSTOM_TAG_ID . into ( ) )
1521
- . unwrap ( )
1522
- . cast_tag :: < CustomTag > ( ) ;
1521
+ let tag = bi. get_tag ( CUSTOM_TAG_ID ) . unwrap ( ) . cast_tag :: < CustomTag > ( ) ;
1523
1522
1524
1523
// strlen without null byte
1525
1524
let strlen = tag. size as usize - mem:: size_of :: < CommandLineTag > ( ) ;
1526
1525
let bytes = unsafe { slice:: from_raw_parts ( ( & tag. name ) as * const u8 , strlen) } ;
1527
1526
let name = core:: str:: from_utf8 ( bytes) . unwrap ( ) ;
1528
1527
assert_eq ! ( name, "name" ) ;
1529
1528
}
1529
+
1530
+ /// Tests that `get_tag` can consume multiple types that implement `Into<TagTypeId>`
1531
+ #[ test]
1532
+ fn get_tag_into_variants ( ) {
1533
+ #[ repr( C , align( 8 ) ) ]
1534
+ struct Bytes ( [ u8 ; 32 ] ) ;
1535
+ let bytes: Bytes = Bytes ( [
1536
+ 32 ,
1537
+ 0 ,
1538
+ 0 ,
1539
+ 0 , // total_size
1540
+ 0 ,
1541
+ 0 ,
1542
+ 0 ,
1543
+ 0 , // reserved
1544
+ // my custom tag
1545
+ TagType :: Cmdline . val ( ) . to_ne_bytes ( ) [ 0 ] ,
1546
+ TagType :: Cmdline . val ( ) . to_ne_bytes ( ) [ 1 ] ,
1547
+ TagType :: Cmdline . val ( ) . to_ne_bytes ( ) [ 2 ] ,
1548
+ TagType :: Cmdline . val ( ) . to_ne_bytes ( ) [ 3 ] ,
1549
+ 13 ,
1550
+ 0 ,
1551
+ 0 ,
1552
+ 0 , // tag size
1553
+ 110 ,
1554
+ 97 ,
1555
+ 109 ,
1556
+ 101 , // ASCII string 'name'
1557
+ 0 ,
1558
+ 0 ,
1559
+ 0 ,
1560
+ 0 , // null byte + padding
1561
+ 0 ,
1562
+ 0 ,
1563
+ 0 ,
1564
+ 0 , // end tag type
1565
+ 8 ,
1566
+ 0 ,
1567
+ 0 ,
1568
+ 0 , // end tag size
1569
+ ] ) ;
1570
+
1571
+ let addr = bytes. 0 . as_ptr ( ) as usize ;
1572
+ let bi = unsafe { load ( addr) } ;
1573
+ let bi = bi. unwrap ( ) ;
1574
+
1575
+ let _tag = bi. get_tag ( TagType :: Cmdline ) . unwrap ( ) ;
1576
+
1577
+ let _tag = bi. get_tag ( 1 ) . unwrap ( ) ;
1578
+
1579
+ let _tag = bi. get_tag ( TagTypeId :: new ( 1 ) ) . unwrap ( ) ;
1580
+ }
1530
1581
}
0 commit comments