Skip to content

Commit 800567f

Browse files
committed
multiboot2: Replace ptr_metadata feature with slice_from_raw_parts
The former one would probably be cleaner, but it's not yet stable.
1 parent 26d62bc commit 800567f

File tree

6 files changed

+25
-18
lines changed

6 files changed

+25
-18
lines changed

multiboot2/src/boot_loader_name.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ impl StructAsBytes for BootLoaderNameTag {
7272

7373
#[cfg(test)]
7474
mod tests {
75+
use core::ptr::slice_from_raw_parts;
76+
7577
use crate::{TagType, boot_loader_name::METADATA_SIZE};
7678

7779
const MSG: &str = "hello";
@@ -98,8 +100,8 @@ mod tests {
98100
fn test_parse_str() {
99101
let tag = get_bytes();
100102
let tag = unsafe {
101-
let (ptr, _) = tag.as_ptr().to_raw_parts();
102-
(core::ptr::from_raw_parts(
103+
let ptr = tag.as_ptr() as *const ();
104+
(slice_from_raw_parts(
103105
ptr, tag.len() - METADATA_SIZE
104106
) as *const super::BootLoaderNameTag)
105107
.as_ref()

multiboot2/src/builder/mod.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ pub use information::Multiboot2InformationBuilder;
99
use core::alloc::Layout;
1010
use core::convert::TryInto;
1111
use core::mem::size_of;
12+
use core::ptr::slice_from_raw_parts_mut;
1213
use alloc::alloc::alloc;
1314
use alloc::boxed::Box;
1415

@@ -36,9 +37,9 @@ pub(super) fn boxed_dst_tag(typ: TagType, content: &[u8]) -> Box<Tag> {
3637
content_ptr.add(idx).write(*val);
3738
}
3839
Box::from_raw(
39-
core::ptr::from_raw_parts_mut(
40-
ptr as *mut (), content.len()
41-
)
40+
slice_from_raw_parts_mut(
41+
ptr as *mut usize, content.len()
42+
) as *mut Tag
4243
)
4344
}
4445
}

multiboot2/src/command_line.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ impl StructAsBytes for CommandLineTag {
7070

7171
#[cfg(test)]
7272
mod tests {
73+
use core::ptr::slice_from_raw_parts;
74+
7375
use crate::{TagType, command_line::METADATA_SIZE};
7476

7577
const MSG: &str = "hello";
@@ -96,8 +98,8 @@ mod tests {
9698
fn test_parse_str() {
9799
let tag = get_bytes();
98100
let tag = unsafe {
99-
let (ptr, _) = tag.as_ptr().to_raw_parts();
100-
(core::ptr::from_raw_parts(
101+
let ptr = tag.as_ptr() as *const ();
102+
(slice_from_raw_parts(
101103
ptr, tag.len() - METADATA_SIZE
102104
) as *const super::CommandLineTag)
103105
.as_ref()

multiboot2/src/lib.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@
3434
//! ## MSRV
3535
//! The MSRV is 1.52.1 stable.
3636
37-
#![feature(ptr_metadata)]
38-
3937
#[cfg(feature = "builder")]
4038
extern crate alloc;
4139

@@ -44,7 +42,7 @@ extern crate alloc;
4442
#[cfg(test)]
4543
extern crate std;
4644

47-
use core::{fmt, convert::TryInto};
45+
use core::{fmt, convert::TryInto, ptr::slice_from_raw_parts};
4846

4947
pub use boot_loader_name::BootLoaderNameTag;
5048
#[cfg(feature = "builder")]
@@ -351,7 +349,7 @@ impl BootInformation {
351349
(next_ptr as *const u32).add(1).read()
352350
}.try_into().unwrap();
353351
TagIter::new(
354-
core::ptr::from_raw_parts(next_ptr, size - METADATA_SIZE)
352+
slice_from_raw_parts(next_ptr, size - METADATA_SIZE) as *mut Tag
355353
)
356354
}
357355
}

multiboot2/src/module.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use crate::builder::traits::StructAsBytes;
77
use core::convert::TryInto;
88
use core::fmt::{Debug, Formatter};
99
use core::mem;
10+
use core::ptr::slice_from_raw_parts;
1011
use core::str::Utf8Error;
1112

1213
#[cfg(feature = "builder")]
@@ -113,8 +114,8 @@ impl<'a> Iterator for ModuleIter<'a> {
113114
self.iter
114115
.find(|x| x.typ == TagType::Module)
115116
.map(|tag| unsafe {
116-
let (ptr, _) = (tag as *const Tag).to_raw_parts();
117-
&*(core::ptr::from_raw_parts(
117+
let ptr = tag as *const Tag as *const ();
118+
&*(slice_from_raw_parts(
118119
ptr, tag.size as usize - METADATA_SIZE,
119120
) as *const ModuleTag)
120121
})
@@ -133,6 +134,8 @@ impl<'a> Debug for ModuleIter<'a> {
133134

134135
#[cfg(test)]
135136
mod tests {
137+
use core::ptr::slice_from_raw_parts;
138+
136139
use crate::{TagType, module::METADATA_SIZE};
137140

138141
const MSG: &str = "hello";
@@ -162,9 +165,9 @@ mod tests {
162165
fn test_parse_str() {
163166
let tag = get_bytes();
164167
let tag = unsafe {
165-
let (ptr, _) = tag.as_ptr().to_raw_parts();
168+
let ptr = tag.as_ptr() as *const ();
166169
&*(
167-
core::ptr::from_raw_parts(ptr, tag.len() - METADATA_SIZE)
170+
slice_from_raw_parts(ptr, tag.len() - METADATA_SIZE)
168171
as *const super::ModuleTag
169172
)
170173
};

multiboot2/src/tag_type.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use core::fmt::{Debug, Formatter};
77
use core::hash::Hash;
88
use core::marker::PhantomData;
99
use core::mem::size_of;
10+
use core::ptr::slice_from_raw_parts;
1011

1112
/// Possible types of a Tag in the Multiboot2 Information Structure (MBI), therefore the value
1213
/// of the the `typ` property. The names and values are taken from the example C code
@@ -179,15 +180,15 @@ impl<'a> Iterator for TagIter<'a> {
179180
} => None, // end tag
180181
tag => {
181182
// go to next tag
182-
let (tag_addr, _) = self.current.to_raw_parts();
183+
let tag_addr = self.current as *const ();
183184
let mut tag_addr = tag_addr as usize;
184185
tag_addr += ((tag.size + 7) & !7) as usize; //align at 8 byte
185186
let size: usize = unsafe {
186187
(tag_addr as *const u32).add(1).read()
187188
}.try_into().unwrap();
188-
self.current = core::ptr::from_raw_parts(
189+
self.current = slice_from_raw_parts(
189190
tag_addr as *const (), size - METADATA_SIZE,
190-
);
191+
) as *const Tag;
191192

192193
Some(tag)
193194
}

0 commit comments

Comments
 (0)