Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for using raw_ref macros #43

Merged
merged 3 commits into from
Jun 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,36 @@ cache:
cargo: true
matrix:
include:
- name: miri
rust: nightly
os: linux
script:
- sh ci/miri.sh

- rust: 1.19.0 # Oldest supported (first version with numeric fields in struct patterns)
- rust: 1.31.0 # Oldest supported with allow(clippy)
- rust: 1.36.0 # Oldest supported with MaybeUninit
- rust: stable
- rust: beta
- rust: nightly

- env: MIRI
rust: nightly
os: linux
script:
- sh ci/miri.sh

- env: CONST_OFFSET
- name: all-features
rust: nightly
os: linux
script: # `--lib` added to prevent doctests from being compiled.
# This is due to `unstable_const` requiring extra `feature(...)` directives
# which the doctests do not have.
- cargo test --verbose --features unstable_const --lib
- cargo test --verbose --all-features --lib

- env: RUSTFMT
- name: rustfmt
rust: 1.36.0
install:
- rustup component add rustfmt
script:
- cargo fmt -- --check

- env: RUSTFLAGS="-D warnings"
- name: deny-warnings
env: RUSTFLAGS="-D warnings"
rust: 1.33.0 # `stable`: Locking down for consistent behavior
script:
- cargo check --tests
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ doc-comment = "0.3"
[features]
default = []
unstable_const = []
unstable_raw = []
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ fn main() {
}
```

## Usage in constants ##
## Feature flags ##

### Usage in constants ###
`memoffset` has **experimental** support for compile-time `offset_of!` on a nightly compiler.

In order to use it, you must enable the `unstable_const` crate feature and several compiler features.
Expand All @@ -78,4 +80,9 @@ struct Foo {
}

let foo = [0; offset_of!(Foo, b)]
```
```

### Raw references ###
Recent nightlies support [a way to create raw pointers](https://github.com/rust-lang/rust/issues/73394) that avoids creating intermediate safe references.
`memoffset` can make use of that feature to avoid what is technically Undefined Behavior.
Use the `unstable_raw` feature to enable this.
1 change: 1 addition & 0 deletions ci/miri.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ rustup component add miri
cargo miri setup

cargo miri test
cargo miri test --all-features
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
const_raw_ptr_deref
)
)]
#![cfg_attr(feature = "unstable_raw", feature(raw_ref_macros))]

#[macro_use]
#[cfg(doctests)]
Expand All @@ -79,7 +80,6 @@ doctest!("../README.md");
// Doing this enables this crate to function under both std and no-std crates.
#[doc(hidden)]
pub use core::mem;

#[doc(hidden)]
pub use core::ptr;

Expand Down
23 changes: 23 additions & 0 deletions src/offset_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,29 @@ macro_rules! _memoffset__field_check {
///
/// The `base` pointer *must not* be dangling, but it *may* point to
/// uninitialized memory.
#[cfg(feature = "unstable_raw")] // Correct variant that uses `raw_const!`.
#[macro_export(local_inner_macros)]
macro_rules! raw_field {
($base:expr, $parent:path, $field:tt) => {{
_memoffset__field_check!($parent, $field);
let base_ptr: *const $parent = $base;

// Get the field address without creating a reference.
// Crucially, we know that this will not trigger a deref coercion because
// of the `field_check!` we did above.
#[allow(unused_unsafe)] // for when the macro is used in an unsafe block
unsafe {
$crate::ptr::raw_const!((*base_ptr).$field)
}
}};
}

/// Computes a const raw pointer to the given field of the given base pointer
/// to the given parent type.
///
/// The `base` pointer *must not* be dangling, but it *may* point to
/// uninitialized memory.
#[cfg(not(feature = "unstable_raw"))] // Incorrect (UB) variant that creates an intermediate reference.
#[macro_export(local_inner_macros)]
macro_rules! raw_field {
($base:expr, $parent:path, $field:tt) => {{
Expand Down