Skip to content

Commit ee46412

Browse files
authored
Merge pull request #70 from Gilnaa/gilad-4-stable-const
Automatically enable const evaluation
2 parents d8accb7 + 923a5f2 commit ee46412

5 files changed

Lines changed: 34 additions & 10 deletions

File tree

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ jobs:
3333
- 1.36.0 # Oldest supported with MaybeUninit
3434
- 1.40.0 # Oldest supported with cfg(doctest)
3535
- 1.51.0 # Oldest supported with ptr::addr_of!
36+
- 1.65.0 # Oldest supported with stable const evaluation (sans cell)
3637
- stable
3738
- beta
3839
- nightly

README.md

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,23 @@ fn main() {
4545
}
4646
```
4747

48-
## Feature flags ##
48+
## Usage in constants ##
49+
`memoffset` has support for compile-time `offset_of!` on rust>=1.65, or on older nightly compilers.
4950

50-
### Usage in constants ###
51-
`memoffset` has **experimental** support for compile-time `offset_of!` on a nightly compiler.
51+
### Usage on stable Rust ###
52+
Constant evaluation is automatically enabled and avilable on stable compilers starting with rustc 1.65.
5253

53-
In order to use it, you must enable the `unstable_const` crate feature and several compiler features.
54+
This is an incomplete implementation with one caveat:
55+
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.
56+
57+
This means that if need to get the offset of a cell, you'll have to remain on nightly for now.
58+
59+
### Usage on recent nightlies ###
60+
61+
If you're using a new-enough nightly and you require the ability to get the offset of a `Cell`,
62+
you'll have to enable the `unstable_const` cargo feature, as well as enabling `const_refs_to_cell` in your crate root.
63+
64+
Do note that `unstable_const` is an unstable feature that is set to be removed in a future version of `memoffset`.
5465

5566
Cargo.toml:
5667
```toml
@@ -59,6 +70,14 @@ version = "0.7"
5970
features = ["unstable_const"]
6071
```
6172

73+
Your crate root: (`lib.rs`/`main.rs`)
74+
```rust,ignore
75+
#![feature(const_refs_to_cell)]
76+
```
77+
78+
### Usage on older nightlies ###
79+
In order to use it on an older nightly compiler, you must enable the `unstable_const` crate feature and several compiler features.
80+
6281
Your crate root: (`lib.rs`/`main.rs`)
6382
```rust,ignore
6483
#![feature(const_ptr_offset_from, const_refs_to_cell)]

build.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,7 @@ fn main() {
1919
if ac.probe_rustc_version(1, 51) {
2020
println!("cargo:rustc-cfg=raw_ref_macros");
2121
}
22+
if ac.probe_rustc_version(1, 65) {
23+
println!("cargo:rustc-cfg=stable_const");
24+
}
2225
}

src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,10 @@
5757
5858
#![no_std]
5959
#![cfg_attr(
60-
feature = "unstable_const",
61-
feature(const_ptr_offset_from, const_refs_to_cell)
60+
all(feature = "unstable_const", not(stable_const)),
61+
feature(const_ptr_offset_from)
6262
)]
63+
#![cfg_attr(feature = "unstable_const", feature(const_refs_to_cell))]
6364

6465
#[macro_use]
6566
#[cfg(doctests)]

src/offset_of.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ macro_rules! _memoffset__let_base_ptr {
4646
}
4747

4848
/// Macro to compute the distance between two pointers.
49-
#[cfg(feature = "unstable_const")]
49+
#[cfg(any(feature = "unstable_const", stable_const))]
5050
#[macro_export]
5151
#[doc(hidden)]
5252
macro_rules! _memoffset_offset_from_unsafe {
@@ -58,7 +58,7 @@ macro_rules! _memoffset_offset_from_unsafe {
5858
unsafe { (field as *const u8).offset_from(base as *const u8) as usize }
5959
}};
6060
}
61-
#[cfg(not(feature = "unstable_const"))]
61+
#[cfg(not(any(feature = "unstable_const", stable_const)))]
6262
#[macro_export]
6363
#[doc(hidden)]
6464
macro_rules! _memoffset_offset_from_unsafe {
@@ -312,7 +312,7 @@ mod tests {
312312
assert_eq!(f_ptr as usize + 0, raw_field_union!(f_ptr, Foo, c) as usize);
313313
}
314314

315-
#[cfg(feature = "unstable_const")]
315+
#[cfg(any(feature = "unstable_const", stable_const))]
316316
#[test]
317317
fn const_offset() {
318318
#[repr(C)]
@@ -337,7 +337,7 @@ mod tests {
337337
assert_eq!([0; offset_of!(Foo, b)].len(), 4);
338338
}
339339

340-
#[cfg(feature = "unstable_const")]
340+
#[cfg(any(feature = "unstable_const", stable_const))]
341341
#[test]
342342
fn const_fn_offset() {
343343
const fn test_fn() -> usize {

0 commit comments

Comments
 (0)