Skip to content

Commit 9fbe6d6

Browse files
committed
multiboot2: add Tag::cast_tag to prevent code duplication
1 parent ec24c49 commit 9fbe6d6

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
@@ -218,7 +218,7 @@ impl BootInformation {
218218
/// Search for the Memory map tag.
219219
pub fn memory_map_tag(&self) -> Option<&MemoryMapTag> {
220220
self.get_tag(TagType::Mmap)
221-
.map(|tag| unsafe { &*(tag as *const Tag as *const MemoryMapTag) })
221+
.map(|tag| tag.cast_tag::<MemoryMapTag>())
222222
}
223223

224224
/// Get an iterator of all module tags.
@@ -229,13 +229,13 @@ impl BootInformation {
229229
/// Search for the BootLoader name tag.
230230
pub fn boot_loader_name_tag(&self) -> Option<&BootLoaderNameTag> {
231231
self.get_tag(TagType::BootLoaderName)
232-
.map(|tag| unsafe { &*(tag as *const Tag as *const BootLoaderNameTag) })
232+
.map(|tag| tag.cast_tag::<BootLoaderNameTag>())
233233
}
234234

235235
/// Search for the Command line tag.
236236
pub fn command_line_tag(&self) -> Option<&CommandLineTag> {
237237
self.get_tag(TagType::Cmdline)
238-
.map(|tag| unsafe { &*(tag as *const Tag as *const CommandLineTag) })
238+
.map(|tag| tag.cast_tag::<CommandLineTag>())
239239
}
240240

241241
/// Search for the VBE framebuffer tag.
@@ -247,25 +247,25 @@ impl BootInformation {
247247
/// Search for the EFI 32-bit SDT tag.
248248
pub fn efi_sdt_32_tag(&self) -> Option<&EFISdt32> {
249249
self.get_tag(TagType::Efi32)
250-
.map(|tag| unsafe { &*(tag as *const Tag as *const EFISdt32) })
250+
.map(|tag| tag.cast_tag::<EFISdt32>())
251251
}
252252

253253
/// Search for the EFI 64-bit SDT tag.
254254
pub fn efi_sdt_64_tag(&self) -> Option<&EFISdt64> {
255255
self.get_tag(TagType::Efi64)
256-
.map(|tag| unsafe { &*(tag as *const Tag as *const EFISdt64) })
256+
.map(|tag| tag.cast_tag::<EFISdt64>())
257257
}
258258

259259
/// Search for the (ACPI 1.0) RSDP tag.
260260
pub fn rsdp_v1_tag(&self) -> Option<&RsdpV1Tag> {
261261
self.get_tag(TagType::AcpiV1)
262-
.map(|tag| unsafe { &*(tag as *const Tag as *const RsdpV1Tag) })
262+
.map(|tag| tag.cast_tag::<RsdpV1Tag>())
263263
}
264264

265265
/// Search for the (ACPI 2.0 or later) RSDP tag.
266266
pub fn rsdp_v2_tag(&self) -> Option<&RsdpV2Tag> {
267267
self.get_tag(TagType::AcpiV2)
268-
.map(|tag| unsafe { &*(tag as *const Tag as *const RsdpV2Tag) })
268+
.map(|tag| tag.cast_tag::<RsdpV2Tag>())
269269
}
270270

271271
/// Search for the EFI Memory map tag.
@@ -276,32 +276,32 @@ impl BootInformation {
276276
Some(_tag) => None,
277277
None => self
278278
.get_tag(TagType::EfiMmap)
279-
.map(|tag| unsafe { &*(tag as *const Tag as *const EFIMemoryMapTag) }),
279+
.map(|tag| tag.cast_tag::<EFIMemoryMapTag>()),
280280
}
281281
}
282282

283283
/// Search for the EFI 32-bit image handle pointer.
284284
pub fn efi_32_ih(&self) -> Option<&EFIImageHandle32> {
285285
self.get_tag(TagType::Efi32Ih)
286-
.map(|tag| unsafe { &*(tag as *const Tag as *const EFIImageHandle32) })
286+
.map(|tag| tag.cast_tag::<EFIImageHandle32>())
287287
}
288288

289289
/// Search for the EFI 64-bit image handle pointer.
290290
pub fn efi_64_ih(&self) -> Option<&EFIImageHandle64> {
291291
self.get_tag(TagType::Efi64Ih)
292-
.map(|tag| unsafe { &*(tag as *const Tag as *const EFIImageHandle64) })
292+
.map(|tag| tag.cast_tag::<EFIImageHandle64>())
293293
}
294294

295295
/// Search for the Image Load Base Physical Address.
296296
pub fn load_base_addr(&self) -> Option<&ImageLoadPhysAddr> {
297297
self.get_tag(TagType::LoadBaseAddr)
298-
.map(|tag| unsafe { &*(tag as *const Tag as *const ImageLoadPhysAddr) })
298+
.map(|tag| tag.cast_tag::<ImageLoadPhysAddr>())
299299
}
300300

301301
/// Search for the VBE information tag.
302302
pub fn vbe_info_tag(&self) -> Option<&'static VBEInfoTag> {
303303
self.get_tag(TagType::Vbe)
304-
.map(|tag| unsafe { &*(tag as *const Tag as *const VBEInfoTag) })
304+
.map(|tag| tag.cast_tag::<VBEInfoTag>())
305305
}
306306

307307
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)