Skip to content

Commit 49a01c3

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) if Rust 1.51.0 and above is used, otherwise use the original fallback version, which works in a less technically correct way. [1]: rust-lang/rust#72279 Signed-off-by: Boqun Feng <[email protected]>
1 parent 9111400 commit 49a01c3

File tree

5 files changed

+12
-16
lines changed

5 files changed

+12
-16
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

-5
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,3 @@ Your crate root: (`lib.rs`/`main.rs`)
7373
```
7474

7575
If you intend to use `offset_of!` inside a `const fn`, also add the `const_fn` compiler feature.
76-
77-
### 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.

build.rs

+3
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,7 @@ fn main() {
1616
if ac.probe_rustc_version(1, 40) {
1717
println!("cargo:rustc-cfg=doctests");
1818
}
19+
if ac.probe_rustc_version(1, 51) {
20+
println!("cargo:rustc-cfg=raw_ref_macros");
21+
}
1922
}

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

+9-9
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,22 @@
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")]
21+
/// `addr_of!`, or just ref-then-cast when that is not available.
22+
#[cfg(raw_ref_macros)]
2323
#[macro_export]
2424
#[doc(hidden)]
25-
macro_rules! _memoffset__raw_const {
25+
macro_rules! _memoffset__addr_of {
2626
($path:expr) => {{
27-
$crate::ptr::raw_const!($path)
27+
$crate::ptr::addr_of!($path)
2828
}};
2929
}
30-
#[cfg(not(feature = "unstable_raw"))]
30+
#[cfg(not(raw_ref_macros))]
3131
#[macro_export]
3232
#[doc(hidden)]
33-
macro_rules! _memoffset__raw_const {
33+
macro_rules! _memoffset__addr_of {
3434
($path:expr) => {{
3535
// This is UB because we create an intermediate reference to uninitialized memory.
36-
// Nothing we can do about that without `raw_const!` though.
36+
// Nothing we can do about that without `addr_of!` though.
3737
&$path as *const _
3838
}};
3939
}
@@ -88,7 +88,7 @@ macro_rules! raw_field {
8888
// of the field check we did above.
8989
#[allow(unused_unsafe)] // for when the macro is used in an unsafe block
9090
unsafe {
91-
_memoffset__raw_const!((*($base as *const $parent)).$field)
91+
_memoffset__addr_of!((*($base as *const $parent)).$field)
9292
}
9393
}};
9494
}
@@ -109,7 +109,7 @@ macro_rules! raw_field_tuple {
109109
// of the field check we did above.
110110
#[allow(unused_unsafe)] // for when the macro is used in an unsafe block
111111
unsafe {
112-
_memoffset__raw_const!((*($base as *const $parent)).$field)
112+
_memoffset__addr_of!((*($base as *const $parent)).$field)
113113
}
114114
}};
115115
}

0 commit comments

Comments
 (0)