Skip to content

Commit f3a8d9b

Browse files
committed
Use stablilized addr_of macro
Since Rust 1.51.0, support for macro addr_of!() has been stabilized[1], and this provides a way to get a raw pointer without potential UB in some cases. Memoffset alreadly uses the feature at the pre-stablilized stage (the macro was named as raw_const then). Therefore, switch to use the stablilized version (and name) and remove the out-dated version, also remove the related feature gate. [1]: rust-lang/rust#72279 Signed-off-by: Boqun Feng <boqun.feng@gmail.com>
1 parent 9111400 commit f3a8d9b

File tree

4 files changed

+4
-27
lines changed

4 files changed

+4
-27
lines changed

Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,3 @@ doc-comment = "0.3"
1818
[features]
1919
default = []
2020
unstable_const = []
21-
unstable_raw = []

README.md

+2-3
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,5 @@ Your crate root: (`lib.rs`/`main.rs`)
7575
If you intend to use `offset_of!` inside a `const fn`, also add the `const_fn` compiler feature.
7676

7777
### Raw references ###
78-
Recent nightlies support [a way to create raw pointers](https://github.com/rust-lang/rust/issues/73394) that avoids creating intermediate safe references.
79-
`memoffset` can make use of that feature to avoid what is technically Undefined Behavior.
80-
Use the `unstable_raw` feature to enable this.
78+
Since Rust 1.51.0, [a way to create raw pointers](https://github.com/rust-lang/rust/issues/73394) that avoids creating intermediate safe references
79+
has been stablilized, now `memoffset` makes use of that feature to avoid what is technically Undefined Behavior.

src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@
6767
const_raw_ptr_deref,
6868
)
6969
)]
70-
#![cfg_attr(feature = "unstable_raw", feature(raw_ref_macros))]
7170

7271
#[macro_use]
7372
#[cfg(doctests)]

src/raw_field.rs

+2-22
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,6 @@
1818
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
1919
// SOFTWARE.
2020

21-
/// `raw_const!`, or just ref-then-cast when that is not available.
22-
#[cfg(feature = "unstable_raw")]
23-
#[macro_export]
24-
#[doc(hidden)]
25-
macro_rules! _memoffset__raw_const {
26-
($path:expr) => {{
27-
$crate::ptr::raw_const!($path)
28-
}};
29-
}
30-
#[cfg(not(feature = "unstable_raw"))]
31-
#[macro_export]
32-
#[doc(hidden)]
33-
macro_rules! _memoffset__raw_const {
34-
($path:expr) => {{
35-
// This is UB because we create an intermediate reference to uninitialized memory.
36-
// Nothing we can do about that without `raw_const!` though.
37-
&$path as *const _
38-
}};
39-
}
40-
4121
/// Deref-coercion protection macro.
4222
#[cfg(allow_clippy)]
4323
#[macro_export]
@@ -88,7 +68,7 @@ macro_rules! raw_field {
8868
// of the field check we did above.
8969
#[allow(unused_unsafe)] // for when the macro is used in an unsafe block
9070
unsafe {
91-
_memoffset__raw_const!((*($base as *const $parent)).$field)
71+
$crate::ptr::addr_of!((*($base as *const $parent)).$field)
9272
}
9373
}};
9474
}
@@ -109,7 +89,7 @@ macro_rules! raw_field_tuple {
10989
// of the field check we did above.
11090
#[allow(unused_unsafe)] // for when the macro is used in an unsafe block
11191
unsafe {
112-
_memoffset__raw_const!((*($base as *const $parent)).$field)
92+
$crate::ptr::addr_of!((*($base as *const $parent)).$field)
11393
}
11494
}};
11595
}

0 commit comments

Comments
 (0)