Skip to content

Commit bd73c24

Browse files
committed
multiboot2: add Tag::cast_tag to prevent code duplication
1 parent b09c7df commit bd73c24

File tree

2 files changed

+19
-12
lines changed

2 files changed

+19
-12
lines changed

multiboot2/src/lib.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ impl BootInformation {
225225
/// Search for the Memory map tag.
226226
pub fn memory_map_tag(&self) -> Option<&MemoryMapTag> {
227227
self.get_tag(TagType::Mmap)
228-
.map(|tag| unsafe { &*(tag as *const Tag as *const MemoryMapTag) })
228+
.map(|tag| tag.cast_tag::<MemoryMapTag>())
229229
}
230230

231231
/// Get an iterator of all module tags.
@@ -236,13 +236,13 @@ impl BootInformation {
236236
/// Search for the BootLoader name tag.
237237
pub fn boot_loader_name_tag(&self) -> Option<&BootLoaderNameTag> {
238238
self.get_tag(TagType::BootLoaderName)
239-
.map(|tag| unsafe { &*(tag as *const Tag as *const BootLoaderNameTag) })
239+
.map(|tag| tag.cast_tag::<BootLoaderNameTag>())
240240
}
241241

242242
/// Search for the Command line tag.
243243
pub fn command_line_tag(&self) -> Option<&CommandLineTag> {
244244
self.get_tag(TagType::Cmdline)
245-
.map(|tag| unsafe { &*(tag as *const Tag as *const CommandLineTag) })
245+
.map(|tag| tag.cast_tag::<CommandLineTag>())
246246
}
247247

248248
/// Search for the VBE framebuffer tag. The result is `Some(Err(e))`, if the
@@ -255,25 +255,25 @@ impl BootInformation {
255255
/// Search for the EFI 32-bit SDT tag.
256256
pub fn efi_sdt_32_tag(&self) -> Option<&EFISdt32> {
257257
self.get_tag(TagType::Efi32)
258-
.map(|tag| unsafe { &*(tag as *const Tag as *const EFISdt32) })
258+
.map(|tag| tag.cast_tag::<EFISdt32>())
259259
}
260260

261261
/// Search for the EFI 64-bit SDT tag.
262262
pub fn efi_sdt_64_tag(&self) -> Option<&EFISdt64> {
263263
self.get_tag(TagType::Efi64)
264-
.map(|tag| unsafe { &*(tag as *const Tag as *const EFISdt64) })
264+
.map(|tag| tag.cast_tag::<EFISdt64>())
265265
}
266266

267267
/// Search for the (ACPI 1.0) RSDP tag.
268268
pub fn rsdp_v1_tag(&self) -> Option<&RsdpV1Tag> {
269269
self.get_tag(TagType::AcpiV1)
270-
.map(|tag| unsafe { &*(tag as *const Tag as *const RsdpV1Tag) })
270+
.map(|tag| tag.cast_tag::<RsdpV1Tag>())
271271
}
272272

273273
/// Search for the (ACPI 2.0 or later) RSDP tag.
274274
pub fn rsdp_v2_tag(&self) -> Option<&RsdpV2Tag> {
275275
self.get_tag(TagType::AcpiV2)
276-
.map(|tag| unsafe { &*(tag as *const Tag as *const RsdpV2Tag) })
276+
.map(|tag| tag.cast_tag::<RsdpV2Tag>())
277277
}
278278

279279
/// Search for the EFI Memory map tag, if the boot services were exited.
@@ -287,32 +287,32 @@ impl BootInformation {
287287
Some(_tag) => None,
288288
None => self
289289
.get_tag(TagType::EfiMmap)
290-
.map(|tag| unsafe { &*(tag as *const Tag as *const EFIMemoryMapTag) }),
290+
.map(|tag| tag.cast_tag::<EFIMemoryMapTag>()),
291291
}
292292
}
293293

294294
/// Search for the EFI 32-bit image handle pointer.
295295
pub fn efi_32_ih(&self) -> Option<&EFIImageHandle32> {
296296
self.get_tag(TagType::Efi32Ih)
297-
.map(|tag| unsafe { &*(tag as *const Tag as *const EFIImageHandle32) })
297+
.map(|tag| tag.cast_tag::<EFIImageHandle32>())
298298
}
299299

300300
/// Search for the EFI 64-bit image handle pointer.
301301
pub fn efi_64_ih(&self) -> Option<&EFIImageHandle64> {
302302
self.get_tag(TagType::Efi64Ih)
303-
.map(|tag| unsafe { &*(tag as *const Tag as *const EFIImageHandle64) })
303+
.map(|tag| tag.cast_tag::<EFIImageHandle64>())
304304
}
305305

306306
/// Search for the Image Load Base Physical Address.
307307
pub fn load_base_addr(&self) -> Option<&ImageLoadPhysAddr> {
308308
self.get_tag(TagType::LoadBaseAddr)
309-
.map(|tag| unsafe { &*(tag as *const Tag as *const ImageLoadPhysAddr) })
309+
.map(|tag| tag.cast_tag::<ImageLoadPhysAddr>())
310310
}
311311

312312
/// Search for the VBE information tag.
313313
pub fn vbe_info_tag(&self) -> Option<&VBEInfoTag> {
314314
self.get_tag(TagType::Vbe)
315-
.map(|tag| unsafe { &*(tag as *const Tag as *const VBEInfoTag) })
315+
.map(|tag| tag.cast_tag::<VBEInfoTag>())
316316
}
317317

318318
fn get(&self) -> &BootInformationInner {

multiboot2/src/tag_type.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,13 @@ pub struct Tag {
114114
// tag specific fields
115115
}
116116

117+
impl Tag {
118+
/// Casts the base tag to the specific tag type.
119+
pub fn cast_tag<'a, T>(&self) -> &'a T {
120+
unsafe { &*(self as *const Tag as *const T) }
121+
}
122+
}
123+
117124
impl Debug for Tag {
118125
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
119126
f.debug_struct("Tag")

0 commit comments

Comments
 (0)