Skip to content

Commit

Permalink
Automatically enable const evaluation
Browse files Browse the repository at this point in the history
When running on a compiler newer than 1.65,
automatically enable usage of the macro in const contexts.

Closes #4
  • Loading branch information
Gilnaa committed Dec 8, 2022
1 parent d8accb7 commit 3db5f23
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 10 deletions.
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
5 changes: 3 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
8 changes: 4 additions & 4 deletions src/offset_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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)]
Expand All @@ -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 {
Expand Down

0 comments on commit 3db5f23

Please sign in to comment.