Skip to content

Commit

Permalink
Merge pull request #70 from Gilnaa/gilad-4-stable-const
Browse files Browse the repository at this point in the history
Automatically enable const evaluation
  • Loading branch information
Gilnaa authored Dec 11, 2022
2 parents d8accb7 + 923a5f2 commit ee46412
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 10 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ jobs:
- 1.36.0 # Oldest supported with MaybeUninit
- 1.40.0 # Oldest supported with cfg(doctest)
- 1.51.0 # Oldest supported with ptr::addr_of!
- 1.65.0 # Oldest supported with stable const evaluation (sans cell)
- stable
- beta
- nightly
Expand Down
27 changes: 23 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,23 @@ 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.
### Usage on stable Rust ###
Constant evaluation is automatically enabled and avilable on stable compilers starting with rustc 1.65.

In order to use it, you must enable the `unstable_const` crate feature and several compiler features.
This is an incomplete implementation with one caveat:
Due to dependence on [`#![feature(const_refs_to_cell)]`](https://github.com/rust-lang/rust/issues/80384), you cannot get the offset of a `Cell` field in a const-context.

This means that if need to get the offset of a cell, you'll have to remain on nightly for now.

### Usage on recent nightlies ###

If you're using a new-enough nightly and you require the ability to get the offset of a `Cell`,
you'll have to enable the `unstable_const` cargo feature, as well as enabling `const_refs_to_cell` in your crate root.

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 All @@ -59,6 +70,14 @@ version = "0.7"
features = ["unstable_const"]
```

Your crate root: (`lib.rs`/`main.rs`)
```rust,ignore
#![feature(const_refs_to_cell)]
```

### Usage on older nightlies ###
In order to use it on an older nightly compiler, you must enable the `unstable_const` crate feature and several compiler features.

Your crate root: (`lib.rs`/`main.rs`)
```rust,ignore
#![feature(const_ptr_offset_from, const_refs_to_cell)]
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 ee46412

Please sign in to comment.