diff --git a/README.md b/README.md index e297b33..9d5088f 100644 --- a/README.md +++ b/README.md @@ -45,12 +45,18 @@ fn main() { } ``` -## Feature flags ## +## Usage in constants ## +`memoffset` has support for compile-time `offset_of!` on rust>=1.65, or on older nightly compilers. -### Usage in constants ### -`memoffset` has **experimental** support for compile-time `offset_of!` on a nightly compiler. +Stable support for const evaluation is not as complete as unstable due to dependence on [`#![feature(const_refs_to_cell)]`](/~https://github.com/rust-lang/rust/issues/80384). +This means that if need to get the offset of a cell, you'll have to remain on nightly for now. -In order to use it, you must enable the `unstable_const` crate feature and several compiler features. +### How to enable on stable ### +If you're running on any compiler newer than 1.65, there's no need to enable anything. + +### How to enable on an older nightly ### +In order to use it on an older nightly compiler, you must enable the `unstable_const` crate feature and several compiler features. +Do note that `unstable_const` is an unstable feature that is set to be removed in a future version of `memoffset`. Cargo.toml: ```toml diff --git a/build.rs b/build.rs index 0604c19..e18810f 100644 --- a/build.rs +++ b/build.rs @@ -19,4 +19,7 @@ fn main() { if ac.probe_rustc_version(1, 51) { println!("cargo:rustc-cfg=raw_ref_macros"); } + if ac.probe_rustc_version(1, 65) { + println!("cargo:rustc-cfg=stable_const"); + } } diff --git a/src/lib.rs b/src/lib.rs index d80ff17..72736aa 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -57,9 +57,10 @@ #![no_std] #![cfg_attr( - feature = "unstable_const", - feature(const_ptr_offset_from, const_refs_to_cell) + all(feature = "unstable_const", not(stable_const)), + feature(const_ptr_offset_from) )] +#![cfg_attr(feature = "unstable_const", feature(const_refs_to_cell))] #[macro_use] #[cfg(doctests)] diff --git a/src/offset_of.rs b/src/offset_of.rs index d070181..9ce4ae2 100644 --- a/src/offset_of.rs +++ b/src/offset_of.rs @@ -46,7 +46,7 @@ macro_rules! _memoffset__let_base_ptr { } /// Macro to compute the distance between two pointers. -#[cfg(feature = "unstable_const")] +#[cfg(any(feature = "unstable_const", stable_const))] #[macro_export] #[doc(hidden)] macro_rules! _memoffset_offset_from_unsafe { @@ -58,7 +58,7 @@ macro_rules! _memoffset_offset_from_unsafe { unsafe { (field as *const u8).offset_from(base as *const u8) as usize } }}; } -#[cfg(not(feature = "unstable_const"))] +#[cfg(not(any(feature = "unstable_const", stable_const)))] #[macro_export] #[doc(hidden)] macro_rules! _memoffset_offset_from_unsafe { @@ -312,7 +312,7 @@ mod tests { assert_eq!(f_ptr as usize + 0, raw_field_union!(f_ptr, Foo, c) as usize); } - #[cfg(feature = "unstable_const")] + #[cfg(any(feature = "unstable_const", stable_const))] #[test] fn const_offset() { #[repr(C)] @@ -337,7 +337,7 @@ mod tests { assert_eq!([0; offset_of!(Foo, b)].len(), 4); } - #[cfg(feature = "unstable_const")] + #[cfg(any(feature = "unstable_const", stable_const))] #[test] fn const_fn_offset() { const fn test_fn() -> usize {