Skip to content

Commit

Permalink
Use stablilized addr_of macro
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
fbq committed Mar 8, 2021
1 parent 9111400 commit f3a8d9b
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 27 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,3 @@ doc-comment = "0.3"
[features]
default = []
unstable_const = []
unstable_raw = []
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,5 @@ Your crate root: (`lib.rs`/`main.rs`)
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.
`memoffset` can make use of that feature to avoid what is technically Undefined Behavior.
Use the `unstable_raw` feature to enable this.
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
has been stablilized, now `memoffset` makes use of that feature to avoid what is technically Undefined Behavior.
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@
const_raw_ptr_deref,
)
)]
#![cfg_attr(feature = "unstable_raw", feature(raw_ref_macros))]

#[macro_use]
#[cfg(doctests)]
Expand Down
24 changes: 2 additions & 22 deletions src/raw_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,6 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

/// `raw_const!`, or just ref-then-cast when that is not available.
#[cfg(feature = "unstable_raw")]
#[macro_export]
#[doc(hidden)]
macro_rules! _memoffset__raw_const {
($path:expr) => {{
$crate::ptr::raw_const!($path)
}};
}
#[cfg(not(feature = "unstable_raw"))]
#[macro_export]
#[doc(hidden)]
macro_rules! _memoffset__raw_const {
($path:expr) => {{
// This is UB because we create an intermediate reference to uninitialized memory.
// Nothing we can do about that without `raw_const!` though.
&$path as *const _
}};
}

/// Deref-coercion protection macro.
#[cfg(allow_clippy)]
#[macro_export]
Expand Down Expand Up @@ -88,7 +68,7 @@ macro_rules! raw_field {
// of the field check we did above.
#[allow(unused_unsafe)] // for when the macro is used in an unsafe block
unsafe {
_memoffset__raw_const!((*($base as *const $parent)).$field)
$crate::ptr::addr_of!((*($base as *const $parent)).$field)
}
}};
}
Expand All @@ -109,7 +89,7 @@ macro_rules! raw_field_tuple {
// of the field check we did above.
#[allow(unused_unsafe)] // for when the macro is used in an unsafe block
unsafe {
_memoffset__raw_const!((*($base as *const $parent)).$field)
$crate::ptr::addr_of!((*($base as *const $parent)).$field)
}
}};
}

0 comments on commit f3a8d9b

Please sign in to comment.