Skip to content

Commit

Permalink
Merge pull request #84 from RalfJung/no-nightly
Browse files Browse the repository at this point in the history
stub out unstable_const feature
  • Loading branch information
Gilnaa authored Mar 26, 2024
2 parents d553d8c + 730bb05 commit 0ec6b3b
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 64 deletions.
19 changes: 1 addition & 18 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ jobs:
- 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
- 1.77.0 # Oldest supported with native `offset_of!`
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@master
Expand All @@ -46,20 +44,6 @@ jobs:
# with backwards compatibility workarounds.
run: cargo test --lib

nightly:
name: Test Suite (nightly features)
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@master
with:
toolchain: nightly
- name: Run cargo test
# `--lib` prevents doctests from being run.
# This is due to `unstable_const` requiring extra `feature(...)` directives
# which the doctests do not have.
run: cargo test --all-features --lib

miri:
name: Test Suite (Miri)
runs-on: ubuntu-latest
Expand All @@ -72,7 +56,6 @@ jobs:
- name: Test with Miri
run: |
cargo miri test
cargo miri test --all-features
style:
name: lints and formatting
Expand Down
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
- Clarify documentation about macro indirection
- Added changelog
- Turn the crate into a thin stdlib wrapper on rustc>=1.77
- Remove `unstable_offset_of`
- Turn `unstable_offset_of` and `unstable_const` into NOPs; they are not needed any more on recent nightlies

## v0.9.0 (18/05/2023)
### Added
- Cargo feature `unstable_offset_of` which turns the crate into an stdlib polyfill
- Cargo feature `unstable_offset_of` which turns the crate into a stdlib polyfill

## v0.8.0 (15/12/2022)
### Changed
Expand Down
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ doc-comment = "0.3"

[features]
default = []
# NOP features, solely so that people do not have to change their Cargo.toml
unstable_offset_of = []
unstable_const = []
36 changes: 3 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,37 +49,7 @@ fn main() {
```

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

### Usage on stable Rust ###
Constant evaluation is automatically enabled and available on stable compilers starting with rustc 1.65.

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 on a rustc version less than 1.77.

### Usage on somewhat recent nightlies ###

If you're using a nightly that does not yet have `core::mem::offset_of!()` 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
[dependencies.memoffset]
version = "0.9"
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)]
```
On versions below 1.77, 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.
5 changes: 0 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,6 @@
//! ```
#![no_std]
#![cfg_attr(
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
11 changes: 5 additions & 6 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(any(feature = "unstable_const", stable_const))]
#[cfg(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(any(feature = "unstable_const", stable_const)))]
#[cfg(not(stable_const))]
#[macro_export]
#[doc(hidden)]
macro_rules! _memoffset_offset_from_unsafe {
Expand Down Expand Up @@ -237,7 +237,6 @@ mod tests {
}

#[test]
#[cfg_attr(miri, ignore)] // this creates unaligned references
fn offset_simple_packed() {
#[repr(C, packed)]
struct Foo {
Expand Down Expand Up @@ -368,7 +367,7 @@ mod tests {
assert_eq!(f_ptr as usize + 0, raw_field_union!(f_ptr, Foo, c) as usize);
}

#[cfg(any(feature = "unstable_const", stable_offset_of, stable_const))]
#[cfg(any(stable_offset_of, stable_const))]
#[test]
fn const_offset() {
#[repr(C)]
Expand All @@ -381,7 +380,7 @@ mod tests {
assert_eq!([0; offset_of!(Foo, b)].len(), 4);
}

#[cfg(feature = "unstable_const")]
#[cfg(stable_offset_of)]
#[test]
fn const_offset_interior_mutable() {
#[repr(C)]
Expand All @@ -393,7 +392,7 @@ mod tests {
assert_eq!([0; offset_of!(Foo, b)].len(), 4);
}

#[cfg(any(feature = "unstable_const", stable_offset_of, stable_const))]
#[cfg(any(stable_offset_of, stable_const))]
#[test]
fn const_fn_offset() {
const fn test_fn() -> usize {
Expand Down

0 comments on commit 0ec6b3b

Please sign in to comment.