From bcf50b5cdd60d7a5565d5f90d27e6ba31fbd2291 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sun, 9 Aug 2020 18:44:38 +0200 Subject: [PATCH] use MaybeUninit::as_ptr instead of transmute --- README.md | 18 ++---------------- src/lib.rs | 2 +- src/offset_of.rs | 4 +--- 3 files changed, 4 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 70bbead..b035674 100644 --- a/README.md +++ b/README.md @@ -68,24 +68,10 @@ features = ["unstable_const"] Your crate root: (`lib.rs`/`main.rs`) ```rust,ignore -#![feature(ptr_offset_from, const_ptr_offset_from, const_raw_ptr_deref)] +#![feature(ptr_offset_from, const_ptr_offset_from, const_maybe_uninit_as_ptr, const_raw_ptr_deref)] ``` -Or, if you intend to use `offset_of!` inside a `const fn`: -```rust,ignore -#![feature(ptr_offset_from, const_fn, const_fn_transmute, const_ptr_offset_from, const_raw_ptr_deref)] -``` - -and then: - -```rust,ignore -struct Foo { - a: u32, - b: u32, -} - -let foo = [0; offset_of!(Foo, b)] -``` +If you intend to use `offset_of!` inside a `const fn`, also add the `const_fn` compiler feature. ### Raw references ### Recent nightlies support [a way to create raw pointers](https://github.com/rust-lang/rust/issues/73394) that avoids creating intermediate safe references. diff --git a/src/lib.rs b/src/lib.rs index bf2e507..c85fb01 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -62,8 +62,8 @@ feature( ptr_offset_from, const_fn, - const_fn_transmute, const_ptr_offset_from, + const_maybe_uninit_as_ptr, const_raw_ptr_deref, ) )] diff --git a/src/offset_of.rs b/src/offset_of.rs index 4c46ed8..bb2bce1 100644 --- a/src/offset_of.rs +++ b/src/offset_of.rs @@ -31,9 +31,7 @@ macro_rules! _memoffset__let_base_ptr { // `let_base_ptr` declares a variable (several, actually) // instead of returning one. let uninit = $crate::mem::MaybeUninit::<$type>::uninit(); - // Transmuting for const-compatibility. - #[allow(unused_unsafe)] // for when the macro is used in an unsafe block - let $name: *const $type = unsafe { $crate::mem::transmute(&uninit) }; + let $name: *const $type = uninit.as_ptr(); }; } #[cfg(not(maybe_uninit))]